1. Recursion

class Solution:
    def isDecomposable(self, s: str) -> bool:
        n = len(s)

        def same(i, length):
            return i + length <= n and len(set(s[i:i + length])) == 1

        def dfs(i, used):
            if i == n:
                return used
            if same(i, 3) and dfs(i + 3, used):
                return True
            if not used and same(i, 2) and dfs(i + 2, True):
                return True
            return False

        return dfs(0, False)

Time & Space Complexity

  • Time complexity: O(n2)O(n ^ 2)
  • Space complexity: O(n)O(n) for the recursion stack.

Where nn is the length of the string ss.


2. Dynamic Programming (Top-Down)

class Solution:
    def isDecomposable(self, s: str) -> bool:
        n = len(s)
        dp = [[None] * 2 for _ in range(n)]

        def same(i, length):
            return i + length <= n and len(set(s[i:i + length])) == 1

        def dfs(i, used):
            if i == n:
                return used == 1
            if dp[i][used] is not None:
                return dp[i][used]
            res = False
            if same(i, 3) and dfs(i + 3, used):
                res = True
            elif used == 0 and same(i, 2) and dfs(i + 2, 1):
                res = True
            dp[i][used] = res
            return res

        return dfs(0, 0)

Time & Space Complexity

  • Time complexity: O(n)O(n)
  • Space complexity: O(n)O(n)

Where nn is the length of the string ss.


3. Dynamic Programming (Bottom-Up)

class Solution:
    def isDecomposable(self, s: str) -> bool:
        n = len(s)

        def same(i, length):
            return i + length <= n and len(set(s[i:i + length])) == 1

        dp = [[False] * 2 for _ in range(n + 1)]
        dp[n][1] = True
        for i in range(n - 1, -1, -1):
            for used in range(2):
                if same(i, 3) and dp[i + 3][used]:
                    dp[i][used] = True
                elif used == 0 and same(i, 2) and dp[i + 2][1]:
                    dp[i][used] = True
        return dp[0][0]

Time & Space Complexity

  • Time complexity: O(n)O(n)
  • Space complexity: O(n)O(n)

Where nn is the length of the string ss.


4. Greedy

class Solution:
    def isDecomposable(self, s: str) -> bool:
        n = len(s)
        i = 0
        usedTwo = False
        while i < n:
            if i + 2 < n and s[i] == s[i + 1] == s[i + 2]:
                i += 3
            elif not usedTwo and i + 1 < n and s[i] == s[i + 1]:
                usedTwo = True
                i += 2
            else:
                return False
        return usedTwo

Time & Space Complexity

  • Time complexity: O(n)O(n)
  • Space complexity: O(1)O(1) extra space.

Where nn is the length of the string ss.


5. Two Pointers

class Solution:
    def isDecomposable(self, s: str) -> bool:
        n = len(s)
        twos = 0
        l = 0
        while l < n:
            r = l
            while r < n and s[r] == s[l]:
                r += 1
            length = r - l
            if length % 3 == 1:
                return False
            if length % 3 == 2:
                twos += 1
            l = r
        return twos == 1

Time & Space Complexity

  • Time complexity: O(n)O(n)
  • Space complexity: O(1)O(1) extra space.

Where nn is the length of the string ss.