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++
// 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; } |
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.
Recommended Posts:
- Morse Code Implementation
- Count of distinct Strings possible by swapping prefixes of pairs of Strings from the Array
- Queries to print count of distinct array elements after replacing element at index P by a given element
- Count of binary strings of length N having equal count of 0's and 1's and count of 1's ≥ count of 0's in each prefix substring
- Thue-Morse sequence
- Count of strings that can be formed from another string using each character at-most once
- Minimize length of string by replacing K pairs of distinct adjacent characters
- Count of distinct possible strings after performing given operations
- Count of Distinct strings possible by inserting K characters in the original string
- Maximize the value of A by replacing some of its digits with digits of B
- Python | Replacing Nth occurrence of multiple characters in a String with the given character
- Make the string lexicographically smallest and non palindromic by replacing exactly one character
- Find a string such that every character is lexicographically greater than its immediate next character
- Ways to split string such that each partition starts with distinct character
- Largest index for each distinct character in given string with frequency K
- Printing frequency of each character just after its consecutive occurrences
- Modify string by increasing each character by its distance from the end of the word
- Check if frequency of each character is equal to its position in English Alphabet
- Print all possible combinations of the string by replacing '$' with any other digit from the string
- Pairs of strings which on concatenating contains each character of "string"
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.


