Skip to content

Commit

Permalink
Merge pull request neetcode-gh#2030 from saip7795/sp/palindrome-parti…
Browse files Browse the repository at this point in the history
…tioning

Create : 0131-Palindrome-Partitioning.rb
  • Loading branch information
saip7795 authored Jan 17, 2023
2 parents 3ea0fd8 + cf41657 commit 2416657
Showing 1 changed file with 33 additions and 0 deletions.
33 changes: 33 additions & 0 deletions ruby/0131-palindrome-partitioning.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
def partition(s)
@result = []
@part = []
@s = s

def dfs(i)
if i >= @s.length
@result.append(@part.dup())
return
end
(i..@s.length-1).each do |j|
if is_palindrome(@s,i,j)
@part.append(@s[i..j])
dfs(j+1)
@part.pop()
end
end
end

dfs(0)

return @result
end


def is_palindrome(s,i,j)
while i<j
return false if s[i] != s[j]
i +=1
j -=1
end
return true
end

0 comments on commit 2416657

Please sign in to comment.