Find all strings formed from characters mapped to digits of a number | Function to find all strings formed from a given number where each digit maps to given characters . ; vector of strings to store output ; stores index of first occurrence of the digits in input ; maintains index of current digit considered ; for each digit ; store index of first occurrence of the digit in the map ; clear vector contents for future use ; do for each character thats maps to the digit ; for first digit , simply push all its mapped characters in the output list ; from second digit onwards ; for each string in output list append current character to it . ; convert current character to string ; Imp - If this is not the first occurrence of the digit , use same character as used in its first occurrence ; store strings formed by current digit ; nothing more needed to be done if this is not the first occurrence of the digit ; replace contents of output list with temp list ; Driver Code ; vector to store the mappings ; vector to store input number ; print all possible strings
def findCombinations ( input : list , table : list ) -> list : NEW_LINE INDENT out , temp = [ ] , [ ] NEW_LINE mp = dict ( ) NEW_LINE index = 0 NEW_LINE for d in input : NEW_LINE INDENT if d not in mp : NEW_LINE INDENT mp [ d ] = index NEW_LINE DEDENT temp . clear ( ) NEW_LINE for i in range ( len ( table [ d - 1 ] ) ) : NEW_LINE INDENT if index == 0 : NEW_LINE INDENT s = table [ d - 1 ] [ i ] NEW_LINE out . append ( s ) NEW_LINE DEDENT if index > 0 : NEW_LINE INDENT for string in out : NEW_LINE INDENT s = table [ d - 1 ] [ i ] NEW_LINE if mp [ d ] != index : NEW_LINE INDENT s = string [ mp [ d ] ] NEW_LINE DEDENT string = string + s NEW_LINE temp . append ( string ) NEW_LINE DEDENT if mp [ d ] != index : NEW_LINE INDENT break NEW_LINE DEDENT DEDENT DEDENT if index > 0 : NEW_LINE INDENT out = temp . copy ( ) NEW_LINE DEDENT index += 1 NEW_LINE DEDENT return out NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT table = [ [ ' A ' , ' B ' , ' C ' ] , [ ' D ' , ' E ' , ' F ' ] , [ ' G ' , ' H ' , ' I ' ] , [ ' J ' , ' K ' , ' L ' ] , [ ' M ' , ' N ' , ' O ' ] , [ ' P ' , ' Q ' , ' R ' ] , [ ' S ' , ' T ' , ' U ' ] , [ ' V ' , ' W ' , ' X ' ] , [ ' Y ' , ' Z ' ] ] NEW_LINE input = [ 1 , 2 , 1 ] NEW_LINE out = findCombinations ( input , table ) NEW_LINE for it in out : NEW_LINE INDENT print ( it , end = " ▁ " ) NEW_LINE DEDENT DEDENT