131, divided palindromic sequence
Given a string s, s is divided into a number of sub-strings, each sub-string is a string palindrome.
S return all the possible partitioning scheme.
Example:
Input: "aab"
Output:
[
[ "AA", "B"],
[ "A", "A", "B"]
]
Method 1: backtracking
class Solution:
def partition(self, s: str) -> List[List[str]]:
if not s:
return [[]]
res = []
for i in range(len(s)):
tmp = s[:i+1]
if tmp == tmp[::-1]:
for j in self.partition(s[i+1 :]):
res.append([tmp]+ j)
return res
125 palindromic sequence verification
Given a string, verify that it is a palindrome string, consider only the alphabetic and numeric characters, can ignore letter case.
Description: In this problem, we define the empty string as a valid palindromic sequence.
Example 1:
Input: "A man, a plan, a canal: Panama"
Output: true
Example 2:
Input: "race a car"
Output: false
Note: When do your own division saw a palindrome string to write simpler approach
Solution 1:
class Solution:
def isPalindrome(self, s: str) -> bool:
if not s:
return True
s1 = s.lower()
res = []
for i in s1:
if i.isalpha() or i.isdigit():
res.append(i)
if res == res[::-1]:
return True
else:
return False
Solution 2:
class Solution:
def isPalindrome(self, s: str) -> bool:
if not s:
return True
s1 = s.lower()
res = []
for i in s1:
if i.isalpha() or i.isdigit():
res.append(i)
if len(res)%2 == 0:
i,j = 0,len(res)-1
while i <= (len(res)/2 - 1) and j >= len(res)/2:
if res[i] != res[j]:
return False
i += 1
j -= 1
return True
else:
i,j = 0,len(res)-1
while i <= (len(res)//2 -1) and j > len(res)//2 :
if res[i] != res[j]:
return False
i += 1
j -= 1
return True
139, the word split
medium
Given a string s non-empty and non-empty dictionary wordDict comprising a word list, it is determined whether the space s can be split into one or more words appear in the dictionary.
Description:
拆分时可以重复使用字典中的单词。
你可以假设字典中没有重复的单词。
Example 1:
Input: s = "leetcode", wordDict = [ "leet", "code"]
Output: true
interpretation: returns true because "leetcode" can be split into "leet code".
Example 2:
Input: s = "applepenapple", wordDict = [ "apple", "pen"]
Output: true
interpretation: returns true because "applepenapple" can be split into "apple pen apple".
Note that you can re-use the word in the dictionary.
Example 3:
Input: s = "catsandog", wordDict = [ "cats", "dog", "sand", "and", "cat"]
Output: false
Note: The use of dynamic programming
class Solution:
def wordBreak(self, s: str, wordDict: List[str]) -> bool:
if not s:
return True
dp = [0]
for i in range(len(s)+1):
for j in dp:
if s[j:i] in wordDict:
dp.append(i)
break
return dp[-1] == len(s)
140, word split II
difficult
The return section
word Split II
Given a string s non-empty and non-empty dictionary wordDict comprises a list of words, to increase the space in the string to build a sentence, the sentence such that all the words are in the dictionary. Return all these possible sentences.
Description:
分隔时可以重复使用字典中的单词。
你可以假设字典中没有重复的单词。
Example 1:
输入:
s = “catsanddog”
wordDict = [“cat”, “cats”, “and”, “sand”, “dog”]
输出:
[
“cats and dog”,
“cat sand dog”
]
Example 2:
Input:
S = "pineapplepenapple"
wordDict = [ "Apple", "PEN", "applepen", "Pine", "Pineapple"]
Output:
[
"Pine Apple PEN Apple",
"Pineapple PEN Apple",
"Pine applepen Apple "
]
explanation: Note that you can re-use the word in the dictionary.
Example 3:
Input:
S = "catsandog"
wordDict = [ "CATS", "Dog", "Sand", "and", "CAT"]
Output:
[]
Notes: problem-solving ideas: This question is not just like the word break can be split as to determine whether, and to find all manner of division, then we must consider the dfs. However, the direct use dfs problem-solving does not work, and why? Because the tree is too large to traverse it again if all the time complexity is too high, not by oj. So we need to prune and how to prune it? The results using the word break in the dynamic programming problem, before dfs, first determine whether the string can be split, if it can not be divided, skip this one. This question is actually dp + dfs.
class Solution:
def check(self,s,wordDict):
dp = [False for i in range(len(s)+1)]
dp[0] = 1
for i in range(1,len(s)+1):
for k in range(i):
if dp[k] and s[k:i] in wordDict :
dp[i] = True
return dp[len(s)]
def dfs(self,s,wordDict,stringlist):
if self.check(s,wordDict):
if len(s) == 0:
return Solution.res.append(stringlist[1:])
for i in range(1,len(s)+1):
if s[:i] in wordDict:
self.dfs(s[i:],wordDict,stringlist + ' ' + s[:i])
def wordBreak(self, s: str, wordDict: List[str]) -> List[str]:
Solution.res= []
self.dfs(s,wordDict,'')
return Solution.res