Problem:
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.
Summary:
Give the string s and T, and determine if T is the same letter of the same alphabetic word as S.
Analysis:
1. Hash table, record the number of letters in S, and then determine whether the letters in t have the same occurrence frequency.
1 classSolution {2 Public:3 BOOLIsanagram (stringSstringt) {4 intLen1 = S.size (), len2 = T.size (), ch[ -] = {0};5 6 for(inti =0; i < len1; i++) {7 intTMP = S[i]-'a';8ch[tmp]++;9 }Ten One for(inti =0; i < len2; i++) { A intTMP = T[i]-'a'; -ch[tmp]--; - } the - for(inti =0; I < -; i++) { - if(Ch[i]) { - return false; + } - } + A return true; at } -};
2. The characters in the two strings are ordered from small to large, and the two strings are determined to be equal after sorting. But this method is less efficient.
1 classSolution {2 Public:3 BOOLIsanagram (stringSstringt) {4 sort (S.begin (), S.end ());5 sort (T.begin (), T.end ());6 7 returns = = T?true:false;8 }9};
Leetcode 242 Valid Anagram