Skip to content

Commit 2416657

Browse files
authored
Merge pull request neetcode-gh#2030 from saip7795/sp/palindrome-partitioning
Create : 0131-Palindrome-Partitioning.rb
2 parents 3ea0fd8 + cf41657 commit 2416657

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

ruby/0131-palindrome-partitioning.rb

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
def partition(s)
2+
@result = []
3+
@part = []
4+
@s = s
5+
6+
def dfs(i)
7+
if i >= @s.length
8+
@result.append(@part.dup())
9+
return
10+
end
11+
(i..@s.length-1).each do |j|
12+
if is_palindrome(@s,i,j)
13+
@part.append(@s[i..j])
14+
dfs(j+1)
15+
@part.pop()
16+
end
17+
end
18+
end
19+
20+
dfs(0)
21+
22+
return @result
23+
end
24+
25+
26+
def is_palindrome(s,i,j)
27+
while i<j
28+
return false if s[i] != s[j]
29+
i +=1
30+
j -=1
31+
end
32+
return true
33+
end

0 commit comments

Comments
 (0)