-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathValidAnagram.cpp
More file actions
37 lines (27 loc) · 802 Bytes
/
ValidAnagram.cpp
File metadata and controls
37 lines (27 loc) · 802 Bytes
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
37
// https://leetcode.com/explore/interview/card/top-interview-questions-easy/127/strings/882/
#include <iostream>
bool isAnagram(std::string s, std::string t) {
if (s.size() != t.size()) {
return false;
}
int table[256] {0};
for (int i {0}; i < s.size(); ++i) {
table[s[i]]++;
}
for (int i {0}; i < s.size(); ++i) {
if (--table[t[i]] < 0) {
return false;
}
}
return true;
}
int main()
{
std::string s = "anagram";
std::string t = "nagaram";
std::cout << s << " and " << t << " : " << std::boolalpha << isAnagram(s, t) << std::endl;
s = "rat";
t = "car";
std::cout << s << " and " << t << " : " << std::boolalpha << isAnagram(s, t) << std::endl;
return 0;
}