-
Notifications
You must be signed in to change notification settings - Fork 31
/
0242-valid-anagram.cpp
71 lines (56 loc) · 1.92 KB
/
0242-valid-anagram.cpp
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
/*
Problem: LeetCode 242 - Valid Anagram
Description:
Given two strings s and t, return true if t is an anagram of s, and false otherwise.
Intuition:
To determine if two strings are valid anagrams, we can compare the counts of each character in both strings. If the counts of all characters are equal, the strings are valid anagrams.
Approach:
1. If the lengths of the two input strings s and t are not equal, return false.
2. Initialize an array of size 26 to store the counts of each character.
3. Iterate through each character in string s and increment its count in the array.
4. Iterate through each character in string t and decrement its count in the array.
5. If any count in the array is not zero, return false as the characters do not match.
6. Return true if all counts in the array are zero.
Time Complexity:
The time complexity of this approach is O(n), where n is the length of the input strings s and t. This is because we iterate through both strings once to update the counts of each character.
Space Complexity:
The space complexity is O(1), as we use a fixed-size array of size 26 to store the counts.
*/
auto init = []() {
ios::sync_with_stdio(false);
cin.tie(0);
cout.tie(0);
return 0;
}
();
class Solution {
public:
bool isAnagram(string s, string t) {
if (s.length() != t.length()) {
return false;
}
int count[26] = {0};
for (char ch : s) {
count[ch - 'a']++;
}
for (char ch : t) {
count[ch - 'a']--;
}
for (int i = 0; i < 26; i++) {
if (count[i] != 0) {
return false;
}
}
return true;
}
};
// class Solution {
// public:
// bool isAnagram(string s, string t) {
// if(s.size() != t.size())
// return false;
// sort(s.begin(), s.end());
// sort(t.begin(), t.end());
// return s == t;
// }
// };