【题目描述】
Write a methodanagram(s,t)to decide if two strings are anagrams or not.
写出一个函数anagram(s, t)判断两个字符串是否可以通过改变字母的顺序变成一样的字符串。
【题目链接】
www.lintcode.com/en/problem/two-strings-are-anagrams/
【题目解析】
可建立一个长度为256的数组,统计所有256个字符在String s出现的次数,然后减去这些字符在String t中出现的次数。若某个字符统计次数最终小于0,说明t中这个字符比s中更多,返回false。否则,循环结束,说明所有字符在s和t中出现的次数一致,返回true。
【参考答案】
Lintcode158 Two Strings Are Anagrams solution 题解
最新推荐文章于 2021-02-23 20:43:02 发布