1 minute read

131. Palindrome Partitioning (medium)

class Solution:
    def is_palindrome(self, s: str):
        return s == s[::-1]

    def dfs(self, s: str, res, current):
        n = len(s)
        if n == 0:
            res.append(current)
            return
        for i in range(1, n + 1):
            target = s[:i]
            if self.is_palindrome(target):
                temp = current[:]
                temp.append(target)
                self.dfs(s[i:], res, temp)

    def partition(self, s: str) -> List[List[str]]:
        res = []
        self.dfs(s, res, [])
        return res

1525. Number of Good Ways to Split a String (medium)

class Solution:
    def numSplits(self, s: str) -> int:
        n = len(s)
        res = 0
        back_counter = collections.Counter(s)
        front_counter = collections.Counter()
        for i in range(n):
            cur = s[i]
            front_counter[cur] += 1
            back_counter[cur] -= 1
            if back_counter[cur] == 0:
                back_counter.pop(cur)
            a, b = len(front_counter.keys()), len(back_counter.keys())
            if a == b:
                res += 1
            elif a > b:
                break
        return res

58. Length of Last Word (easy)

class Solution:
    def lengthOfLastWord(self, s: str) -> int:
        split = s.split()
        return len(split[-1])

71. Simplify Path (medium)

class Solution:
    def simplifyPath(self, path: str) -> str:
        split = [x for x in path.split('/') if x != '']
        temp = []
        for p in split:
            if p == '.':
                continue
            elif p == '..':
                if len(temp) >= 1: temp.pop()
            else:
                temp.append(p)
        # print(temp)
        return '/' + '/'.join(temp)