-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalid-anagram.cpp
More file actions
36 lines (33 loc) · 1.1 KB
/
valid-anagram.cpp
File metadata and controls
36 lines (33 loc) · 1.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
// Source : https://leetcode.com/problems/valid-anagram/
// Author : RQY
// Date : 2016-09-04
/**********************************************************************************
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.
Follow up:
What if the inputs contain unicode characters? How would you adapt your solution to such case?
**********************************************************************************/
class Solution {
public:
bool isAnagram(string s, string t) {
if(s.length() != t.length())
return false;
int myhash[26];
memset(myhash, 0, sizeof(myhash));
for(auto ch : s){
myhash[ch - 'a'] += 1;
}
int i;
for(i = 0; i < t.length(); ++i){
if(myhash[t[i] - 'a'] > 0)
myhash[t[i] - 'a'] -= 1;
else
return false;
}
return true;
}
};