Skip to content

Commit 2375a7f

Browse files
committed
Merge branch 'jk-palindrome' of https://github.com/jsca-kwok/Ruby into jk-palindrome
2 parents 6c72cd2 + 49db26c commit 2375a7f

File tree

2 files changed

+128
-0
lines changed

2 files changed

+128
-0
lines changed

DIRECTORY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
* [Almost Palindrome Checker](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/strings/almost_palindrome_checker.rb)
2525
* [Anagram Checker](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/strings/anagram_checker.rb)
2626
* [Jewels And Stones](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/strings/jewels_and_stones.rb)
27+
* [Palindrome](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/strings/palindrome.rb)
2728
* [Remove Vowels](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/strings/remove_vowels.rb)
2829
* [Two Sum](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/two_sum.rb)
2930
* [Two Sum Ii](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/two_sum_ii.rb)
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
# Challenge name: Valid Palindrome
2+
#
3+
# Given a string s, determine if it is a palindrome,
4+
# considering only alphanumeric characters and ignoring cases.
5+
#
6+
# Example 1:
7+
# Input: s = "A man, a plan, a canal: Panama"
8+
# Output: true
9+
# Explanation: "amanaplanacanalpanama" is a palindrome.
10+
#
11+
# Example 2:
12+
# Input: s = "race a car"
13+
# Output: false
14+
# Explanation: "raceacar" is not a palindrome.
15+
#
16+
# Constraints:
17+
# 1 <= s.length <= 2 * 105
18+
# s consists only of printable ASCII characters.
19+
# @param {String} s
20+
# @return {Boolean}
21+
22+
#
23+
# Approach 1: Using Ruby method .reverse
24+
#
25+
# Time Complexity: O(n)
26+
#
27+
def is_palindrome(s)
28+
letters_only = s.downcase.gsub(/[^0-9a-z]/i, '')
29+
letters_only.reverse == letters_only
30+
end
31+
32+
s = 'A man, a plan, a canal: Panama'
33+
puts is_palindrome(s)
34+
# Output: true
35+
# Explanation: "amanaplanacanalpanama" is a palindrome.
36+
37+
s = 'race a car'
38+
puts is_palindrome(s)
39+
# Output: false
40+
# Explanation: "raceacar" is not a palindrome.
41+
42+
s = 'ab_a'
43+
puts is_palindrome(s)
44+
# Output: true
45+
# Explanation: "aba" is a palindrome.
46+
47+
#
48+
# Approach 2: Reversed array
49+
#
50+
# Complexity Analysis:
51+
#
52+
# Time Complexity: O(n), in length n of the string.
53+
#
54+
# We need to iterate through the string: When we filter out non-alphanumeric characters and convert the remaining
55+
# characters to lower-case. When we reverse the string. When we compare the original and the reversed strings.
56+
# Each iteration runs linearly in time (since each character operation completes in constant time).
57+
# Thus, the effective run-time complexity is linear.
58+
#
59+
# Space Complexity: O(n), in length n of the string. We need O(n) additional
60+
# space to store the filtered string and the reversed string.
61+
#
62+
def is_palindrome(s)
63+
letters_only_array = s.downcase.gsub(/[^0-9a-z]/i, '').split('')
64+
reversed_array = []
65+
letters_only_array.each do |letter|
66+
reversed_array.unshift(letter)
67+
end
68+
letters_only_array == reversed_array
69+
end
70+
71+
s = 'A man, a plan, a canal: Panama'
72+
puts is_palindrome(s)
73+
# Output: true
74+
# Explanation: "amanaplanacanalpanama" is a palindrome.
75+
76+
s = 'race a car'
77+
puts is_palindrome(s)
78+
# Output: false
79+
# Explanation: "raceacar" is not a palindrome.
80+
81+
s = 'ab_a'
82+
puts is_palindrome(s)
83+
# Output: true
84+
# Explanation: "aba" is a palindrome.
85+
86+
#
87+
# Approach 2: Two Pointers
88+
#
89+
90+
#
91+
# Complexity Analysis:
92+
#
93+
# Time Complexity: O(n), in length n of the string. We traverse over each
94+
# character at most once until the two pointers meet in the middle, or when
95+
# we break and return early.
96+
# Space Complexity: O(1). No extra space required, at all.
97+
#
98+
def is_palindrome(s)
99+
letters_only = s.downcase.gsub(/[^0-9a-z]/i, '')
100+
p1 = 0
101+
p2 = letters_only.length - 1
102+
103+
while p1 < p2
104+
if letters_only[p1] == letters_only[p2]
105+
p1 += 1
106+
p2 -= 1
107+
else
108+
return false
109+
end
110+
end
111+
true
112+
end
113+
114+
s = 'A man, a plan, a canal: Panama'
115+
puts is_palindrome(s)
116+
# Output: true
117+
# Explanation: "amanaplanacanalpanama" is a palindrome.
118+
119+
s = 'race a car'
120+
puts is_palindrome(s)
121+
# Output: false
122+
# Explanation: "raceacar" is not a palindrome.
123+
124+
s = 'ab_a'
125+
puts is_palindrome(s)
126+
# Output: true
127+
# Explanation: "aba" is a palindrome.

0 commit comments

Comments
 (0)