[leetcode]242. Valid Anagram

[leetcode]242. Valid Anagram


Analysis

厉害的人总是那么的厉害呀~—— [菜鸡反省中~]

Given two strings s and t , write a function to determine if t is an anagram of s.
简单来说就是判断两个字符串中的字符是否都一样,很简单,直接把所有字符排一下序,然后遍历一遍就可以了。

Implement

class Solution {
public:
    bool isAnagram(string s, string t) {
        if(s.length() != t.length())
            return false;
        vector<char> str1, str2;
        for(int i=0; i<s.length(); i++){
            str1.push_back(s[i]);
            str2.push_back(t[i]);
        }
        sort(str1.begin(), str1.end());
        sort(str2.begin(), str2.end());
        for(int i=0;i<str1.size(); i++){
            if(str1[i] != str2[i])
                return false;
        }
        return true;
    }
};

猜你喜欢

转载自blog.csdn.net/weixin_32135877/article/details/80680602