-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcheck_permutation.cpp
More file actions
47 lines (44 loc) · 927 Bytes
/
check_permutation.cpp
File metadata and controls
47 lines (44 loc) · 927 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
42
43
44
45
46
47
/*
Given two strings, write a method to decide if one is a permutation of the
other. (Also called as ANAGRAM)
*/
#include<bits/stdc++.h>
using namespace std;
bool is_perm(string s1, string s2)
{
if(s1.length()!=s2.length())
return false;
sort(s1.begin(),s1.end());
sort(s2.begin(),s2.end());
if(s1==s2)
return true;
return false;
}
//permuation is words having same character count
//assumption ascii char set
bool is_perm_eff(string s1,string s2)
{
if(s1.length()!=s2.length())
return false;
int count[128]={};
for(int i=0;i<s1.length();i++)
{ int val = s1[i];
count[val]++; //count number of occurances of each character of string 1
}
for(int i=0;i<s2.length();i++)
{
int val=s2[i];
count[val]--;
if(count[val]<0)
return false;
}
return true;
}
int main()
{
string a,b;
cout<<"Enter two strings: \n";
cin>>a>>b;
cout<<is_perm(a,b)<<endl; //O(nlogn)
cout<<is_perm_eff(a,b)<<endl;
}