242. Valid Anagram

来源:互联网 发布:淘宝网m 编辑:程序博客网 时间:2024/06/05 04:31

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.

思路: 

anagram n. 由颠倒字母顺序而构成的字[短语];



给定两个字符串,字符串的大小一样,只有顺序不一样。

两个字符串只包含小写字母。


class Solution {public:    bool isAnagram(string s, string t) {        int len_s = s.length();        int len_t = t.length();                if(len_s != len_t){            return false;        }                vector<int> count(26, 0);                for(int i =0; i < len_s; i++){            count[s[i] - 'a']++;        }                for(int i = 0; i < len_t; i++){           if( --count[t[i]-'a']<0 )           {               return false;           }                    }        return true;    }};




0 0
原创粉丝点击