For a given string, check whether a string is "smashable". Smashable means whether a word can be reduced to a single character, which is in your predefined dictionary of words, and every intermediate word during the reduction should also lie in the dictionary.
For example, given: SPRINT
PINT
PIT
IT
I
My solution
class Solution(object):
def smashable(self, string, words):
return self.dfs(string, words)
def dfs(self, string, words):
if len(string) == 1:
if string in words:
return True
return False
for i in range(len(string)):
curr_string = string[:i] + string[i+1:]
if curr_string in words:
return self.dfs(curr_string, words)