-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathValid+anarams.cpp
More file actions
41 lines (36 loc) · 971 Bytes
/
Valid+anarams.cpp
File metadata and controls
41 lines (36 loc) · 971 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
38
39
40
41
class Solution {
public:
bool isAnagram(string s, string t)
{
if(s.size()!=t.size()) return false;
int n=s.size();
unordered_map<char,int> m;
for(int i=0;i<n;i++){
m[s[i]]++;
m[t[i]]--;
}
for(auto count:m){
if(count.second) return false;
}
return true;
// --------------------- solution-------------------- uising char array
if(s.size()!=t.size()) return false;
int n=s.size();
int arr[26]={0};
for(int i=0;i<n;i++)
{
arr[s[i]-'a']++;
arr[t[i]-'a']--;
}
for(int i=0;i<26;i++)
{
if(arr[i]) return false;
}
return true;
//--------------------using sorting---------------
sort(s.begin(),s.end());
sort(t.begin(),t.end());
if(s==t) return true;
return false;
}
};