Skip to content

Commit 1b9bbcf

Browse files
committed
add python solution
1 parent 6f150b6 commit 1b9bbcf

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

Diff for: solutions/139.Word_Break/AC_dp_n2.py

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#!/usr/bin/python
2+
# -*- coding: utf-8 -*-
3+
# Author: illuz <iilluzen[at]gmail.com>
4+
# File: AC_dp_n2.py
5+
# Create Date: 2015-04-21 10:21:18
6+
# Usage: AC_dp_n2.py
7+
# Descripton:
8+
9+
10+
class Solution:
11+
# @param s, a string
12+
# @param dict, a set of string
13+
# @return a boolean
14+
def wordBreak(self, s, dict):
15+
n = len(s)
16+
dp = [False] * (n + 1)
17+
dp[0] = True
18+
for i in range(n):
19+
if dp[i]:
20+
for word in dict:
21+
j = len(word)
22+
if i + j <= n and s[i: i + j] == word:
23+
dp[i + j] = True
24+
return dp[n]
25+
26+
# debug
27+
s = Solution()
28+
print s.wordBreak('a', ['a'])

0 commit comments

Comments
 (0)