leetcode

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README

commit 059292e8e6c05a3e3465a3df1a447e559031cdd8
parent eea791708232d7e8b1db4f2e257e814e8117f580
Author: Andrew Laack <andrew@laack.co>
Date:   Wed, 16 Jul 2025 11:45:22 -0500

Completed palindrome partitioning

Diffstat:
Apalindrome-partitioning/palindrome-partitioningV1.py | 32++++++++++++++++++++++++++++++++
1 file changed, 32 insertions(+), 0 deletions(-)

diff --git a/palindrome-partitioning/palindrome-partitioningV1.py b/palindrome-partitioning/palindrome-partitioningV1.py @@ -0,0 +1,32 @@ +class Solution: + def partition(self, s: str) -> List[List[str]]: + results = all_parts(s, [], 0) + return results + + +def all_parts(s, current, used): + + if len(s) == used: + return [current] + + ret_ls = [] + + for i in range(used, len(s)): + if is_palindrome(s, i, used): + cp_ls = current.copy() + cp_ls.append(s[used : i + 1]) + results = all_parts(s, cp_ls, i + 1) + for res in results: + ret_ls.append(res) + + return ret_ls + + +def is_palindrome(s, upper, lower): + while upper > lower: + if s[lower] != s[upper]: + return False + + lower += 1 + upper -= 1 + return True