SHARE
TWEET

Untitled

a guest Jun 25th, 2019 53 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. word_list = ['a',
  2. 'b',
  3. 'ba',
  4. 'bca',
  5. 'bda',
  6. 'bdca'
  7.              ]
  8. short = ['a', 'c', 'e']
  9.  
  10. apple = ['sapple', 'apple', 'a']
  11.  
  12. def longestStringChain(word_list):
  13.     max = 0
  14.     for word in word_list:
  15.         depth = find_subword_count(word, word_list)
  16.         if depth > max:
  17.             max = depth
  18.     return max + 1
  19.  
  20.  
  21. def find_subword_count(current_word, word_list):
  22.  
  23.     if len(current_word) == 1:
  24.         return 0
  25.     max = 0
  26.     for i in range(len(current_word)):
  27.         chars = list(current_word)
  28.         del chars[i]
  29.         subword = ''.join(chars)
  30.         if subword not in word_list:
  31.             continue
  32.         count = 1 + find_subword_count(subword, word_list)
  33.         if max >= count:
  34.             continue
  35.         max = count
  36.     return max
  37.  
  38.  
  39.  
  40. print(longestStringChain(word_list))
  41.  
  42. print(longestStringChain(short))
  43.  
  44. print(longestStringChain(apple))
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top