Valid Anagrammy SubmissionsQuestionTotal accepted:43694 Total submissions:111615 difficulty:easy
Given, Strings s and T, write a function to determine if it is a anagram of s.
For example,
s = "Anagram", t = "Nagaram", return true.
s = "Rat", t = "Car", return false.
Note:
Assume the string contains only lowercase alphabets.
Follow up:
What if the inputs contain Unicode characters? How would adapt your solution to such case?
This method can be used to compare all including Unicode characters.
To sort the interior of a string, this method must be remembered!
The head function of this method is # include <algorithm>
C + + notation:
1 classSolution {2 Public:3 BOOLIsanagram (stringSstringt) {4 sort (S.begin (), S.end ());5 sort (T.begin (), T.end ());6 if(s = =t)7 return true;8 Else 9 return false;Ten } One};
My solution time is too long, here are some simple solutions in discuss:
- The idea was simple. It creates a size int arrays as buckets in alphabet. It increments the bucket value with a string s and decrement with string T. So if they was anagrams, all buckets should remain with initial value which is zero. So just checking that and return
1 Public classSolution {2 Public BooleanIsanagram (string s, String t) {3 int[] Alphabet =New int[26];4 for(inti = 0; I < s.length (); i++) Alphabet[s.charat (i)-' a ']++;5 for(inti = 0; I < t.length (); i++) Alphabet[t.charat (i)-' a ']--;6 for(intI:alphabet)if(I! = 0)return false;7 return true;8 }9}
See the other solution found that the core ideas are the same, but the statement of the different expression, some simple such as the above to learn.
"09_242" Valid anagram