Description
Write a method to anagram(s,t)
decide if strings is anagrams or not.
Clarification
What is anagram?
- Strings is anagram if they can is 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
.
Challenge
O (n) time, O (1) Extra space
Problem solving: The topic given two strings, judging whether the two strings in addition to the character sequence is different, is equal. The easiest thing to think about is to sort the characters and determine if the bitwise characters are equal. In Java, however, to sort the characters in a string and only convert it to a character array, the challenge condition is not met. But with other methods (such as a hash table), the code can only handle characters in letters or ASCII, which is detrimental to generality. Paste the code for the sorting method:
1 Public classSolution {2 /**3 * @params:the First String4 * @paramt:the Second String5 * @return: TRUE or False6 */7 Public BooleanAnagram (string s, String t) {8 //Write your code here9 Char[]arr_s =S.tochararray ();Ten Char[]arr_t =T.tochararray (); One Arrays.sort (arr_s); A Arrays.sort (arr_t); - returnstring.valueof (arr_s). Equals (String.valueof (arr_t)); - } the}
158. Valid Anagram "Lintcode by Java"