[LeetCode] Valid Anagram

[LeetCode] Valid Anagram

分类:c++oj

c++leetcode

Valid Anagram

Given two stringssandt, write a function to determine iftis an anagram ofs.

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

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

解题思路:

这道题的难点在于弄清题意。字谜游戏,给定两个词,,判断这两个词是否只有组成的字母顺序不一样。而后说明两个字符串只包含小写字母。于是我们可以用一个26长度的数组来计数。

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

版权声明:本文为博主原创文章,未经博主允许不得转载。

上一篇Azure搭建SVN服务器及客户端简介下一篇[LeetCode] Remove Duplicates from Sorted List II

顶0踩0

君子当权积福,小人仗势欺人。

[LeetCode] Valid Anagram

相关文章:

你感兴趣的文章:

标签云: