|
| 1 | +#!/usr/bin/env python |
| 2 | +''' |
| 3 | +http://thenoisychannel.com/2011/08/08/retiring-a-great-interview-problem/ |
| 4 | +''' |
| 5 | +from __future__ import division |
| 6 | +import random |
| 7 | + |
| 8 | + |
| 9 | +''' |
| 10 | +Leetcode: Word Break |
| 11 | +Given a string s and a dictionary of words dict, determine if s can be segmented into a space-separated sequence of one or more dictionary words. |
| 12 | +For example, given |
| 13 | +s = "leetcode", |
| 14 | +dict = ["leet", "code"]. |
| 15 | +Return true because "leetcode" can be segmented as "leet code". |
| 16 | +''' |
| 17 | +def wordbreak(s, words): |
| 18 | + if len(s) == 0: return True |
| 19 | + # Test all prefixes |
| 20 | + for i in range(1, len(s)+1): |
| 21 | + sub = s[:i] |
| 22 | + if not sub in words: continue |
| 23 | + if wordbreak(s[i:], words): |
| 24 | + return True |
| 25 | + return False |
| 26 | + |
| 27 | + |
| 28 | +### DP? O(n^2) |
| 29 | +# cut(i) = s[:i] can be splitted |
| 30 | +# cut(i+1) = OR{cut(j) and s[j:i] is a word for 0 <= j < i} |
| 31 | +def wordbreak_DP(s, words): |
| 32 | + words = set(words) |
| 33 | + cut = {0: True} |
| 34 | + for i in range(1,len(s)+1): |
| 35 | + cut[i] = False |
| 36 | + for j in range(i): |
| 37 | + if cut[j] and s[j:i] in words: |
| 38 | + cut[i] = True |
| 39 | + break |
| 40 | + return cut[len(s)] |
| 41 | + |
| 42 | + |
| 43 | +memo = {} |
| 44 | +def wordbreak_memo(s, words): |
| 45 | + if s == '': return True |
| 46 | + global memo |
| 47 | + if s in memo: return memo[s] |
| 48 | + for i in range(1,len(s)+1): |
| 49 | + sub = s[:i] |
| 50 | + if not sub in words: continue |
| 51 | + if wordbreak_memo(s[i:], words): |
| 52 | + memo[s] = True#; print memo |
| 53 | + return True |
| 54 | + return False |
| 55 | + |
| 56 | +''' |
| 57 | +Leetcode: Word Break II |
| 58 | +Given a string s and a dictionary of words dict, add spaces in s to construct a sentence where each word is a valid dictionary word. Return all such possible sentences. |
| 59 | +For example, given |
| 60 | +s = "catsanddog", |
| 61 | +dict = ["cat", "cats", "and", "sand", "dog"]. |
| 62 | +A solution is ["cats and dog", "cat sand dog"]. |
| 63 | +''' |
| 64 | +# ~O(exponential) |
| 65 | +def wordbreak2_generator(s, words): |
| 66 | + if len(s) == 0: yield [] |
| 67 | + else: |
| 68 | + # Test all prefixes |
| 69 | + for i in range(1, len(s)+1): |
| 70 | + sub = s[:i] |
| 71 | + if not sub in words: continue |
| 72 | + for others in wordbreak2_generator(s[i:], words): |
| 73 | + yield [sub] + others |
| 74 | + |
| 75 | +def wordbreak2(s, words): |
| 76 | + words = set(words) |
| 77 | + for combo in wordbreak2_generator(s, words): |
| 78 | + print combo |
| 79 | + |
| 80 | + |
| 81 | + |
| 82 | +if __name__ == '__main__': |
| 83 | + s = "catsanddogseat" |
| 84 | + words = set(["cat", "cats", "and", "sand", "dog", "dogs", "eat", "seat"]) |
| 85 | + print wordbreak(s, words) |
| 86 | + print wordbreak_DP(s, words) |
| 87 | + print wordbreak_memo(s, words) |
| 88 | + wordbreak2(s, words) |
| 89 | + |
| 90 | + |
0 commit comments