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
function uniqueMorseRep ( arr ) { var morseCode = [ " " , " " , " " , " " , " " , " " , " " , " " , " " , " " , " " , " " , " " , " " , " " , " " , " " , " " , " " , " " , " " , " " , " " , " " , " " , " " ] ; var st = new Set ( ) ; var N = arr . length ; for ( var i = 0 ; i < N ; i ++ ) { var temp = " " ; var M = arr [ i ] . length ; for ( var j = 0 ; j < M ; j ++ ) { temp += morseCode [ arr [ i ] [ j ] . charCodeAt ( 0 ) - ' ' . charCodeAt ( 0 ) ] ; } st . add ( temp ) ; } return st . size ; } var arr = [ " " , " " , " " , " " ] ; document . write ( uniqueMorseRep ( arr ) ) ;