-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathAnagram.cpp
127 lines (119 loc) · 2.38 KB
/
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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
// Program using STL(Standard Template Library)
#include<bits/stdc++.h>
using namespace std;
int main()
{
int t, i,j,n1,n2,flag=0;;
cin>>t;
string s1[t+1],s2[t+1];
getline(cin,s1[0]);
for(i=0; i<t; i++)
{
getline(cin,s1[i]);
getline(cin,s2[i]);
sort(s1[i].begin() , s1[i].end());
sort(s2[i].begin() ,s2[i].end());
n1 = s1[i].length();
n2 = s2[i].length();
if(n1!=n2)
{
flag=0;
break;
}
else
{
for(j=0; j<n1; j++)
{
if(s1[i][j]!=s2[i][j])
{
flag=0;
break;
}
else
{
flag=1;
}
}
}
if(flag==0)
{
cout<<" Strings are not anagram"<<endl;
}
else
{
cout<<" Strings are anagram"<<endl;
}
}
}
-----------------------------------------------------------------------------------------------------------------------------
// Program using quicksort
#include<bits/stdc++.h>
using namespace std;
int partition(string &s , int start , int end)
{
char pivot = s[end];
int pindex = start;
for(int i= start ; i<end; i++)
{
if(s[i]<=pivot)
{
swap(s[i] , s[pindex]);
pindex++;
}
}
swap(s[pindex] ,s[end]);
return pindex;
}
void quicksort(string &s , int start ,int end)
{
if(start<end)
{
int pindex = partition(s,start ,end);
quicksort(s,start ,pindex-1);
quicksort(s,pindex+1,end);
}
}
int main()
{
int t, i,j,n1,n2,flag=0;;
cin>>t;
string s1[t+1],s2[t+1];
getline(cin,s1[0]);
for(i=0; i<t; i++)
{
getline(cin,s1[i]);
getline(cin,s2[i]);
n1 = s1[i].length();
n2 = s2[i].length();
if(n1!=n2)
{
flag=0;
break;
}
else
{
quicksort(s1[i],0,n1-1);
quicksort(s2[i],0,n2-1);
for(j=0; j<n1; j++)
{
if(s1[i][j]!=s2[i][j])
{
flag=0;
break;
}
else
{
flag=1;
}
}
}
if(flag==0)
{
cout<<" Strings are not anagram"<<endl;
}
else
{
cout<<" Strings are anagram"<<endl;
}
}
}