2017算法课.04(Valid Anagram )

来源:互联网 发布:30岁精华推荐知乎 编辑:程序博客网 时间:2024/04/28 08:41

Given two strings s and t, write a function to determine if t is an anagram of s.

For example,
s = "anagram", t = "nagaram", return true.
s = "rat", t = "car", return false.

Note:
You may assume the string contains only lowercase alphabets.



这里首先如果字符串大小不同。直接return false


如果长度相同,那么,字符串所拥有的字符也要相同。这里可以先进行排序,然后再进行比较更加方便。这里用到,sort函数,进行排序。

class Solution {
public:
    bool isAnagram(string s, string t) {
        sort(s.begin(), s.end());
        sort(t.begin(), t.end());
        return s == t;
    }
};

0 0
原创粉丝点击