-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeleteanagramword.cpp
More file actions
59 lines (46 loc) · 1.1 KB
/
Copy pathdeleteanagramword.cpp
File metadata and controls
59 lines (46 loc) · 1.1 KB
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
#include<bits/stdc++.h>
using namespace std;
bool isAnagram(string a, string b){
if(a.length() != b.length()){
return false;
}else{
if(a == b){
return true;
}else{
array<int, 26> h1;
array<int, 26> h2;
h1.fill(0);
h2.fill(0);
for(int i = 0 ; i < a.length() ; i++){
h1[a[i] - 'a']++;
h2[b[i] - 'a']++;
}
return (h1 == h2);
}
}
}
bool isAnagramSort(string a, string b){
sort(a.begin(), a.end());
sort(b.begin(), b.end());
return (a == b);
}
vector<string> removeAnagrams(vector<string>& words) {
auto it = words.begin()+1;
while(it < words.end()){
if(isAnagram(*(it), *(it-1))){
words.erase(it);
continue;
}
it++;
}
return words;
}
int main(){
vector<string> a = {"abba","baba","bbaa","cd","cd"};
vector<string> b = removeAnagrams(a);
for(string i : b){
cout << i << ", ";
}
cout << endl << isAnagramSort("ada", "caa");
return 0;
}