leetcode 242. Valid Anagram

来源:互联网 发布:乐乎公寓电话 编辑:程序博客网 时间:2024/05/29 11:20

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.


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






0 0
原创粉丝点击