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分别转成容器存储,然后按字符顺序排序。再比较两个容器是否相等。
class Solution { 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; } };
原文地址:https://www.cnblogs.com/tingweichen/p/9951659.html
时间: 2024-10-11 16:40:03