Count distinct strings possible by replacing each character by its Morse code

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


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


Budding Web DeveloperKeen learnerAverage CoderDancer&Social; Activist

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.