Advertisement
hoangreal

Return all sentence where each word is a valid dictionary word

Oct 21st, 2024
44
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.77 KB | None | 0 0
  1. """
  2. Given a string s and a dictionary of strings wordDict, add spaces in s to construct a sentence where each word is a valid dictionary word. Return all such possible sentences in any order.
  3.  
  4. Note that the same word in the dictionary may be reused multiple times in the segmentation.
  5. """
  6.  
  7. class TrieNode:
  8.     def __init__(self):
  9.         self.isEnd = False
  10.         self.children = [None] * 26  # For lowercase English letters
  11.  
  12.  
  13. class Trie:
  14.     def __init__(self):
  15.         self.root = TrieNode()
  16.  
  17.     def insert(self, word):
  18.         node = self.root
  19.         for char in word:
  20.             index = ord(char) - ord("a")
  21.             if not node.children[index]:
  22.                 node.children[index] = TrieNode()
  23.             node = node.children[index]
  24.         node.isEnd = True
  25.  
  26.  
  27. class Solution:
  28.     def wordBreak(self, s: str, wordDict: List[str]) -> List[str]:
  29.         # Build the Trie from the word dictionary
  30.         trie = Trie()
  31.         for word in wordDict:
  32.             trie.insert(word)
  33.  
  34.         # Map to store results of subproblems
  35.         dp = {}
  36.  
  37.         # Iterate from the end of the string to the beginning
  38.         for start_idx in range(len(s), -1, -1):
  39.             # List to store valid sentences starting from start_idx
  40.             valid_sentences = []
  41.  
  42.             # Initialize current node to the root of the trie
  43.             current_node = trie.root
  44.  
  45.             # Iterate from start_idx to the end of the string
  46.             for end_idx in range(start_idx, len(s)):
  47.                 char = s[end_idx]
  48.                 index = ord(char) - ord("a")
  49.  
  50.                 # Check if the current character exists in the trie
  51.                 if not current_node.children[index]:
  52.                     break
  53.  
  54.                 # Move to the next node in the trie
  55.                 current_node = current_node.children[index]
  56.  
  57.                 # Check if we have found a valid word
  58.                 if current_node.isEnd:
  59.                     current_word = s[start_idx : end_idx + 1]
  60.  
  61.                     # If it's the last word, add it as a valid sentence
  62.                     if end_idx == len(s) - 1:
  63.                         valid_sentences.append(current_word)
  64.                     else:
  65.                         # If it's not the last word, append it to each sentence formed by the remaining substring
  66.                         sentences_from_next_index = dp.get(end_idx + 1, [])
  67.                         for sentence in sentences_from_next_index:
  68.                             valid_sentences.append(
  69.                                 current_word + " " + sentence
  70.                             )
  71.  
  72.             # Store the valid sentences in dp
  73.             dp[start_idx] = valid_sentences
  74.  
  75.         # Return the sentences formed from the entire string
  76.         return dp.get(0, [])
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement