leetcode242—Valid Anagram

Posted tingweichen

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了leetcode242—Valid Anagram相关的知识,希望对你有一定的参考价值。

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;
    }
};



以上是关于leetcode242—Valid Anagram的主要内容,如果未能解决你的问题,请参考以下文章

leetcode 242. Valid Anagram

Leetcode 242 Valid Anagram

leetcode 242. Valid Anagram

242. Valid Anagram(leetcode)

[leetcode-242-Valid Anagram]

[leetcode]242.Valid Anagram