Skip to content

Commit b82d35f

Browse files
committed
Solve valid-anagram
1 parent ce295ce commit b82d35f

1 file changed

Lines changed: 35 additions & 0 deletions

File tree

valid-anagram/OstenHun.cpp

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
Given two strings s and t, return true
3+
if t is an anagram of s, and false otherwise.
4+
5+
An anagram is a word or phrase formed by rearranging
6+
the letters of a different word or phrase,
7+
using all the original letters exactly once.
8+
*/
9+
10+
#include <string>
11+
using namespace std;
12+
13+
class Solution {
14+
public:
15+
bool isAnagram(const string& s, const string& t) {
16+
if (s.length() != t.length()) {
17+
return false;
18+
}
19+
20+
int freq[26] = {};
21+
22+
for (size_t i = 0; i < s.length(); i++) {
23+
freq[s[i] - 'a']++;
24+
freq[t[i] - 'a']--;
25+
}
26+
27+
for (int count : freq) {
28+
if (count != 0) {
29+
return false;
30+
}
31+
}
32+
33+
return true;
34+
}
35+
};

0 commit comments

Comments
 (0)