Count distinct strings possible by replacing each character by its Morse code | C ++ program to implement the above approach ; Function to count unique array elements by replacing each character by its Morse code ; Stores Morse code of all lowercase characters ; Stores distinct elements of string by replacing each character by Morse code ; Stores length of arr [ ] array ; Traverse the array ; Stores the Morse code of arr [ i ] ; Stores length of current string ; Update temp ; Insert temp into st ; Return count of elements in the set ; Driver code
#include <bits/stdc++.h> NEW_LINE using namespace std ; int uniqueMorseRep ( vector < string > & arr ) { vector < string > morseCode = { " . - " , " - . . . " , " - . - . " , " - . . " , " . " , " . . - . " , " - - . " , " . . . . " , " . . " , " . - - - " , " - . - " , " . - . . " , " - - " , " - . " , " - - - " , " . - - . " , " - - . - " , " . - . " , " . . . " , " - " , " . . - " , " . . . - " , " . - - " , " - . . - " , " - . - - " , " - - . . " } ; set < string > st ; int N = arr . size ( ) ; for ( int i = 0 ; i < N ; i ++ ) { string temp = " " ; int M = arr [ i ] . length ( ) ; for ( int j = 0 ; j < M ; j ++ ) { temp += morseCode [ arr [ i ] [ j ] - ' a ' ] ; } st . insert ( temp ) ; } return st . size ( ) ; } int main ( ) { vector < string > arr = { " gig " , " zeg " , " gin " , " msn " } ; cout << uniqueMorseRep ( arr ) << endl ; }