Skip to content

Commit b40fe95

Browse files
committed
Extract split method from loop
1 parent 2375a7f commit b40fe95

File tree

1 file changed

+5
-4
lines changed

1 file changed

+5
-4
lines changed

data_structures/arrays/strings/almost_palindrome_checker.rb

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,11 @@
2525
def almost_palindrome_checker(string)
2626
p1 = 0
2727
p2 = string.length - 1
28+
array = string.split('')
2829

2930
while p1 < p2
30-
if string.split('')[p1] != string.split('')[p2]
31-
return palindrome_checker(string, p1, p2 - 1) || palindrome_checker(string, p1 + 1, p2)
31+
if array[p1] != array[p2]
32+
return palindrome_checker(array, p1, p2 - 1) || palindrome_checker(array, p1 + 1, p2)
3233
end
3334
p1 += 1
3435
p2 -= 1
@@ -37,9 +38,9 @@ def almost_palindrome_checker(string)
3738
true
3839
end
3940

40-
def palindrome_checker(string, p1, p2)
41+
def palindrome_checker(array, p1, p2)
4142
while p1 < p2
42-
if string.split('')[p1] != string.split('')[p2]
43+
if array[p1] != array[p2]
4344
return false
4445
end
4546
p1 += 1

0 commit comments

Comments
 (0)