![]() Append the current letter to the current combination combination = combination + letter.Iterate over the letters mapping the next available digit.Add a base case: If there are no more digits to check that means that the current combination is done.Create a recursive function recurse(combination, next_digits).For all the corresponding keypad characters of the input digit, we will be appending those characters to the front of the queue and then pushing them into the queue until the length of strings in the queue becomes equal to the length of the input digit string. If we will use the BFS approach, then we can take a queue of strings and will push an empty string to it. However, the answer would be the same in both the iterative approach. If we use the DFS approach then we can use stack and if we use BFS then we can use a queue. Here, if we look at the recursion tree, you can draw conclusions that to get all possible strings, we can make a DFS search or a BFS search. Generally, we can convert all-recursive solution to an iterative solution. Is there an iterative solution to this problem? In short, We keep adding each possible letter recursively and this will generate all the possible strings. In that case, the recursion tree will have further children until the result string length will be equal to the number of input digit (Why?). What if we have more than 2 digits as input? The green circles show the base case for the recursion when the length of the string is equal to the length of input digits. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |