Scribbling

LeetCode: 131. Palindrome Partitioning 본문

Computer Science/Coding Test

LeetCode: 131. Palindrome Partitioning

focalpoint 2021. 11. 13. 01:09

 

class Solution:
    def partition(self, s: str) -> List[List[str]]:
        self.ret = []
        self.dfs(s, [])
        return self.ret
    
    def dfs(self, s, path):
        if not s:
            self.ret.append(path)
            return        
        for i in range(1, len(s)+1):
            first, second = s[:i], s[i:]
            if self.is_Palindromic(first):
                self.dfs(second, path+[first])
    
    def is_Palindromic(self, s):
        return s == s[::-1]