leetcode242—Valid Anagram
阿新 • • 發佈:2018-11-13
padding 字符串 cte UNC adding hat vector nic pre
Given two strings s and t , write a function to determine if t is an anagram of s.
Example 1:
Input: s = "anagram", t = "nagaram" Output: true
Example 2:
Input: s = "rat", t = "car" Output: false
Note:
You may assume the string contains only lowercase alphabets.
Follow up:
What if the inputs contain unicode characters? How would you adapt your solution to such case?
想法:將字符串s和字符串t分別轉成容器存儲,然後按字符順序排序。再比較兩個容器是否相等。
classSolution { public: bool isAnagram(string s, string t) { vector<char> st1; vector<char> st2; for(int i = 0 ; i < s.size() ; i++){ st1.push_back(s.at(i)); } sort(st1.begin(),st1.end()); for(int j = 0 ; j < t.size() ; j++){ st2.push_back(t.at(j)); } sort(st2.begin(),st2.end());return st1 == st2; } };
leetcode242—Valid Anagram