Print all pairs of anagrams in a given array of strings

Here given code implementation process.

// Include header file
#include <iostream>
#include <string>
#include <algorithm>

using namespace std;
/*
    C++ program for
    Print all pairs of anagrams in a given array of strings
*/
class Anagrams
{
	public: void anagramsPairs(string arr[], int n)
	{
		if (n <= 0)
		{
			return;
		}
		string record[n];
		string word = "";
		// Sort array string and put into record
		for (int i = 0; i < n; ++i)
		{
			word = arr[i];
			// Sort word
			sort(word.begin(), word.end());
			record[i] = word;
		}
		// Display relative pairs of anagrams
		for (int i = 0; i < n; ++i)
		{
			for (int j = i + 1; j < n; ++j)
			{
				if (record[i].compare(record[j]) == 0)
				{
					cout << "(" << arr[i] << "," << arr[j] << ")\n";
				}
			}
		}
	}
};
int main()
{
	Anagrams *task = new Anagrams();
	string arr[] = {
		"mango" , "among" , "retace" , "power" , "create" , "receat"
	};
	// Get the length of array
	int n = sizeof(arr) / sizeof(arr[0]);
	task->anagramsPairs(arr, n);
	return 0;
}

Output

(mango,among)
(retace,create)
(retace,receat)
(create,receat)
import java.util.Arrays;
/*
    Java program for
    Print all pairs of anagrams in a given array of strings
*/
public class Anagrams
{
	public void anagramsPairs(String[] arr, int n)
	{
		if (n <= 0)
		{
			return;
		}
		String[] record = new String[n];
		String auxiliary = "";
		// Sort array string and put into record
		for (int i = 0; i < n; ++i)
		{
			char[] word = arr[i].toCharArray();
			// Sort word
			Arrays.sort(word);
			// Convert into String 
			auxiliary = new String(word);
			record[i] = auxiliary;
		}
		// Display relative pairs of anagrams
		for (int i = 0; i < n; ++i)
		{
			for (int j = i + 1; j < n; ++j)
			{
				if (record[i].equals(record[j]))
				{
					System.out.print("(" + arr[i] + "," + arr[j] + ")\n");
				}
			}
		}
	}
	public static void main(String[] args)
	{
		Anagrams task = new Anagrams();
		String[] arr = {
			"mango" , "among" , "retace" , "power" , "create" , "receat"
		};
		// Get the length of array
		int n = arr.length;
		task.anagramsPairs(arr, n);
	}
}

Output

(mango,among)
(retace,create)
(retace,receat)
(create,receat)
// Include namespace system
using System;
/*
    Csharp program for
    Print all pairs of anagrams in a given array of strings
*/
public class Anagrams
{
	public void anagramsPairs(String[] arr, int n)
	{
		if (n <= 0)
		{
			return;
		}
		String[] record = new String[n];
		String auxiliary = "";
		// Sort array string and put into record
		for (int i = 0; i < n; ++i)
		{
			char[] word = arr[i].ToCharArray();
			// Sort word
			Array.Sort(word);
			// Convert into String 
			auxiliary = new String(word);
			record[i] = auxiliary;
		}
		// Display relative pairs of anagrams
		for (int i = 0; i < n; ++i)
		{
			for (int j = i + 1; j < n; ++j)
			{
				if (record[i].Equals(record[j]))
				{
					Console.Write("(" + arr[i] + "," + arr[j] + ")\n");
				}
			}
		}
	}
	public static void Main(String[] args)
	{
		Anagrams task = new Anagrams();
		String[] arr = {
			"mango" , "among" , "retace" , "power" , "create" , "receat"
		};
		// Get the length of array
		int n = arr.Length;
		task.anagramsPairs(arr, n);
	}
}

Output

(mango,among)
(retace,create)
(retace,receat)
(create,receat)
package main
import "strings"
import "sort"
import "fmt"
/*
    Go program for
    Print all pairs of anagrams in a given array of strings
*/
func anagramsPairs(arr[] string, n int) {
	if n <= 0 {
		return
	}
	var record = make([]string,n);
	var auxiliary string = ""
	// Sort array string and put into record
	for i := 0 ; i < n ; i++ {

		s := strings.Split(arr[i], "")
    	sort.Strings(s)
    	auxiliary = strings.Join(s, "")
		record[i] = auxiliary
	}
	// Display relative pairs of anagrams
	for i := 0 ; i < n ; i++ {
		for j := i + 1 ; j < n ; j++ {
			if record[i] == record[j] {
				fmt.Print("(", arr[i], ",", arr[j], ")\n")
			}
		}
	}
}
func main() {
	
	var arr = [] string {"mango" , "among" , "retace" , "power" , "create" , "receat"}
	// Get the length of array
	var n int = len(arr)
	anagramsPairs(arr, n)
}

Output

(mango,among)
(retace,create)
(retace,receat)
(create,receat)
<?php
/*
    Php program for
    Print all pairs of anagrams in a given array of strings
*/
class Anagrams
{
	public	function anagramsPairs($arr, $n)
	{
		if ($n <= 0)
		{
			return;
		}
		$record = array_fill(0, $n, "");
		$auxiliary = "";
		// Sort array string and put into record
		for ($i = 0; $i < $n; ++$i)
		{
			$word = str_split($arr[$i]);
			// Sort word
			sort($word);
			// Convert into String 
			$auxiliary = implode($word);
			$record[$i] = $auxiliary;
		}
		// Display relative pairs of anagrams
		for ($i = 0; $i < $n; ++$i)
		{
			for ($j = $i + 1; $j < $n; ++$j)
			{
				if ((strcmp($record[$i], $record[$j]) == 0))
				{
					echo("(".$arr[$i].",".$arr[$j].")\n");
				}
			}
		}
	}
}

function main()
{
	$task = new Anagrams();
	$arr = array("mango", "among", "retace", "power", "create", "receat");
	// Get the length of array
	$n = count($arr);
	$task->anagramsPairs($arr, $n);
}
main();

Output

(mango,among)
(retace,create)
(retace,receat)
(create,receat)
/*
    Node JS program for
    Print all pairs of anagrams in a given array of strings
*/
class Anagrams
{
	anagramsPairs(arr, n)
	{
		if (n <= 0)
		{
			return;
		}
		var record = Array(n).fill("");
		var auxiliary = "";
		// Sort array string and put into record
		for (var i = 0; i < n; ++i)
		{
			var word = (arr[i]).split('');
			// Sort word
			word.sort();
			// Convert into String 
			auxiliary = new String(word);
			record[i] = auxiliary;
		}
		// Display relative pairs of anagrams
		for (var i = 0; i < n; ++i)
		{
			for (var j = i + 1; j < n; ++j)
			{
				if ((record[i].localeCompare(record[j]) == 0))
				{
					process.stdout.write("(" + arr[i] + "," + arr[j] + ")\n");
				}
			}
		}
	}
}

function main()
{
	var task = new Anagrams();
	var arr = ["mango", "among", "retace", "power", "create", "receat"];
	// Get the length of array
	var n = arr.length;
	task.anagramsPairs(arr, n);
}
main();

Output

(mango,among)
(retace,create)
(retace,receat)
(create,receat)
#    Python 3 program for
#    Print all pairs of anagrams in a given array of strings
class Anagrams :
	def anagramsPairs(self, arr, n) :
		if (n <= 0) :
			return
		
		record = [""] * (n)
		i = 0
		#  Sort list string and put into record
		while (i < n) :
			word = ''.join(sorted(arr[i]))
			record[i] = word
			i += 1
		
		i = 0
		#  Display relative pairs of anagrams
		while (i < n) :
			j = i + 1
			while (j < n) :
				if (record[i] == record[j]) :
					print("(", arr[i] ,",", arr[j] ,")")
				
				j += 1
			
			i += 1
		
	

def main() :
	task = Anagrams()
	arr = ["mango", "among", "retace", "power", "create", "receat"]
	#  Get the length of list
	n = len(arr)
	task.anagramsPairs(arr, n)

if __name__ == "__main__": main()

Output

( mango , among )
( retace , create )
( retace , receat )
( create , receat )
#    Ruby program for
#    Print all pairs of anagrams in a given array of strings
class Anagrams 
	def anagramsPairs(arr, n) 
		if (n <= 0) 
			return
		end

		record = Array.new(n) {nil}
		auxiliary = ""
		i = 0
		#  Sort array string and put into record
		while (i < n) 
			word = arr[i]
			auxiliary = word.chars.sort(&:casecmp).join
			record[i] = auxiliary
			i += 1
		end

		i = 0
		#  Display relative pairs of anagrams
		while (i < n) 
			j = i + 1
			while (j < n) 
				if (record[i] === record[j]) 
					print("(", arr[i] ,",", arr[j] ,")\n")
				end
				j += 1
			end
			i += 1
		end
	end
end

def main() 
	task = Anagrams.new()
	arr = ["mango", "among", "retace", "power", "create", "receat"]
	#  Get the length of array
	n = arr.length
	task.anagramsPairs(arr, n)
end

main()

Output

(mango,among)
(retace,create)
(retace,receat)
(create,receat)
/*
    Scala program for
    Print all pairs of anagrams in a given array of strings
*/
class Anagrams()
{
	def anagramsPairs(arr: Array[String], n: Int): Unit = {
		if (n <= 0)
		{
			return;
		}
		var record: Array[String] = Array.fill[String](n)("");
		var auxiliary: String = "";
		var i: Int = 0;
		// Sort array string and put into record
		while (i < n)
		{
			var word: Array[Char] = arr(i).toCharArray();
			// Sort word
			word = word.sorted;
			// Convert into String
			auxiliary = new String(word);
			record(i) = auxiliary;
			i += 1;
		}
		i = 0;
		// Display relative pairs of anagrams
		while (i < n)
		{
			var j: Int = i + 1;
			while (j < n)
			{
				if (record(i).equals(record(j)))
				{
					print("(" + arr(i) + "," + arr(j) + ")\n");
				}
				j += 1;
			}
			i += 1;
		}
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var task: Anagrams = new Anagrams();
		var arr: Array[String] = Array(
          "mango", "among", "retace", "power", "create", "receat");
		// Get the length of array
		var n: Int = arr.length;
		task.anagramsPairs(arr, n);
	}
}

Output

(mango,among)
(retace,create)
(retace,receat)
(create,receat)
import Foundation;
/*
    Swift 4 program for
    Print all pairs of anagrams in a given array of strings
*/
class Anagrams
{
	func anagramsPairs(_ arr: [String], _ n: Int)
	{
		if (n <= 0)
		{
			return;
		}
		var record: [String] = Array(repeating: "", count: n);
		var auxiliary: String = "";
		var i: Int = 0;
		// Sort array string and put into record
		while (i < n)
		{
			// Convert into String
			auxiliary = String(arr[i].sorted());
			record[i] = auxiliary;
			i += 1;
		}
		i = 0;
		// Display relative pairs of anagrams
		while (i < n)
		{
			var j: Int = i + 1;
			while (j < n)
			{
				if (record[i] == record[j])
				{
					print("(", arr[i] ,",", arr[j] ,")");
				}
				j += 1;
			}
			i += 1;
		}
	}
}
func main()
{
	let task: Anagrams = Anagrams();
	let arr: [String] = [
      "mango", "among", "retace", "power", "create", "receat"];
	// Get the length of array
	let n: Int = arr.count;
	task.anagramsPairs(arr, n);
}
main();

Output

( mango , among )
( retace , create )
( retace , receat )
( create , receat )
/*
    Kotlin program for
    Print all pairs of anagrams in a given array of strings
*/
class Anagrams
{
	fun anagramsPairs(arr: Array < String > , n: Int): Unit
	{
		if (n <= 0)
		{
			return;
		}
		val record: Array < String > = Array(n)
		{
			""
		};
		var auxiliary: String ;
		var i: Int = 0;
		// Sort array string and put into record
		while (i < n)
		{
			var word = arr[i].toCharArray();
          	// Get sorted word
			auxiliary = word.sorted().joinToString("");
			record[i] = auxiliary;
			i += 1;
		}
		i = 0;
		// Display relative pairs of anagrams
		while (i < n)
		{
			var j: Int = i + 1;
			while (j < n)
			{
				if (record[i].equals(record[j]))
				{
					print("(" + arr[i] + "," + arr[j] + ")\n");
				}
				j += 1;
			}
			i += 1;
		}
	}
}
fun main(args: Array < String > ): Unit
{
	val task: Anagrams = Anagrams();
	val arr: Array < String > = arrayOf(
      "mango", "among", "retace", "power", "create", "receat");
	// Get the length of array
	val n: Int = arr.count();
	task.anagramsPairs(arr, n);
}

Output

(mango,among)
(retace,create)
(retace,receat)
(create,receat)


Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

New Comment







© 2021, kalkicode.com, All rights reserved