-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path1.2(checkPermutation).cpp
More file actions
59 lines (52 loc) · 1.12 KB
/
1.2(checkPermutation).cpp
File metadata and controls
59 lines (52 loc) · 1.12 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
/*
Check Permutation:
Given 2 strings, write an method to decide if one is a permutation of the other
*/
#include <iostream>
#include <string>
#include <algorithm>
#include <vector>
using namespace std;
/*
//sort = nlog n time complexity
bool ispermutation(string str1, string str2){
bool ispermutation =false;
if(str1.size()!=str2.size()){
return false;
}else{
sort(str1.begin(), str1.end());
sort(str2.begin(), str2.end());
for(int i=0; i<str1.size(); i++){
if(str1[i]!=str2[i]){
return false;
}
}
return true;
}
}
*/
//alternativly this will have O(n+m) with n extra space
bool ispermutation(string str1, string str2){
vector <int> m(str1.size(),0);
if(str1.size()!=str2.size()) return false;
for(int i=0; i<str1.size(); i++) m[str1[i]-'a']++;
for(int i=0; i<str2.size(); i++){
if (m[str2[i]-'a']<=0) return false;
else m[str2[i]-'a']--;
}
for(int i=0; i<m.size(); i++){
if(m[i]!=0) return false;
}
return true;
}
int main(){
string str1 = "abcdeff";
string str2 = "abcdeff";
if(ispermutation(str1, str2)){
cout<<"is permutation";
}else
{
cout<<"not permutation";
}
return 0;
}