palindrome_partitioning
1# @leet start 2class Solution: 3 def partition(self, s: str) -> list[list[str]]: 4 def is_palindrome(s): 5 return s == s[::-1] 6 7 def dfs(s, path, result): 8 if not s: 9 result.append(path) 10 return 11 for i in range(1, len(s) + 1): 12 if is_palindrome(s[:i]): 13 dfs(s[i:], path + [s[:i]], result) 14 15 result = [] 16 dfs(s, [], result) 17 return result 18 19 20# @leet end 21 22 23def test(): 24 assert 2 + 2 == 4
class
Solution:
3class Solution: 4 def partition(self, s: str) -> list[list[str]]: 5 def is_palindrome(s): 6 return s == s[::-1] 7 8 def dfs(s, path, result): 9 if not s: 10 result.append(path) 11 return 12 for i in range(1, len(s) + 1): 13 if is_palindrome(s[:i]): 14 dfs(s[i:], path + [s[:i]], result) 15 16 result = [] 17 dfs(s, [], result) 18 return result
def
partition(self, s: str) -> list[list[str]]:
4 def partition(self, s: str) -> list[list[str]]: 5 def is_palindrome(s): 6 return s == s[::-1] 7 8 def dfs(s, path, result): 9 if not s: 10 result.append(path) 11 return 12 for i in range(1, len(s) + 1): 13 if is_palindrome(s[:i]): 14 dfs(s[i:], path + [s[:i]], result) 15 16 result = [] 17 dfs(s, [], result) 18 return result
def
test():