From c2bd60c2231b3ccbc385a4dd46d3e7ead10aab9b Mon Sep 17 00:00:00 2001 From: Jessica Kwok Date: Thu, 1 Apr 2021 09:31:12 -0700 Subject: [PATCH 1/6] Add palindrome challenge --- DIRECTORY.md | 1 + data_structures/arrays/strings/palindrome.rb | 44 ++++++++++++++++++++ 2 files changed, 45 insertions(+) create mode 100644 data_structures/arrays/strings/palindrome.rb diff --git a/DIRECTORY.md b/DIRECTORY.md index d0c4469..bca02fb 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -23,6 +23,7 @@ * Strings * [Anagram Checker](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/strings/anagram_checker.rb) * [Jewels And Stones](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/strings/jewels_and_stones.rb) + * [Palindrome](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/strings/palindrome.rb) * [Remove Vowels](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/strings/remove_vowels.rb) * [Two Sum](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/two_sum.rb) * [Two Sum Ii](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/two_sum_ii.rb) diff --git a/data_structures/arrays/strings/palindrome.rb b/data_structures/arrays/strings/palindrome.rb new file mode 100644 index 0000000..1ebcfbe --- /dev/null +++ b/data_structures/arrays/strings/palindrome.rb @@ -0,0 +1,44 @@ +# Challenge name: Valid Palindrome +# +# Given a string s, determine if it is a palindrome, +# considering only alphanumeric characters and ignoring cases. +# +# Example 1: +# Input: s = "A man, a plan, a canal: Panama" +# Output: true +# Explanation: "amanaplanacanalpanama" is a palindrome. +# +# Example 2: +# Input: s = "race a car" +# Output: false +# Explanation: "raceacar" is not a palindrome. +# +# Constraints: +# 1 <= s.length <= 2 * 105 +# s consists only of printable ASCII characters. +# @param {String} s +# @return {Boolean} + +# +# Approach 1: Brute Force +# +# Time Complexity: +# +def is_palindrome(s) + s.downcase +end + +s = 'A man, a plan, a canal: Panama' +puts is_palindrome(s) +# Output: true +# Explanation: "amanaplanacanalpanama" is a palindrome. + +s = 'race a car' +puts is_palindrome(s) +# Output: false +# Explanation: "raceacar" is not a palindrome. + +s = 'ab_a' +puts is_palindrome(s) +# Output: true +# Explanation: "aba" is a palindrome. \ No newline at end of file From 04a4c9aaedfac2f3b9145be39f1201362a053a0d Mon Sep 17 00:00:00 2001 From: Jessica Kwok Date: Mon, 5 Apr 2021 13:57:41 -0700 Subject: [PATCH 2/6] Add solution using .reverse --- data_structures/arrays/strings/palindrome.rb | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/data_structures/arrays/strings/palindrome.rb b/data_structures/arrays/strings/palindrome.rb index 1ebcfbe..03490ac 100644 --- a/data_structures/arrays/strings/palindrome.rb +++ b/data_structures/arrays/strings/palindrome.rb @@ -20,12 +20,13 @@ # @return {Boolean} # -# Approach 1: Brute Force +# Approach 1: Using Ruby method .reverse # -# Time Complexity: +# Time Complexity: O(n) # def is_palindrome(s) - s.downcase + letters_only = s.downcase.gsub(/[^0-9a-z]/i, '') + letters_only.reverse == letters_only end s = 'A man, a plan, a canal: Panama' From 6c7b37ab76b1a52c93205d91fb65a9d0413cc1db Mon Sep 17 00:00:00 2001 From: Jessica Kwok Date: Mon, 5 Apr 2021 14:08:23 -0700 Subject: [PATCH 3/6] Add solution using a reversed array --- data_structures/arrays/strings/palindrome.rb | 29 ++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/data_structures/arrays/strings/palindrome.rb b/data_structures/arrays/strings/palindrome.rb index 03490ac..4d70171 100644 --- a/data_structures/arrays/strings/palindrome.rb +++ b/data_structures/arrays/strings/palindrome.rb @@ -39,6 +39,35 @@ puts is_palindrome(s) # Output: false # Explanation: "raceacar" is not a palindrome. +s = 'ab_a' +puts is_palindrome(s) +# Output: true +# Explanation: "aba" is a palindrome. + +# +# Approach 2: Reversed array +# +# Time Complexity: O(n) +# +def is_palindrome(s) + letters_only_array = s.downcase.gsub(/[^0-9a-z]/i, '').split('') + reversed_array = [] + letters_only_array.each do |letter| + reversed_array.unshift(letter) + end + letters_only_array == reversed_array +end + +s = 'A man, a plan, a canal: Panama' +puts is_palindrome(s) +# Output: true +# Explanation: "amanaplanacanalpanama" is a palindrome. + +s = 'race a car' +puts is_palindrome(s) +# Output: false +# Explanation: "raceacar" is not a palindrome. + s = 'ab_a' puts is_palindrome(s) # Output: true From 7655003b6cc333ee1512a33f7c9fa0901974189d Mon Sep 17 00:00:00 2001 From: Jessica Kwok Date: Mon, 5 Apr 2021 14:31:57 -0700 Subject: [PATCH 4/6] Add solution using two pointers --- data_structures/arrays/strings/palindrome.rb | 36 ++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/data_structures/arrays/strings/palindrome.rb b/data_structures/arrays/strings/palindrome.rb index 4d70171..28e7921 100644 --- a/data_structures/arrays/strings/palindrome.rb +++ b/data_structures/arrays/strings/palindrome.rb @@ -68,6 +68,42 @@ puts is_palindrome(s) # Output: false # Explanation: "raceacar" is not a palindrome. +s = 'ab_a' +puts is_palindrome(s) +# Output: true +# Explanation: "aba" is a palindrome. + +# +# Approach 2: Two Pointers +# +# Time Complexity: O(n) +# +def is_palindrome(s) + letters_only = s.downcase.gsub(/[^0-9a-z]/i, '') + p1 = 0 + p2 = letters_only.length - 1 + + while p1 < p2 + if letters_only[p1] == letters_only[p2] + p1 += 1 + p2 -= 1 + else + return false + end + end + true +end + +s = 'A man, a plan, a canal: Panama' +puts is_palindrome(s) +# Output: true +# Explanation: "amanaplanacanalpanama" is a palindrome. + +s = 'race a car' +puts is_palindrome(s) +# Output: false +# Explanation: "raceacar" is not a palindrome. + s = 'ab_a' puts is_palindrome(s) # Output: true From bd6b1067c0b56d206e7c4a9307c57312289bc197 Mon Sep 17 00:00:00 2001 From: Vitor Oliveira Date: Fri, 9 Apr 2021 21:07:54 -0700 Subject: [PATCH 5/6] Update data_structures/arrays/strings/palindrome.rb --- data_structures/arrays/strings/palindrome.rb | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/data_structures/arrays/strings/palindrome.rb b/data_structures/arrays/strings/palindrome.rb index 28e7921..176243e 100644 --- a/data_structures/arrays/strings/palindrome.rb +++ b/data_structures/arrays/strings/palindrome.rb @@ -76,7 +76,14 @@ puts is_palindrome(s) # # Approach 2: Two Pointers # -# Time Complexity: O(n) + +# +# Complexity Analysis: +# +# Time Complexity: O(n), in length n of the string. We traverse over each +# character at most once until the two pointers meet in the middle, or when +# we break and return early. +# Space Complexity: O(1). No extra space required, at all. # def is_palindrome(s) letters_only = s.downcase.gsub(/[^0-9a-z]/i, '') @@ -107,4 +114,4 @@ puts is_palindrome(s) s = 'ab_a' puts is_palindrome(s) # Output: true -# Explanation: "aba" is a palindrome. \ No newline at end of file +# Explanation: "aba" is a palindrome. From 49db26c56be44d2ca3ab00034e264a1ae877604e Mon Sep 17 00:00:00 2001 From: Vitor Oliveira Date: Fri, 9 Apr 2021 21:07:57 -0700 Subject: [PATCH 6/6] Update data_structures/arrays/strings/palindrome.rb --- data_structures/arrays/strings/palindrome.rb | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/data_structures/arrays/strings/palindrome.rb b/data_structures/arrays/strings/palindrome.rb index 176243e..587e242 100644 --- a/data_structures/arrays/strings/palindrome.rb +++ b/data_structures/arrays/strings/palindrome.rb @@ -47,7 +47,17 @@ puts is_palindrome(s) # # Approach 2: Reversed array # -# Time Complexity: O(n) +# Complexity Analysis: +# +# Time Complexity: O(n), in length n of the string. +# +# We need to iterate through the string: When we filter out non-alphanumeric characters and convert the remaining +# characters to lower-case. When we reverse the string. When we compare the original and the reversed strings. +# Each iteration runs linearly in time (since each character operation completes in constant time). +# Thus, the effective run-time complexity is linear. +# +# Space Complexity: O(n), in length n of the string. We need O(n) additional +# space to store the filtered string and the reversed string. # def is_palindrome(s) letters_only_array = s.downcase.gsub(/[^0-9a-z]/i, '').split('')