Given an array of strings arr[], the task is to count the number of distinct strings that can be generated from the given array by replacing each character of the strings by its Morse code. Below is the Morse code of all the lowercase alphabets:
Examples:
Input: arr[] = {“gig”, “zeg”, “gin”, “msn”}
Output: 2
Explanation:
Replacing each character of the strings of the given array to its Morse code:
gig = “–…–.”
zeg = “–…–.”
gin = “–…-.”
msn = “–…-.”
Morse code of the strings “gig” and “zeg” are equal.
Morse code of the strings “gin” and “msn” are equal.
Therefore, the total count of distinct elements of the given string by replacing the characters to its Morse code is equal to 2.
Input: arr[] = {“geeks”, “for”, “geeks”}
Output: 2
Approach 1: Follow the steps below to solve the problem:
- Initialize an array, say morseCode[] to store the Morse code of all lowercase characters.
- Create a set, say st to store distinct elements of the array by replacing each character to its Morse code.
- Traverse the array and insert the Morse code of the string of the array into st.
- Finally, print the count of elements present in st.
Below is the implementation of the above approach.
// C++ program to implement // the above approach #include <bits/stdc++.h> using namespace std;
// Function to count unique array elements // by replacing each character by its Morse code int uniqueMorseRep(vector<string>& arr)
{ // Stores Morse code of all
// lowercase characters
vector<string> morseCode
= {
".-" , "-..." , "-.-." ,
"-.." , "." , "..-." , "--." ,
"...." , ".." , ".---" , "-.-" ,
".-.." , "--" , "-." , "---" ,
".--." , "--.-" , ".-." , "..." ,
"-" , "..-" , "...-" , ".--" ,
"-..-" , "-.--" , "--.."
};
// Stores distinct elements of string by
// replacing each character by Morse code
set<string> st;
// Stores length of arr[] array
int N = arr.size();
// Traverse the array
for ( int i = 0; i < N; i++) {
// Stores the Morse code
// of arr[i]
string temp = "" ;
// Stores length of
// current string
int M = arr[i].length();
for ( int j = 0; j < M; j++) {
// Update temp
temp += morseCode[arr[i][j] - 'a' ];
}
// Insert temp into st
st.insert(temp);
}
// Return count of elements
// in the set
return st.size();
} // Driver code int main()
{ vector<string> arr = { "gig" , "zeg" ,
"gin" , "msn" };
cout << uniqueMorseRep(arr) << endl;
} |
// Java program to implement // the above approach import java.util.*;
class GFG{
// Function to count unique // array elements by replacing // each character by its Morse code static int uniqueMorseRep(String[] arr)
{ // Stores Morse code of all
// lowercase characters
String []morseCode = { ".-" , "-..." , "-.-." ,
"-.." , "." , "..-." , "--." ,
"...." , ".." , ".---" , "-.-" ,
".-.." , "--" , "-." , "---" ,
".--." , "--.-" , ".-." , "..." ,
"-" , "..-" , "...-" , ".--" ,
"-..-" , "-.--" , "--.." };
// Stores distinct elements of
// String by replacing each
// character by Morse code
HashSet<String> st = new HashSet<>();
// Stores length of arr[] array
int N = arr.length;
// Traverse the array
for ( int i = 0 ; i < N; i++)
{
// Stores the Morse code
// of arr[i]
String temp = "" ;
// Stores length of
// current String
int M = arr[i].length();
for ( int j = 0 ; j < M; j++)
{
// Update temp
temp += morseCode[arr[i].charAt(j) - 'a' ];
}
// Insert temp into st
st.add(temp);
}
// Return count of elements
// in the set
return st.size();
} // Driver code public static void main(String[] args)
{ String[] arr = { "gig" , "zeg" ,
"gin" , "msn" };
System.out.print(uniqueMorseRep(arr) + "\n" );
} } // This code is contributed by Amit Katiyar |
# Python3 program to implement # the above approach # Function to count unique # array elements by replacing # each character by its Morse # code def uniqueMorseRep(arr):
# Stores Morse code of
# all lowercase characters
morseCode = [ ".-" , "-..." , "-.-." ,
"-.." , "." , "..-." ,
"--." , "...." , ".." ,
".---" , "-.-" , ".-.." ,
"--" , "-." , "---" , ".--." ,
"--.-" , ".-." , "..." , "-" ,
"..-" , "...-" , ".--" , "-..-" ,
"-.--" , "--.." ];
# Stores distinct elements of
# String by replacing each
# character by Morse code
st = set ();
# Stores length of arr array
N = len (arr);
# Traverse the array
for i in range (N):
# Stores the Morse code
# of arr[i]
temp = "";
# Stores length of
# current String
M = len (arr[i]);
for j in range (M):
# Update temp
temp + = morseCode[ ord (arr[i][j]) -
ord ( 'a' )];
# Insert temp into st
st.add(temp);
# Return count of elements
# in the set
return len (st);
# Driver code if __name__ = = '__main__' :
arr = [ "gig" , "zeg" ,
"gin" , "msn" ];
print (uniqueMorseRep(arr) , "");
# This code is contributed by 29AjayKumar |
// C# program to implement // the above approach using System;
using System.Collections.Generic;
class GFG{
// Function to count unique // array elements by replacing // each character by its Morse code static int uniqueMorseRep(String[] arr)
{ // Stores Morse code of all
// lowercase characters
String []morseCode = { ".-" , "-..." , "-.-." ,
"-.." , "." , "..-." , "--." ,
"...." , ".." , ".---" , "-.-" ,
".-.." , "--" , "-." , "---" ,
".--." , "--.-" , ".-." , "..." ,
"-" , "..-" , "...-" , ".--" ,
"-..-" , "-.--" , "--.." };
// Stores distinct elements of
// String by replacing each
// character by Morse code
HashSet<String> st = new HashSet<String>();
// Stores length of []arr array
int N = arr.Length;
// Traverse the array
for ( int i = 0; i < N; i++)
{
// Stores the Morse code
// of arr[i]
String temp = "" ;
// Stores length of
// current String
int M = arr[i].Length;
for ( int j = 0; j < M; j++)
{
// Update temp
temp += morseCode[arr[i][j] - 'a' ];
}
// Insert temp into st
st.Add(temp);
}
// Return count of elements
// in the set
return st.Count;
} // Driver code public static void Main(String[] args)
{ String[] arr = { "gig" , "zeg" ,
"gin" , "msn" };
Console.Write(uniqueMorseRep(arr) + "\n" );
} } // This code is contributed by gauravrajput1 |
<script> // Javascript program to implement // the above approach // Function to count unique array elements // by replacing each character by its Morse code function uniqueMorseRep(arr)
{ // Stores Morse code of all
// lowercase characters
var morseCode
= [
".-" , "-..." , "-.-." ,
"-.." , "." , "..-." , "--." ,
"...." , ".." , ".---" , "-.-" ,
".-.." , "--" , "-." , "---" ,
".--." , "--.-" , ".-." , "..." ,
"-" , "..-" , "...-" , ".--" ,
"-..-" , "-.--" , "--.." ];
// Stores distinct elements of string by
// replacing each character by Morse code
var st = new Set();
// Stores length of arr[] array
var N = arr.length;
// Traverse the array
for ( var i = 0; i < N; i++) {
// Stores the Morse code
// of arr[i]
var temp = "" ;
// Stores length of
// current string
var M = arr[i].length;
for ( var j = 0; j < M; j++) {
// Update temp
temp += morseCode[arr[i][j].charCodeAt(0) - 'a' .charCodeAt(0)];
}
// Insert temp into st
st.add(temp);
}
// Return count of elements
// in the set
return st.size;
} // Driver code var arr = [ "gig" , "zeg" ,
"gin" , "msn" ];
document.write( uniqueMorseRep(arr)); </script> |
2
Time Complexity: O(N×M), where N is the size of the array and M is the length of a word.
Auxiliary Space: O(N)
Approach 2: Using python dictionary
Algorithm:
- Create a dictionary that map value of English alphabet to Morse.
- Use lambda function to loop through every character.
- Use Counter to count unique element after each word is converted to its corresponding morse.
// C++ code to implement the above approach #include <iostream> #include <string> #include <unordered_set> #include <vector> using namespace std;
// Function to calculate the number of unique morse code // representations int uniqueMorseRep(vector<string>& words)
{ // set to store unique representations
unordered_set<string> unique_morse_codes;
vector<string> morse_codes
= { ".-" , "-..." , "-.-." , "-.." , "." , "..-." ,
"--." , "...." , ".." , ".---" , "-.-" , ".-.." ,
"--" , "-." , "---" , ".--." , "--.-" , ".-." ,
"..." , "-" , "..-" , "...-" , ".--" , "-..-" ,
"-.--" , "--.." };
// iterate over each character in the word
for (string word : words) {
string morse_code = "" ;
for ( char c : word) {
morse_code += morse_codes;
}
// add the morse code representation of the word to
// the set
unique_morse_codes.insert(morse_code);
}
// return the size of the set, which is the number of
// unique representations
return unique_morse_codes.size();
} int main()
{ vector<string> words = { "gig" , "zeg" , "gin" , "msn" };
cout << uniqueMorseRep(words) << endl;
return 0;
} // Contributed by adityasha4x71 |
import java.util.*;
public class UniqueMorseCode {
// Function to calculate the number of unique morse code representations
public static int uniqueMorseRep(String[] words) {
// Set to store unique representations
Set<String> uniqueMorseCodes = new HashSet<>();
String[] morseCodes = { ".-" , "-..." , "-.-." , "-.." , "." , "..-." ,
"--." , "...." , ".." , ".---" , "-.-" , ".-.." ,
"--" , "-." , "---" , ".--." , "--.-" , ".-." , "..." ,
"-" , "..-" , "...-" , ".--" , "-..-" , "-.--" , "--.." };
// Iterate over each word in the array
for (String word : words) {
StringBuilder morseCode = new StringBuilder();
// Iterate over each character in the word
for ( char c : word.toCharArray()) {
morseCode.append(morseCodes);
}
// Add the morse code representation of the word to the set
uniqueMorseCodes.add(morseCode.toString());
}
// Return the size of the set, which is the
// number of unique representations
return uniqueMorseCodes.size();
}
public static void main(String[] args) {
String[] words = { "gig" , "zeg" , "gin" , "msn" };
System.out.println(uniqueMorseRep(words));
}
} |
from collections import Counter
def uniqueMorseRep(words):
MORSE = { 'a' : ".-" , 'b' : "-..." , 'c' : "-.-." , 'd' : "-.." , 'e' : "." , 'f' : "..-." , 'g' : "--." , 'h' : "...." , 'i' : ".." , 'j' : ".---" , 'k' : "-.-" , 'l' : ".-.." , 'm' : "--" , 'n' : "-." , 'o' : "---" , 'p' : ".--." , 'q' : "--.-" , 'r' : ".-." , 's' : "..." , 't' : "-" , 'u' : "..-" , 'v' : "...-" , 'w' : ".--" , 'x' : "-..-" , 'y' : "-.--" , 'z' : "--.." }
transform = lambda c: MORSE # use lambda function
return len (Counter("".join( map (transform, word)) for word in words))
# Driver code if __name__ = = '__main__' :
arr = [ "gig" , "zeg" , "gin" , "msn" ]
print (uniqueMorseRep(arr));
|
using System;
using System.Collections.Generic;
public class UniqueMorseCode
{ // Function to calculate the number of unique morse code representations
public static int UniqueMorseRep( string [] words)
{
// Set to store unique representations
HashSet< string > uniqueMorseCodes = new HashSet< string >();
string [] morseCodes = new string [] { ".-" , "-..." , "-.-." , "-.." , "." , "..-." ,
"--." , "...." , ".." , ".---" , "-.-" , ".-.." ,
"--" , "-." , "---" , ".--." , "--.-" , ".-." , "..." ,
"-" , "..-" , "...-" , ".--" , "-..-" , "-.--" , "--.." };
// Iterate over each word in the array
foreach ( string word in words)
{
System.Text.StringBuilder morseCode = new System.Text.StringBuilder();
// Iterate over each character in the word
foreach ( char c in word.ToCharArray())
{
morseCode.Append(morseCodes);
}
// Add the morse code representation of the word to the set
uniqueMorseCodes.Add(morseCode.ToString());
}
// Return the size of the set, which is the
// number of unique representations
return uniqueMorseCodes.Count;
}
public static void Main( string [] args)
{
string [] words = { "gig" , "zeg" , "gin" , "msn" };
Console.WriteLine(UniqueMorseRep(words));
}
} |
function uniqueMorseRep(words) {
const MORSE = { 'a' : ".-" , 'b' : "-..." , 'c' : "-.-." , 'd' : "-.." , 'e' : "." , 'f' : "..-." , 'g' : "--." , 'h' : "...." , 'i' : ".." , 'j' : ".---" , 'k' : "-.-" , 'l' : ".-.." , 'm' : "--" , 'n' : "-." , 'o' : "---" , 'p' : ".--." , 'q' : "--.-" , 'r' : ".-." , 's' : "..." , 't' : "-" , 'u' : "..-" , 'v' : "...-" , 'w' : ".--" , 'x' : "-..-" , 'y' : "-.--" , 'z' : "--.." };
const transform = (c) => MORSE; // use arrow function
const counter = new Map();
for (const word of words) {
const morseWord = Array.from(word, transform).join( '' );
counter.set(morseWord, (counter.get(morseWord) || 0) + 1);
}
return counter.size;
} // Driver code const arr = [ "gig" , "zeg" , "gin" , "msn" ];
console.log(uniqueMorseRep(arr)); //Contributed by Aditya Sharma |
2
Time Complexity: O(N), where N is the size of the array
Auxiliary Space: O(N)