|
| 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