-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSolution.rb
More file actions
50 lines (32 loc) · 922 Bytes
/
Solution.rb
File metadata and controls
50 lines (32 loc) · 922 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# @param {String} s
# @return {Boolean}
def is_palindrome(s)
if s.nil?
return false
end
if s == ''
return true
end
formattedStr = s.downcase
left = 0; right = formattedStr.size - 1
while left < right
unless (formattedStr[left] >= 'a' and formattedStr[left] <= 'z') or (formattedStr[left] >= '0' and formattedStr[left] <= '9')
left += 1
next
end
unless (formattedStr[right] >= 'a' and formattedStr[right] <= 'z') or (formattedStr[right] >= '0' and formattedStr[right] <= '9')
right -= 1
next
end
# print "L_index: #{left}, L_value: #{formattedStr[left]}; R_index: #{right}, R_value: #{formattedStr[right]}.\n"
if formattedStr[left] != formattedStr[right]
return false
end
left += 1
right -= 1
end
true
end
puts is_palindrome 'A man, a plan, a canal: Panama'
puts is_palindrome 'race a car'
puts is_palindrome 'ab'