Count of anagrams of each string in between two arrays
Here given code implementation process.
// Java Program
// Count of anagrams of each string in between two arrays
import java.util.Arrays;
import java.util.HashMap;
public class Anagrams
{
public void findAnagrams(String[] first, String[] second, int n, int m)
{
// use to collect word frequency
HashMap < String, Integer > record = new HashMap < String, Integer > ();
// Define some auxiliary variable
int i = 0;
String auxiliary = "";
// Count frequency of second array by using sorting word
for (i = 0; i < m; i++)
{
char[] word = second[i].toCharArray();
// Sort word
Arrays.sort(word);
// Convert into String
auxiliary = new String(word);
// Get sorted word
if (record.containsKey(auxiliary))
{
// When element exist
// Increase the element frequency
record.put(auxiliary, record.get(auxiliary) + 1);
}
else
{
record.put(auxiliary, 1);
}
}
// Display result
for (i = 0; i < n; i++)
{
char[] word = first[i].toCharArray();
// Sort word
Arrays.sort(word);
// Convert into String
auxiliary = new String(word);
// Get sorted word
if (record.containsKey(auxiliary))
{
// Display number of anagram frequency
System.out.print(" " + record.get(auxiliary));
}
else
{
// When word not exist in second array
System.out.print(" 0");
}
}
}
// Handles the request to find anagrams of given two arrays
public void countAnagram(String[] first, String[] second, int n, int m)
{
if (n > m)
{
// When length of second array is small
findAnagrams(second, first, m, n);
}
else
{
// When length of first array is small
findAnagrams(first, second, n, m);
}
}
public static void main(String[] args)
{
Anagrams task = new Anagrams();
String[] first = {
"ffo" , "off" , "goal" , "doce" , "gola", "algo" , "xyz"
};
String[] second = {
"algo" , "ok", "code", "off" , "fill"
};
int n = first.length;
int m = second.length;
task.countAnagram(first, second, n, m);
}
}
Output
3 0 1 2 0
// Include header file
#include <iostream>
#include <string>
#include <unordered_map>
#include <algorithm>
using namespace std;
// C++ Program
// Count of anagrams of each string in between two arrays
class Anagrams
{
public: void findAnagrams(string first[], string second[], int n, int m)
{
// use to collect word frequency
unordered_map < string, int > record;
// Define some auxiliary variable
int i = 0;
string word = "";
// Count frequency of second array by using sorting word
for (i = 0; i < m; i++)
{
word = second[i];
// Sort word
sort(word.begin(), word.end());
// Get sorted word
if (record.find(word) != record.end())
{
// When element exist
// Increase the element frequency
record[word] = record[word] + 1;
}
else
{
record[word] = 1;
}
}
// Display result
for (i = 0; i < n; i++)
{
word = first[i];
// Sort word
sort(word.begin(), word.end());
// Get sorted word
if (record.find(word) != record.end())
{
// Display number of anagram frequency
cout << " " << record[word];
}
else
{
// When word not exist in second array
cout << " 0";
}
}
}
// Handles the request to find anagrams of given two arrays
void countAnagram(string first[], string second[], int n, int m)
{
if (n > m)
{
// When length of second array is small
this->findAnagrams(second, first, m, n);
}
else
{
// When length of first array is small
this->findAnagrams(first, second, n, m);
}
}
};
int main()
{
Anagrams task = Anagrams();
string first[] = {
"ffo" , "off" , "goal" , "doce" , "gola" , "algo" , "xyz"
};
string second[] = {
"algo" , "ok" , "code" , "off" , "fill"
};
int n = sizeof(first) / sizeof(first[0]);
int m = sizeof(second) / sizeof(second[0]);
task.countAnagram(first, second, n, m);
return 0;
}
Output
3 0 1 2 0
// Include namespace system
using System;
using System.Collections.Generic;
// C# Program
// Count of anagrams of each string in between two arrays
public class Anagrams
{
public void findAnagrams(String[] first, String[] second, int n, int m)
{
// use to collect word frequency
Dictionary < String, int > record =
new Dictionary < String, int > ();
// Define some auxiliary variable
int i = 0;
String auxiliary = "";
// Count frequency of second array by using sorting word
for (i = 0; i < m; i++)
{
char[] word = second[i].ToCharArray();
// Sort word
Array.Sort(word);
// Convert into String
auxiliary = new String(word);
// Get sorted word
if (record.ContainsKey(auxiliary))
{
// When element exist
// Increase the element frequency
record[auxiliary] = record[auxiliary] + 1;
}
else
{
record.Add(auxiliary, 1);
}
}
// Display result
for (i = 0; i < n; i++)
{
char[] word = first[i].ToCharArray();
// Sort word
Array.Sort(word);
// Convert into String
auxiliary = new String(word);
// Get sorted word
if (record.ContainsKey(auxiliary))
{
// Display number of anagram frequency
Console.Write(" " + record[auxiliary]);
}
else
{
// When word not exist in second array
Console.Write(" 0");
}
}
}
// Handles the request to find anagrams of given two arrays
public void countAnagram(String[] first, String[] second, int n, int m)
{
if (n > m)
{
// When length of second array is small
findAnagrams(second, first, m, n);
}
else
{
// When length of first array is small
findAnagrams(first, second, n, m);
}
}
public static void Main(String[] args)
{
Anagrams task = new Anagrams();
String[] first = {
"ffo" , "off" , "goal" , "doce" , "gola" , "algo" , "xyz"
};
String[] second = {
"algo" , "ok" , "code" , "off" , "fill"
};
int n = first.Length;
int m = second.Length;
task.countAnagram(first, second, n, m);
}
}
Output
3 0 1 2 0
<?php
// Php Program
// Count of anagrams of each string in between two arrays
class Anagrams
{
public
function findAnagrams($first, $second, $n, $m)
{
// use to collect word frequency
$record = array();
// Define some auxiliary variable
$i = 0;
$auxiliary = "";
// Count frequency of second array by using sorting word
for ($i = 0; $i < $m; $i++)
{ // convert into array
$auxiliary = str_split($second[$i]);
// sort $auxiliary text
sort($auxiliary);
// Convert into String
$word = implode($auxiliary);
// Get sorted word
if (array_key_exists($word, $record))
{ // When element exist
// Increase the element frequency
$record[$word] = $record[$word] + 1;
}
else
{
$record[$word] = 1;
}
}
// Display result
for ($i = 0; $i < $n; $i++)
{
$auxiliary = str_split($first[$i]);
sort($auxiliary);
// Convert into String
$word = implode($auxiliary);
// Get sorted word
if (array_key_exists($word, $record))
{
// Display number of anagram frequency
echo " ".$record[$word];
}
else
{
// When word not exist in second array
echo " 0";
}
}
}
// Handles the request to find anagrams of given two arrays
public
function countAnagram( & $first, & $second, $n, $m)
{
if ($n > $m)
{
// When length of second array is small
$this->findAnagrams($second, $first, $m, $n);
}
else
{
// When length of first array is small
$this->findAnagrams($first, $second, $n, $m);
}
}
}
function main()
{
$task = new Anagrams();
$first = array("ffo", "off", "goal", "doce", "gola", "algo", "xyz");
$second = array("algo", "ok", "code", "off", "fill");
$n = count($first);
$m = count($second);
$task->countAnagram($first, $second, $n, $m);
}
main();
Output
3 0 1 2 0
// Node Js Program
// Count of anagrams of each string in between two arrays
class Anagrams
{
findAnagrams(first, second, n, m)
{
// use to collect word frequency
var record = new Map();
// Define some auxiliary variable
var i = 0;
var auxiliary = "";
// Count frequency of second array by using sorting word
for (i = 0; i < m; i++)
{
// Sort word
auxiliary = second[i].split('').sort().join('');
// Get sorted word
if (record.has(auxiliary))
{
// When element exist
// Increase the element frequency
record.set(auxiliary, record.get(auxiliary) + 1);
}
else
{
record.set(auxiliary, 1);
}
}
// Display result
for (i = 0; i < n; i++)
{
// Sort word
auxiliary = first[i].split('').sort().join('');
// Get sorted word
if (record.has(auxiliary))
{
// Display number of anagram frequency
process.stdout.write(" " + record.get(auxiliary));
}
else
{
// When word not exist in second array
process.stdout.write(" 0");
}
}
}
// Handles the request to find anagrams of given two arrays
countAnagram(first, second, n, m)
{
if (n > m)
{
// When length of second array is small
this.findAnagrams(second, first, m, n);
}
else
{
// When length of first array is small
this.findAnagrams(first, second, n, m);
}
}
}
function main()
{
var task = new Anagrams();
var first = ["ffo", "off", "goal", "doce", "gola", "algo", "xyz"];
var second = ["algo", "ok", "code", "off", "fill"];
var n = first.length;
var m = second.length;
task.countAnagram(first, second, n, m);
}
main();
Output
3 0 1 2 0
# Python 3 Program
# Count of anagrams of each string in between two arrays
class Anagrams :
def findAnagrams(self, first, second, n, m) :
# use to collect word frequency
record = dict()
# Define some auxiliary variable
i = 0
auxiliary = ""
# Count frequency of second list by using sorting word
while (i < m) :
# Sort word
auxiliary = ''.join(sorted(second[i]))
# Get sorted word
if (auxiliary in record.keys()) :
# When element exist
# Increase the element frequency
record[auxiliary] = record.get(auxiliary) + 1
else :
record[auxiliary] = 1
i += 1
i = 0
# Display result
while (i < n) :
# Sort word
auxiliary = ''.join(sorted(first[i]))
# Get sorted word
if (auxiliary in record.keys()) :
# Display number of anagram frequency
print(" ", record.get(auxiliary), end = "")
else :
# When word not exist in second list
print(" 0", end = "")
i += 1
# Handles the request to find anagrams of given two lists
def countAnagram(self, first, second, n, m) :
if (n > m) :
# When length of second list is small
self.findAnagrams(second, first, m, n)
else :
# When length of first list is small
self.findAnagrams(first, second, n, m)
def main() :
task = Anagrams()
first = ["ffo", "off", "goal", "doce", "gola", "algo", "xyz"]
second = ["algo", "ok", "code", "off", "fill"]
n = len(first)
m = len(second)
task.countAnagram(first, second, n, m)
if __name__ == "__main__": main()
Output
3 0 1 2 0
# Ruby Program
# Count of anagrams of each string in between two arrays
class Anagrams
def findAnagrams(first, second, n, m)
# use to collect word frequency
record = Hash.new
# Define some auxiliary variable
i = 0
auxiliary = ""
# Count frequency of second array by using sorting word
while (i < m)
word = second[i]
auxiliary = word.chars.sort(&:casecmp).join
# Get sorted word
if (record.key?(auxiliary))
record[auxiliary] = record[auxiliary] + 1
else
record[auxiliary] = 1
end
i += 1
end
i = 0
# Display result
while (i < n)
word = first[i]
# Convert into String
auxiliary = word.chars.sort(&:casecmp).join
# Get sorted word
if (record.key?(auxiliary))
# Display number of anagram frequency
print(" ", record[auxiliary])
else
# When word not exist in second array
print(" 0")
end
i += 1
end
end
# Handles the request to find anagrams of given two arrays
def countAnagram(first, second, n, m)
if (n > m)
# When length of second array is small
self.findAnagrams(second, first, m, n)
else
# When length of first array is small
self.findAnagrams(first, second, n, m)
end
end
end
def main()
task = Anagrams.new()
first = ["ffo", "off", "goal", "doce", "gola", "algo", "xyz"]
second = ["algo", "ok", "code", "off", "fill"]
n = first.length
m = second.length
task.countAnagram(first, second, n, m)
end
main()
Output
3 0 1 2 0
import scala.collection.mutable._;
// Scala Program
// Count of anagrams of each string in between two arrays
class Anagrams
{
def findAnagrams(first: Array[String],
second: Array[String], n: Int, m: Int): Unit = {
// use to collect word frequency
var record = Map[String, Int]();
// Define some auxiliary variable
var i: Int = 0;
var auxiliary: String = "";
// Count frequency of second array by using sorting word
while (i < m)
{
var word: Array[Char] = second(i).toCharArray();
// Sort word
word = word.sorted;
// Convert into String
auxiliary = new String(word);
// Get sorted word
if (record.contains(auxiliary))
{
// When element exist
// Increase the element frequency
record.addOne(auxiliary, record.get(auxiliary).get + 1);
}
else
{
record.addOne(auxiliary, 1);
}
i += 1;
}
i = 0;
// Display result
while (i < n)
{
var word: Array[Char] = first(i).toCharArray();
// Sort word
word = word.sorted;
// Convert into String
auxiliary = new String(word);
// Get sorted word
if (record.contains(auxiliary))
{
// Display number of anagram frequency
print(" " + record.get(auxiliary).get);
}
else
{
// When word not exist in second array
print(" 0");
}
i += 1;
}
}
// Handles the request to find anagrams of given two arrays
def countAnagram(first: Array[String],
second: Array[String], n: Int, m: Int): Unit = {
if (n > m)
{
// When length of second array is small
this.findAnagrams(second, first, m, n);
}
else
{
// When length of first array is small
this.findAnagrams(first, second, n, m);
}
}
}
object Main
{
def main(args: Array[String]): Unit = {
var task: Anagrams = new Anagrams();
var first: Array[String] =
Array("ffo", "off", "goal", "doce", "gola", "algo", "xyz");
var second: Array[String] =
Array("algo", "ok", "code", "off", "fill");
var n: Int = first.length;
var m: Int = second.length;
task.countAnagram(first, second, n, m);
}
}
Output
3 0 1 2 0
import Foundation
// Swift 4 Program
// Count of anagrams of each string in between two arrays
class Anagrams
{
func findAnagrams(_ first: [String],
_ second: [String], _ n: Int, _ m: Int)
{
// use to collect word frequency
var record = [String: Int]();
// Define some auxiliary variable
var i: Int = 0;
var auxiliary: String = "";
// Count frequency of second array by using sorting word
while (i < m)
{
let word = second[i];
// Convert into String
auxiliary = String(word.sorted());
// Get sorted word
if (record.keys.contains(auxiliary))
{
// When element exist
// Increase the element frequency
record[auxiliary] = record[auxiliary]! + 1;
}
else
{
record[auxiliary] = 1;
}
i += 1;
}
i = 0;
// Display result
while (i < n)
{
let word = first[i];
// Convert into String
auxiliary = String(word.sorted());
// Get sorted word
if (record.keys.contains(auxiliary))
{
// Display number of anagram frequency
print(" ", record[auxiliary]!, terminator: "");
}
else
{
// When word not exist in second array
print(" 0", terminator: "");
}
i += 1;
}
}
// Handles the request to find anagrams of given two arrays
func countAnagram(_ first: [String],
_ second: [String], _ n: Int, _ m: Int)
{
if (n > m)
{
// When length of second array is small
self.findAnagrams(second, first, m, n);
}
else
{
// When length of first array is small
self.findAnagrams(first, second, n, m);
}
}
}
func main()
{
let task: Anagrams = Anagrams();
let first: [String] = ["ffo", "off", "goal", "doce", "gola", "algo", "xyz"];
let second: [String] = ["algo", "ok", "code", "off", "fill"];
let n: Int = first.count;
let m: Int = second.count;
task.countAnagram(first, second, n, m);
}
main();
Output
3 0 1 2 0
// Kotlin Program
// Count of anagrams of each string in between two arrays
class Anagrams
{
fun findAnagrams(first: Array < String > ,
second: Array < String > , n: Int, m: Int): Unit
{
// use to collect word frequency
var record = mutableMapOf<String, Int>();
// Define some auxiliary variable
var i: Int = 0;
var auxiliary: String ;
// Count frequency of second array by using sorting word
while (i < m)
{
var word = second[i].toCharArray();
// Get sorted word
auxiliary = word.sorted().joinToString("");
if (record.containsKey(auxiliary))
{
// When element exist
// Increase the element frequency
record.put(auxiliary, record.getValue(auxiliary) + 1);
}
else
{
record.put(auxiliary, 1);
}
i += 1;
}
i = 0;
// Display result
while (i < n)
{
var word = first[i].toCharArray();
// Get sorted word
auxiliary = word.sorted().joinToString("");
if (record.containsKey(auxiliary))
{
// Display number of anagram frequency
print(" " + record.getValue(auxiliary));
}
else
{
// When word not exist in second array
print(" 0");
}
i += 1;
}
}
// Handles the request to find anagrams of given two arrays
fun countAnagram(first: Array < String > ,
second: Array < String > , n: Int, m: Int): Unit
{
if (n > m)
{
// When length of second array is small
this.findAnagrams(second, first, m, n);
}
else
{
// When length of first array is small
this.findAnagrams(first, second, n, m);
}
}
}
fun main(args: Array < String > ): Unit
{
var task: Anagrams = Anagrams();
var first: Array < String > =
arrayOf("ffo", "off", "goal", "doce", "gola", "algo", "xyz");
var second: Array < String > =
arrayOf("algo", "ok", "code", "off", "fill");
var n: Int = first.count();
var m: Int = second.count();
task.countAnagram(first, second, n, m);
}
Output
3 0 1 2 0

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