thảo luận Leetcode mỗi ngày

Swift, hơi tốn ram tí :shame:

Check xem cắt partition một khúc đầu nó có phải là palindrome không, nếu phải thì đệ quy khúc sau.

Swift:
class Solution {
    func partition(_ s: String) -> [[String]] {
        guard s.count > 1 else { return [[s]] }

        var results: [[String]] = []

        func check(pre: [String], sub:[String.Element]) {
            guard sub.count > 0 else {
                results += [pre]
                return
            }
            do {
                var newSub = sub
                newSub.removeFirst()
                check(pre: pre + [String(sub.first!)], sub:newSub)
            }
            if sub.count > 1 {
                for lenght in 2...sub.count {
                    //
                    var isPalindrome = true
                    for i in 0..<lenght/2 {
                        if sub[i] != sub[lenght-i-1] {
                            isPalindrome = false
                            break
                        }
                    }
                    if isPalindrome {
                        let strArr = lenght < sub.count ? Array(sub[0..<lenght]) : sub
                        let newSub = lenght < sub.count ? Array(sub[lenght..<sub.count]) : []
                        check(pre: pre + [String(strArr)], sub:newSub)
                    }
                }
            }
        }
        check(pre: [], sub: Array(s))
        return results
    }
}
 
Python:
class Solution:
    def partition(self, s: str) -> List[List[str]]:
        @cache
        def isPalindrome(start, end):
            return start >= end or (s[start] == s[end] and isPalindrome(start + 1, end - 1))
        
        @cache
        def dfs(i):
            if i == n:
                return [[]]
            
            ans = []
            for j in range(i, n):
                if isPalindrome(i, j):
                    firstPart = s[i:j + 1]
                    for partitionList in dfs(j + 1):
                        ans.append([firstPart, *partitionList])
            return ans

        n = len(s)
        return dfs(0)
 
22/05/2024: Tiếp tục chuỗi 1 line

Python:
return [[]] if not s else [[s[:i]] + p for i in range(1, len(s)+1) if s[:i] == s[i-1::-1] for p in self.partition(s[i:])]
One-liner tinh hoa :*
Hôm trước đọc lee215 giải Q4 Contest 1 dòng ảo vl thím ạ
 
Python:
class Solution:
    def partition(self, s: str) -> List[List[str]]:

        def checkPalindrome(string):
            i = 0
            j = len(string) - 1
            while j > i:
                if string[i] != string[j]:
                    return False
                i += 1
                j -= 1
            return True
        
        def innerPartition(s, start, end):
            if start == end:
                return [[]]
            result = []
            for i in range(start, end):
                if checkPalindrome(s[start:i+1]):
                    temps = innerPartition(s, i+1, end)
                    for temp in temps:
                        result.append([s[start:i+1]] + temp)
            return result

        return innerPartition(s, 0, len(s))
 
đả đảo cũng có làm đc gì đâu. Chỉ quăng gạch mà k hiểu đc những sự tinh túy trong sol của t thì t cũng ignore thôi, :doubt:
mình trình còi đọc ko có hiểu gì hết :D mong quý anh phổ cập tinh hoa 1line code cho bàn dân được mở mang :feel_good::feel_good:
 
mình trình còi đọc ko có hiểu gì hết :D mong quý anh phổ cập tinh hoa 1line code cho bàn dân được mở mang :feel_good::feel_good:
1-line cũng là dùng list comprehession thôi bác, thích thì mình viết thôi chứ căn bản viết về 1 line logic phức tạp rất khó đọc nên đi làm không ai viết phức tạp thế cả :LOL:)
 
choảng nhau đến nơi rồi
kJb6jDM.png
 
Back
Top