Print all pairs of anagrams in a given array of strings
Last Updated :
17 Apr, 2023
Given an array of strings, find all anagram pairs in the given array.
Example:
Input: arr[] = {"geeksquiz", "geeksforgeeks", "abcd",
"forgeeksgeeks", "zuiqkeegs"};
Output: (geeksforgeeks, forgeeksgeeks), (geeksquiz, zuiqkeegs)
We can find whether two strings are anagram or not in linear time using count array (see method 2 of this).
One simple idea to find whether all anagram pairs is to run two nested loops. The outer loop picks all strings one by one. The inner loop checks whether remaining strings are anagram of the string picked by outer loop.
Below is the implementation of this approach :
C++
#include <bits/stdc++.h>
using namespace std;
#define NO_OF_CHARS 256
/* function to check whether two strings are anagram of each other */
bool areAnagram(string str1, string str2)
{
// Create two count arrays and initialize all values as 0
int count[NO_OF_CHARS] = {0};
int i;
// For each character in input strings, increment count in
// the corresponding count array
for (i = 0; str1[i] && str2[i]; i++)
{
count[str1[i]]++;
count[str2[i]]--;
}
// If both strings are of different length. Removing this condition
// will make the program fail for strings like "aaca" and "aca"
if (str1[i] || str2[i])
return false;
// See if there is any non-zero value in count array
for (i = 0; i < NO_OF_CHARS; i++)
if (count[i])
return false;
return true;
}
// This function prints all anagram pairs in a given array of strings
void findAllAnagrams(string arr[], int n)
{
for (int i = 0; i < n; i++)
for (int j = i+1; j < n; j++)
if (areAnagram(arr[i], arr[j]))
cout << arr[i] << " is anagram of " << arr[j] << endl;
}
/* Driver program to test to print printDups*/
int main()
{
string arr[] = {"geeksquiz", "geeksforgeeks", "abcd",
"forgeeksgeeks", "zuiqkeegs"};
int n = sizeof(arr)/sizeof(arr[0]);
findAllAnagrams(arr, n);
return 0;
}
Java
// Java program to Print all pairs of
// anagrams in a given array of strings
public class GFG
{
static final int NO_OF_CHARS = 256;
/* function to check whether two
strings are anagram of each other */
static boolean areAnagram(String str1, String str2)
{
// Create two count arrays and initialize
// all values as 0
int[] count = new int[NO_OF_CHARS];
int i;
// For each character in input strings,
// increment count in the corresponding
// count array
for (i = 0; i < str1.length() && i < str2.length();
i++)
{
count[str1.charAt(i)]++;
count[str2.charAt(i)]--;
}
// If both strings are of different length.
// Removing this condition will make the program
// fail for strings like "aaca" and "aca"
if (str1.length() != str2.length())
return false;
// See if there is any non-zero value in
// count array
for (i = 0; i < NO_OF_CHARS; i++)
if (count[i] != 0)
return false;
return true;
}
// This function prints all anagram pairs in a
// given array of strings
static void findAllAnagrams(String arr[], int n)
{
for (int i = 0; i < n; i++)
for (int j = i+1; j < n; j++)
if (areAnagram(arr[i], arr[j]))
System.out.println(arr[i] +
" is anagram of " + arr[j]);
}
/* Driver program to test to print printDups*/
public static void main(String args[])
{
String arr[] = {"geeksquiz", "geeksforgeeks",
"abcd", "forgeeksgeeks",
"zuiqkeegs"};
int n = arr.length;
findAllAnagrams(arr, n);
}
}
// This code is contributed by Sumit Ghosh
Python3
# Python3 program to find
# best meeting point in 2D array
NO_OF_CHARS = 256
# function to check whether two strings
# are anagram of each other
def areAnagram(str1: str, str2: str) -> bool:
# Create two count arrays and
# initialize all values as 0
count = [0] * NO_OF_CHARS
i = 0
# For each character in input strings,
# increment count in the corresponding
# count array
while i < len(str1) and i < len(str2):
count[ord(str1[i])] += 1
count[ord(str2[i])] -= 1
i += 1
# If both strings are of different length.
# Removing this condition will make the program
# fail for strings like "aaca" and "aca"
if len(str1) != len(str2):
return False
# See if there is any non-zero value
# in count array
for i in range(NO_OF_CHARS):
if count[i]:
return False
return True
# This function prints all anagram pairs
# in a given array of strings
def findAllAnagrams(arr: list, n: int):
for i in range(n):
for j in range(i + 1, n):
if areAnagram(arr[i], arr[j]):
print(arr[i], "is anagram of", arr[j])
# Driver Code
if __name__ == "__main__":
arr = ["geeksquiz", "geeksforgeeks",
"abcd", "forgeeksgeeks", "zuiqkeegs"]
n = len(arr)
findAllAnagrams(arr, n)
# This code is contributed by
# sanjeev2552
C#
// C# program to Print all pairs of
// anagrams in a given array of strings
using System;
class GFG
{
static int NO_OF_CHARS = 256;
/* function to check whether two
strings are anagram of each other */
static bool areAnagram(String str1, String str2)
{
// Create two count arrays and initialize
// all values as 0
int[] count = new int[NO_OF_CHARS];
int i;
// For each character in input strings,
// increment count in the corresponding
// count array
for (i = 0; i < str1.Length &&
i < str2.Length; i++)
{
count[str1[i]]++;
count[str2[i]]--;
}
// If both strings are of different length.
// Removing this condition will make the program
// fail for strings like "aaca" and "aca"
if (str1.Length != str2.Length)
return false;
// See if there is any non-zero value in
// count array
for (i = 0; i < NO_OF_CHARS; i++)
if (count[i] != 0)
return false;
return true;
}
// This function prints all anagram pairs in a
// given array of strings
static void findAllAnagrams(String []arr, int n)
{
for (int i = 0; i < n; i++)
for (int j = i+1; j < n; j++)
if (areAnagram(arr[i], arr[j]))
Console.WriteLine(arr[i] +
" is anagram of " + arr[j]);
}
/* Driver program to test to print printDups*/
public static void Main()
{
String []arr = {"geeksquiz", "geeksforgeeks",
"abcd", "forgeeksgeeks",
"zuiqkeegs"};
int n = arr.Length;
findAllAnagrams(arr, n);
}
}
// This code is contributed by nitin mittal
JavaScript
<script>
// JavaScript program to find
// best meeting point in 2D array
let NO_OF_CHARS = 256
// function to check whether two strings
// are anagram of each other
function areAnagram(str1, str2){
// Create two count arrays and
// initialize all values as 0
let count = [];
for(let i = 0;i< NO_OF_CHARS;i++)
count[i] = 0;
let i = 0;
// For each character in input strings,
// increment count in the corresponding
// count array
while(i < (str1).length && i < (str2).length){
count[ord(str1[i])] += 1;
count[ord(str2[i])] -= 1;
i += 1;
}
// If both strings are of different length.
// Removing this condition will make the program
// fail for strings like "aaca" and "aca"
if ( (str1).length != (str2).length)
return false;
// See if there is any non-zero value
// in count array
for(let i = 0; i < NO_OF_CHARS; i++){
if (count[i])
return false;
return True;
}
}
// This function prints all anagram pairs
// in a given array of strings
function findAllAnagrams(arr, n){
for(let i = 0; i < n; i++){
for(let j = i + 1; j < n; j++){
if areAnagram(arr[i], arr[j])
document.write(arr[i]+"is anagram of"+arr[j])
}
}
}
// Driver Code
let arr = ["geeksquiz", "geeksforgeeks",
"abcd", "forgeeksgeeks", "zuiqkeegs"];
let n = (arr).length;
findAllAnagrams(arr, n);
// This code is contributed by Rohitsingh07052.
</script>
Outputgeeksquiz is anagram of zuiqkeegs
geeksforgeeks is anagram of forgeeksgeeks
The time complexity of the above solution is O(n2*m) where n is number of strings and m is maximum length of a string.
Auxiliary Space: O(1) or O(256).
Optimizations:
We can optimize the above solution using following approaches.
1) Using sorting: We can sort array of strings so that all anagrams come together. Then print all anagrams by linearly traversing the sorted array. The time complexity of this solution is O(mnLogn) (We would be doing O(nLogn) comparisons in sorting and a comparison would take O(m) time)
Below is the implementation of this approach :
C++
#include <bits/stdc++.h>
using namespace std;
#define NO_OF_CHARS 256
/* function to check whether two strings are anagram of each other */
bool areAnagram(string str1, string str2)
{
// Create two count arrays and initialize all values as 0
int count[NO_OF_CHARS] = {0};
int i;
// For each character in input strings, increment count in
// the corresponding count array
for (i = 0; str1[i] && str2[i]; i++)
{
count[str1[i]]++;
count[str2[i]]--;
}
// If both strings are of different length. Removing this condition
// will make the program fail for strings like "aaca" and "aca"
if (str1[i] || str2[i])
return false;
// See if there is any non-zero value in count array
for (i = 0; i < NO_OF_CHARS; i++)
if (count[i])
return false;
return true;
}
// This function prints all anagram pairs in a given array of strings
void findAllAnagrams(string arr[], int n)
{
for (int i = 0; i < n-1; i++) {
for (int j = i+1; j < n; j++) {
// If the original strings are anagrams, print them
if (areAnagram(arr[i], arr[j]))
cout << arr[i] << " is anagram of " << arr[j] << endl;
}
}
}
/* Driver program to test to print printDups*/
int main()
{
string arr[] = {"geeksquiz", "geeksforgeeks", "abcd",
"forgeeksgeeks", "zuiqkeegs"};
int n = sizeof(arr)/sizeof(arr[0]);
findAllAnagrams(arr, n);
return 0;
}
Java
import java.util.Arrays;
public class Anagram {
final static int NO_OF_CHARS = 256;
/* function to check whether two strings are anagram of each other */
static boolean areAnagram(String str1, String str2) {
// Create two count arrays and initialize all values as 0
int[] count = new int[NO_OF_CHARS];
Arrays.fill(count, 0);
// For each character in input strings, increment count in
// the corresponding count array
for (int i = 0; i < str1.length() && i < str2.length(); i++) {
count[str1.charAt(i)]++;
count[str2.charAt(i)]--;
}
// If both strings are of different length. Removing this condition
// will make the program fail for strings like "aaca" and "aca"
if (str1.length() != str2.length())
return false;
// See if there is any non-zero value in count array
for (int i = 0; i < NO_OF_CHARS; i++)
if (count[i] != 0)
return false;
return true;
}
// This function prints all anagram pairs in a given array of strings
static void findAllAnagrams(String arr[], int n) {
for (int i = 0; i < n - 1; i++) {
for (int j = i + 1; j < n; j++) {
// If the original strings are anagrams, print them
if (areAnagram(arr[i], arr[j]))
System.out.println(arr[i] + " is anagram of " + arr[j]);
}
}
}
/* Driver program to test to print printDups */
public static void main(String[] args) {
String[] arr = { "geeksquiz", "geeksforgeeks", "abcd", "forgeeksgeeks", "zuiqkeegs" };
int n = arr.length;
findAllAnagrams(arr, n);
}
}
Python3
NO_OF_CHARS = 256
def areAnagram(str1, str2):
NO_OF_CHARS = 256
count = [0] * NO_OF_CHARS
# For each character in input strings, increment count in
# the corresponding count array
for i in range(max(len(str1), len(str2))):
if i < len(str1):
count[ord(str1[i])] += 1
if i < len(str2):
count[ord(str2[i])] -= 1
# See if there is any non-zero value in count array
for i in range(NO_OF_CHARS):
if count[i]:
return False
return True
def findAllAnagrams(arr, n):
for i in range(n-1):
for j in range(i+1, n):
# If the original strings are anagrams, print them
if areAnagram(arr[i], arr[j]):
print(arr[i], "is anagram of", arr[j])
# Driver code
if __name__ == "__main__":
arr = ["geeksquiz", "geeksforgeeks", "abcd", "forgeeksgeeks", "zuiqkeegs"]
n = len(arr)
findAllAnagrams(arr, n)
C#
using System;
public class Anagram
{
const int NO_OF_CHARS = 256;
/* function to check whether two strings are anagram of each other */
static bool AreAnagram(string str1, string str2)
{
// Create two count arrays and initialize all values as 0
int[] count = new int[NO_OF_CHARS];
Array.Fill(count, 0);
// For each character in input strings, increment count in
// the corresponding count array
for (int i = 0; i < str1.Length && i < str2.Length; i++)
{
count[str1[i]]++;
count[str2[i]]--;
}
// If both strings are of different length. Removing this condition
// will make the program fail for strings like "aaca" and "aca"
if (str1.Length != str2.Length)
return false;
// See if there is any non-zero value in count array
for (int i = 0; i < NO_OF_CHARS; i++)
if (count[i] != 0)
return false;
return true;
}
// This function prints all anagram pairs in a given array of strings
static void FindAllAnagrams(string[] arr, int n)
{
for (int i = 0; i < n - 1; i++)
{
for (int j = i + 1; j < n; j++)
{
// If the original strings are anagrams, print them
if (AreAnagram(arr[i], arr[j]))
Console.WriteLine(arr[i] + " is anagram of " + arr[j]);
}
}
}
/* Driver program to test to print printDups */
public static void Main(string[] args)
{
string[] arr = { "geeksquiz", "geeksforgeeks", "abcd", "forgeeksgeeks", "zuiqkeegs" };
int n = arr.Length;
FindAllAnagrams(arr, n);
}
}
JavaScript
const NO_OF_CHARS = 256;
/* function to check whether two strings are anagram of each other */
function areAnagram(str1, str2) {
// Create two count arrays and initialize all values as 0
const count = Array(NO_OF_CHARS).fill(0);
// For each character in input strings, increment count in
// the corresponding count array
for (let i = 0; i < str1.length && i < str2.length; i++) {
count[str1.charCodeAt(i)]++;
count[str2.charCodeAt(i)]--;
}
// If both strings are of different length. Removing this condition
// will make the program fail for strings like "aaca" and "aca"
if (str1.length != str2.length)
return false;
// See if there is any non-zero value in count array
for (let i = 0; i < NO_OF_CHARS; i++)
if (count[i])
return false;
return true;
}
// This function prints all anagram pairs in a given array of strings
function findAllAnagrams(arr) {
const n = arr.length;
for (let i = 0; i < n-1; i++) {
for (let j = i+1; j < n; j++) {
// If the original strings are anagrams, print them
if (areAnagram(arr[i], arr[j]))
console.log(`${arr[i]} is anagram of ${arr[j]}`);
}
}
}
/* Driver program to test to print printDups*/
const arr = ["geeksquiz", "geeksforgeeks", "abcd", "forgeeksgeeks", "zuiqkeegs"];
findAllAnagrams(arr);
Outputgeeksquiz is anagram of zuiqkeegs
geeksforgeeks is anagram of forgeeksgeeks
Space complexity :- O(1)
2) Using Hashing: We can build a hash function like XOR or sum of ASCII values of all characters for a string. Using such a hash function, we can build a hash table. While building the hash table, we can check if a value is already hashed. If yes, we can call areAnagrams() to check if two strings are actually anagrams (Note that xor or sum of ASCII values is not sufficient, see Kaushik Lele's comment here)
Similar Reads
Print all Unique Strings present in a given Array
Given an array of strings arr[], the task is to print all unique strings that are present in the given array. Examples: Input: arr[] = { "geeks", "geek", "ab", "geek" "code", "karega" } Output: geeks ab code karega Explanation: The frequency of the string "geeks" is 1. The frequency of the string "g
15+ min read
Print all subsets of a given Set or Array
Given an array arr of size n, your task is to print all the subsets of the array in lexicographical order.A subset is any selection of elements from an array, where the order does not matter, and no element appears more than once. It can include any number of elements, from none (the empty subset) t
12 min read
Count of Palindrome Strings in given Array of strings
Given an array of strings arr[] of size N where each string consists only of lowercase English letter. The task is to return the count of all palindromic string in the array. Examples: Input: arr[] = {"abc","car","ada","racecar","cool"}Output: 2Explanation: "ada" and "racecar" are the two palindrome
5 min read
Print all distinct strings from a given array
Given an array of strings arr[] of size N, the task is to print all the distinct strings present in the given array. Examples: Input: arr[] = { "Geeks", "For", "Geeks", "Code", "Coder" } Output: Coder Code Geeks For Explanation: Since all the strings in the array are distinct, the required output is
5 min read
Count substrings of a given string whose anagram is a palindrome
Given a string S of length N containing only lowercase alphabets, the task is to print the count of substrings of the given string whose anagram is palindromic. Examples: Input: S = "aaaa"Output: 10Explanation:Possible substrings are {"a", "a", "a", "a", "aa", "aa", "aa", "aaa", "aaa", "aaaa"}. Sinc
10 min read
Count of Superstrings in a given array of strings
Given 2 array of strings X and Y, the task is to find the number of superstrings in X. A string s is said to be a Superstring, if each string present in array Y is a subsequence of string s . Examples: Input: X = {"ceo", "alco", "caaeio", "ceai"}, Y = {"ec", "oc", "ceo"}Output: 2Explanation: Strings
8 min read
Palindrome pair in an array of words (or strings)
Given an array of strings arr[] of size n, the task is to find if there exists two strings arr[i] and arr[j] such that arr[i]+arr[j] is a palindrome i.e the concatenation of string arr[i] and arr[j] results into a palindrome.Examples:Â Input: arr[] = ["geekf", "geeks", "or", "keeg", "abc", "bc"]Outpu
15+ min read
Removing string that is an anagram of an earlier string
Given an array arr of strings, the task is to remove the strings that are an anagram of an earlier string, then print the remaining array in sorted order. Examples: Input: arr[] = { "geeks", "keegs", "code", "doce" }, N = 4 Output: ["code", "geeks"] Explanation: "geeks" and "keegs" are anagrams, so
7 min read
Find all substrings that are anagrams of another substring of the string S
Given a string S, the task is to find all the substrings in the string S which is an anagram of another different substring in the string S. The different substrings mean the substring starts at a different index. Examples: Input: S = "aba"Output: a a ab baExplanation:Following substrings are anagra
6 min read
Print all permutations of a string in Java
Given a string str, the task is to print all the permutations of str. A permutation is an arrangement of all or part of a set of objects, with regard to the order of the arrangement. For instance, the words âbatâ and âtabâ represents two distinct permutation (or arrangements) of a similar three lett
3 min read