Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings
This repository was archived by the owner on Jun 26, 2022. It is now read-only.

Commit025278e

Browse files
Merge pull request#221 from swatiidixit/master
anagram pairs in string added
2 parentsd044410 +afb67b2 commit025278e

File tree

1 file changed

+50
-0
lines changed

1 file changed

+50
-0
lines changed
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
#include<bits/stdc++.h>
2+
usingnamespacestd;
3+
#defineNO_OF_CHARS256
4+
5+
/* function to check whether two strings are anagram of each other*/
6+
boolareAnagram(string str1, string str2)
7+
{
8+
// Create two count arrays and initialize all values as 0
9+
int count[NO_OF_CHARS] = {0};
10+
int i;
11+
12+
// For each character in input strings, increment count in
13+
// the corresponding count array
14+
for (i =0; str1[i] && str2[i]; i++)
15+
{
16+
count[str1[i]]++;
17+
count[str2[i]]--;
18+
}
19+
20+
// If both strings are of different length. Removing this condition
21+
// will make the program fail for strings like "aaca" and "aca"
22+
if (str1[i] || str2[i])
23+
returnfalse;
24+
25+
// See if there is any non-zero value in count array
26+
for (i =0; i < NO_OF_CHARS; i++)
27+
if (count[i])
28+
returnfalse;
29+
returntrue;
30+
}
31+
32+
// This function prints all anagram pairs in a given array of strigns
33+
voidfindAllAnagrams(string arr[],int n)
34+
{
35+
for (int i =0; i < n; i++)
36+
for (int j = i+1; j < n; j++)
37+
if (areAnagram(arr[i], arr[j]))
38+
cout << arr[i] <<" is anagram of" << arr[j] << endl;
39+
}
40+
41+
42+
/* Driver program to test to pront printDups*/
43+
intmain()
44+
{
45+
string arr[] = {"geeksquiz","geeksforgeeks","abcd",
46+
"forgeeksgeeks","zuiqkeegs"};
47+
int n =sizeof(arr)/sizeof(arr[0]);
48+
findAllAnagrams(arr, n);
49+
return0;
50+
}

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp