-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path242. Valid Anagram.cpp
76 lines (60 loc) · 1.51 KB
/
242. 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
72
73
74
75
76
/*
Link: https://leetcode.com/problems/valid-anagram/
Given two strings s and t , write a function to determine if t is an anagram of s.
Example 1:
Input: s = "anagram", t = "nagaram"
Output: true
Example 2:
Input: s = "rat", t = "car"
Output: 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?
*/
// Solution 1 - using sorting
class Solution {
public:
bool isAnagram(string s, string t) {
sort(s.begin(), s.end()); // sorting s
sort(t.begin(), t.end()); // sorting t
if(s == t) // comparing them
return true;
return false;
}
};
/*
TC = O(n log n)
SC = O(1)
*/
// Solution 2
class Solution {
public:
bool isAnagram(string s, string t) {
int i, n = s.size(), m = t.size();
if (n != m) {
return false;
}
map<int, int>mp;
for (i = 0; i < n; i++) {
mp[s[i]]++;
}
for (i = 0; i < m; i++) {
if (!mp[t[i]]){
return false;
} else {
mp[t[i]]--;
}
}
return true;
}
};
/*
TC = O(n)
SC = O(1) space complexity is constant as the input if english alphabet hence.
*/
/*
Follow Up:
What if the inputs contain unicode characters? How would you adapt your solution to such case?
- We can use map for that purpose
*/