首页 > 其他 > 详细

Valid Anagram

时间:2016-02-29 21:32:00      阅读:175      评论: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.

cpp:

class Solution {
public:
    bool isAnagram(string s, string t) {
        if(s.size() != t.size())    return false;
        int n=s.size();
        vector<int> counts(26, 0);
        for(int i=0; i<n; i++)
        {
            counts[s[i]-97]++;
            counts[t[i]-97]--;
        }
        for(auto count:counts)
            if(count)   return false;
        return true;
    }
};

python:

class Solution(object):
    def isAnagram(self,s,t):
        listA = [0]*26
        if len(s) != len(t):
            return False
        elif s == ‘‘ and t ==‘‘:
            return True
        else:
            for i in range(0,len(s)):
                listA[ord(s[i])-97] += 1
                listA[ord(t[i])-97] -= 1
            for i in range(0,26):
                if listA[i]!= 0:
                    return False
        return True
        

 

Valid Anagram

原文:http://www.cnblogs.com/wxquare/p/5228821.html

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