Count distinct strings possible by replacing each character by its Morse code | 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
def uniqueMorseRep ( arr ) : NEW_LINE INDENT morseCode = [ " . - " , " - . . . " , " - . - . " , " - . . " , " . " , " . . - . " , " - - . " , " . . . . " , " . . " , " . - - - " , " - . - " , " . - . . " , " - - " , " - . " , " - - - " , " . - - . " , " - - . - " , " . - . " , " . . . " , " - " , " . . - " , " . . . - " , " . - - " , " - . . - " , " - . - - " , " - - . . " ] ; NEW_LINE st = set ( ) ; NEW_LINE N = len ( arr ) ; NEW_LINE for i in range ( N ) : NEW_LINE INDENT temp = " " ; NEW_LINE M = len ( arr [ i ] ) ; NEW_LINE for j in range ( M ) : NEW_LINE INDENT temp += morseCode [ ord ( arr [ i ] [ j ] ) - ord ( ' a ' ) ] ; NEW_LINE DEDENT st . add ( temp ) ; NEW_LINE DEDENT return len ( st ) ; NEW_LINE DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT arr = [ " gig " , " zeg " , " gin " , " msn " ] ; NEW_LINE print ( uniqueMorseRep ( arr ) , " " ) ; NEW_LINE DEDENT