首页 > 其他 > 详细

[LeetCode] Valid Anagram 验证变位词

时间:2015-08-02 06:17:27      阅读:1558      评论:0      收藏:0      [点我收藏+]

 

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.

 

这不算一道难题,核心点就在于使用哈希表映射,我们还是用一个数组来代替哈希表,使用类似方法的题目有Minimum Window Substring 最小窗口子串Isomorphic Strings 同构字符串Longest Substring Without Repeating Characters 最长无重复子串1.1 Unique Characters of a String 字符串中不同的字符。我们先判断两个字符串长度是否相同,不相同直接返回false。然后把s中所有的字符出现个数统计起来,存入一个大小为26的数组中,因为题目中限定了输入字符串为小写字母组成。然后我们再来统计t字符串,如果发现不匹配则返回false。 参见代码如下:

 

class Solution {
public:
    bool isAnagram(string s, string t) {
        if (s.size() != t.size()) return false;
        int m[26] = {0};
        for (int i = 0; i < s.size(); ++i) ++m[s[i] - a];
        for (int i = 0; i < t.size(); ++i) {
            if (--m[t[i] - a] < 0) return false;
        }
        return true;
    }
};

 

LeetCode All in One 题目讲解汇总(持续更新中...)

[LeetCode] Valid Anagram 验证变位词

原文:http://www.cnblogs.com/grandyang/p/4694988.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!