imashutosh51

Palindrome Partitioning

May 6th, 2026
35
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.40 KB | None | 0 0
  1. def is_palindrome(s):
  2.     if s[:]==s[::-1]:
  3.         return True
  4.     return False
  5.  
  6. def fun(i,s,cur,ans):
  7.     if i==len(s):
  8.         ans.append(cur)
  9.     for end in range(i+1,len(s)+1):
  10.         if is_palindrome(s[i:end]):
  11.             fun(end,s,cur+[s[i:end]],ans)
  12.  
  13. class Solution:
  14.     def partition(self, s: str) -> List[List[str]]:
  15.         ans=[]
  16.         fun(0,s,[],ans)
  17.         return ans
  18.  
  19.        
Advertisement
Add Comment
Please, Sign In to add comment