Skip to content
Related Articles

Related Articles

Count distinct strings possible by replacing each character by its Morse code
  • Last Updated : 09 Nov, 2020

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: 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++

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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;
}

chevron_right


Java

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

chevron_right


Python3

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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

chevron_right


C#

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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

chevron_right


Output: 

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)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up
Recommended Articles
Page :