Two Strings Are Anagrams
2016-09-09 18:04
316 查看
Write a method
decide if two strings are anagrams or not.
判断两个字符串包含的字母知否一致,也就是是否能够通过改变字母的顺序变成相同的字符串。
如果一致,则返回true,如果不一致,返回false。
Clarification
What is Anagram?
- Two strings are anagram if they can be the same after change the order of characters.
Example
Given s =
=
Given s =
return
Given s =
return
anagram(s,t)to
decide if two strings are anagrams or not.
判断两个字符串包含的字母知否一致,也就是是否能够通过改变字母的顺序变成相同的字符串。
如果一致,则返回true,如果不一致,返回false。
Clarification
What is Anagram?
- Two strings are anagram if they can be the same after change the order of characters.
Example
Given s =
"abcd", t
=
"dcab", return
true.
Given s =
"ab", t =
"ab",
return
true.
Given s =
"ab", t =
"ac",
return
false.
public class Solution { /** * @param s: The first string * @param b: The second string * @return true or false */ public boolean anagram(String s, String t) { if (s.length() != t.length()) { return false; } int[] count = new int[256]; for(int i = 0; i < s.length(); i++) { count[(int) s.charAt(i)]++; } for(int j = 0; j < t.length(); j++) { count[(int) t.charAt(j)]--; if (count[(int) t.charAt(j)] < 0){ return false; } } return true; } };
相关文章推荐
- #158 Two Strings Are Anagrams
- Lintcode158 Two Strings Are Anagrams solution 题解
- Two Strings Are Anagrams
- lintcode --- Two Strings Are Anagrams
- two-strings-are-anagrams&&compare-strings&&longest-words
- Two Strings Are Anagrams
- 【4】Decide if two strings are anagrams or not
- LintCode Two Strings Are Anagrams 两个字符串是变位词
- [Amazon] Two Strings Are Anagrams (Compare Strings)
- [LintCode]Two Strings Are Anagrams(C++|Java|Python)
- Two Strings Are Anagrams-LintCode
- [CrackCode] 1.4 Write a method to decide if two strings are anagrams or not
- Two Strings Are Anagrams
- Lintcode: Two Strings Are Anagrams
- Two Strings are Anagram
- lintcode-easy-Two Strings Are Annagrams
- Check whether two strings are anagram of each other
- Which two are the uses of the ASM metadata backup and restore (AMBR) feature? (Choose two.)
- [shiro] Wildcard string cannot be null or empty. Make sure permission strings are properly formatted.
- 583. Delete Operation for Two Strings