Leetcode 139: Word Break

Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, determine if s can be segmented into a space-separated sequence of one or more dictionary words. You may assume the dictionary does not contain duplicate words.

For example, given
s = "leetcode",
dict = ["leet", "code"].

Return true because "leetcode" can be segmented as "leet code".

 

  1 public class Solution {
  2     public bool WordBreak(string s, IList<string> wordDict) {
  3         var hashset = new HashSet<string>();
  4         
  5         foreach (var word in wordDict)
  6         {
  7             hashset.Add(word);
  8         }
  9         
 10         var dp = new bool[s.Length + 1];
 11         dp[0] = true;
 12         
 13         for (int i = 1; i <= s.Length; i++)
 14         {
 15             for (int j = 0; j < i; j++)
 16             {
 17                 if (hashset.Contains(s.Substring(j, i - j)))
 18                 {
 19                     dp[i] = dp[j];
 20                     if (dp[i]) break;
 21                 }
 22             }
 23         }
 24         
 25         return dp[s.Length];
 26     }
 27     
 28 }
 29 
 30 public class PrefixTreeSolution {
 31     // prefix tree node
 32     public class TreeNode
 33     {
 34         public bool isWord;
 35         public TreeNode[] children;
 36         public char val;
 37         
 38         public TreeNode(char val, bool isWord)
 39         {
 40             this.val = val;
 41             this.isWord = isWord;
 42             this.children = new TreeNode[26];
 43         }
 44         
 45         public void AddChild(char c)
 46         {
 47             int index = this.GetIndex(c);
 48             
 49             if (this.children[index] == null)
 50             {
 51                 this.children[index] = new TreeNode(c, false);
 52             }
 53         }
 54         
 55         public TreeNode GetChild(char c)
 56         {
 57             return this.children[this.GetIndex(c)];
 58         }
 59         
 60         private int GetIndex(char c)
 61         {
 62             return (int)c - (int)'a';
 63         }
 64     }
 65     
 66     public bool WordBreak(string s, IList<string> wordDict) {
 67         var root = new TreeNode('*', false);
 68         
 69         // build prefix tree
 70         foreach (var word in wordDict)
 71         {
 72             InsertWord(root, word, 0);
 73         }
 74         
 75         return DFS(s, 0, root, root);
 76     }
 77     
 78     private bool DFS(string s, int start, TreeNode node, TreeNode root)
 79     {
 80         if (start >= s.Length) return true;
 81         var cur = node;
 82         
 83         for (int i = start; i < s.Length; i++)
 84         {
 85             char c = s[i];
 86             
 87             var child = cur.GetChild(c);
 88             if (child == null)
 89             {
 90                 return false;
 91             }
 92             else if (child.isWord)
 93             {
 94                 if (DFS(s, i + 1, root, root))
 95                 {
 96                     return true;
 97                 }
 98             }
 99             
100             cur = child;
101         }
102         
103         return false;
104     }
105     
106     private void InsertWord(TreeNode node, string s, int start)
107     {
108         if (start >= s.Length)
109         {
110             node.isWord = true;
111             return;
112         }
113         
114         char c = s[start];
115         
116         node.AddChild(c);
117         
118         InsertWord(node.GetChild(c), s, start + 1);
119     }
120 }

 

posted @ 2017-11-22 07:34  逸朵  阅读(176)  评论(0)    收藏  举报