From 66859e84916d225b0b13d8dfcaa05f64bf8e9d30 Mon Sep 17 00:00:00 2001 From: Jessica Kwok Date: Thu, 24 Jun 2021 13:37:00 -0700 Subject: [PATCH 1/4] Add palindrome challenge --- DIRECTORY.md | 1 + .../arrays/strings/palindrome_checker.rb | 20 +++++++++++++++++++ 2 files changed, 21 insertions(+) create mode 100644 data_structures/arrays/strings/palindrome_checker.rb diff --git a/DIRECTORY.md b/DIRECTORY.md index d0c4469..f2dae76 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 Checker](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_checker.rb b/data_structures/arrays/strings/palindrome_checker.rb new file mode 100644 index 0000000..75666a7 --- /dev/null +++ b/data_structures/arrays/strings/palindrome_checker.rb @@ -0,0 +1,20 @@ +# Challenge name: Palindrome II +# +# Given a string s, return true if the s can be palindrome after deleting at most one character from it. +# +# Example 1: +# Input: s = "aba" +# Output: true +# +# Example 2: +# Input: s = "abca" +# Output: true +# Explanation: You could delete the character 'c'. +# +# Example 3: +# Input: s = "abc" +# Output: false +# +# Constraints: +# 1 <= s.length <= 105 +# s consists of lowercase English letters. \ No newline at end of file From 6c72cd24a1be64cb00ed9edc4c5b8370772df970 Mon Sep 17 00:00:00 2001 From: Jessica Kwok Date: Tue, 29 Jun 2021 14:00:08 -0700 Subject: [PATCH 2/4] Add two pointers approach --- DIRECTORY.md | 2 +- .../strings/almost_palindrome_checker.rb | 59 +++++++++++++++++++ .../arrays/strings/palindrome_checker.rb | 20 ------- 3 files changed, 60 insertions(+), 21 deletions(-) create mode 100644 data_structures/arrays/strings/almost_palindrome_checker.rb delete mode 100644 data_structures/arrays/strings/palindrome_checker.rb diff --git a/DIRECTORY.md b/DIRECTORY.md index f2dae76..5b1cf15 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -21,9 +21,9 @@ * [Single Number](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/single_number.rb) * [Sort Squares Of An Array](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/sort_squares_of_an_array.rb) * Strings + * [Almost Palindrome Checker](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/strings/almost_palindrome_checker.rb) * [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 Checker](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/almost_palindrome_checker.rb b/data_structures/arrays/strings/almost_palindrome_checker.rb new file mode 100644 index 0000000..67c644e --- /dev/null +++ b/data_structures/arrays/strings/almost_palindrome_checker.rb @@ -0,0 +1,59 @@ +# Challenge name: Almost Palindrome +# +# Given a string s, return true if the s can be palindrome after deleting at most one character from it. +# +# Example 1: +# Input: s = "aba" +# Output: true +# +# Example 2: +# Input: s = "abca" +# Output: true +# Explanation: You could delete the character 'c'. +# +# Example 3: +# Input: s = "abc" +# Output: false +# +# Constraints: +# 1 <= s.length <= 105 +# s consists of lowercase English letters. + +# Approach 1: Two Pointers +# Time Complexity: O(n) + +def almost_palindrome_checker(string) + p1 = 0 + p2 = string.length - 1 + + while p1 < p2 + if string.split('')[p1] != string.split('')[p2] + return palindrome_checker(string, p1, p2 - 1) || palindrome_checker(string, p1 + 1, p2) + end + p1 += 1 + p2 -= 1 + end + + true +end + +def palindrome_checker(string, p1, p2) + while p1 < p2 + if string.split('')[p1] != string.split('')[p2] + return false + end + p1 += 1 + p2 -= 1 + end + + true +end + +puts almost_palindrome_checker('aba') +# => true + +puts almost_palindrome_checker('abca') +# => true + +puts almost_palindrome_checker('abc') +# => false \ No newline at end of file diff --git a/data_structures/arrays/strings/palindrome_checker.rb b/data_structures/arrays/strings/palindrome_checker.rb deleted file mode 100644 index 75666a7..0000000 --- a/data_structures/arrays/strings/palindrome_checker.rb +++ /dev/null @@ -1,20 +0,0 @@ -# Challenge name: Palindrome II -# -# Given a string s, return true if the s can be palindrome after deleting at most one character from it. -# -# Example 1: -# Input: s = "aba" -# Output: true -# -# Example 2: -# Input: s = "abca" -# Output: true -# Explanation: You could delete the character 'c'. -# -# Example 3: -# Input: s = "abc" -# Output: false -# -# Constraints: -# 1 <= s.length <= 105 -# s consists of lowercase English letters. \ No newline at end of file From b40fe95c437a08ba908cfcd9581204eddcde751f Mon Sep 17 00:00:00 2001 From: Jessica Kwok Date: Fri, 2 Jul 2021 12:45:20 -0700 Subject: [PATCH 3/4] Extract split method from loop --- .../arrays/strings/almost_palindrome_checker.rb | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/data_structures/arrays/strings/almost_palindrome_checker.rb b/data_structures/arrays/strings/almost_palindrome_checker.rb index 67c644e..75e2212 100644 --- a/data_structures/arrays/strings/almost_palindrome_checker.rb +++ b/data_structures/arrays/strings/almost_palindrome_checker.rb @@ -25,10 +25,11 @@ def almost_palindrome_checker(string) p1 = 0 p2 = string.length - 1 + array = string.split('') while p1 < p2 - if string.split('')[p1] != string.split('')[p2] - return palindrome_checker(string, p1, p2 - 1) || palindrome_checker(string, p1 + 1, p2) + if array[p1] != array[p2] + return palindrome_checker(array, p1, p2 - 1) || palindrome_checker(array, p1 + 1, p2) end p1 += 1 p2 -= 1 @@ -37,9 +38,9 @@ def almost_palindrome_checker(string) true end -def palindrome_checker(string, p1, p2) +def palindrome_checker(array, p1, p2) while p1 < p2 - if string.split('')[p1] != string.split('')[p2] + if array[p1] != array[p2] return false end p1 += 1 From a0952eb7b2b224559214d1e124f83092b99da5d1 Mon Sep 17 00:00:00 2001 From: Vitor Oliveira Date: Fri, 2 Jul 2021 12:52:55 -0700 Subject: [PATCH 4/4] add space complexity --- .../arrays/strings/almost_palindrome_checker.rb | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/data_structures/arrays/strings/almost_palindrome_checker.rb b/data_structures/arrays/strings/almost_palindrome_checker.rb index 75e2212..0115ec3 100644 --- a/data_structures/arrays/strings/almost_palindrome_checker.rb +++ b/data_structures/arrays/strings/almost_palindrome_checker.rb @@ -19,8 +19,15 @@ # 1 <= s.length <= 105 # s consists of lowercase English letters. +# # Approach 1: Two Pointers +# + +# Complexity Analysis: +# # Time Complexity: O(n) +# Space Complexity: O(1) + def almost_palindrome_checker(string) p1 = 0 @@ -57,4 +64,4 @@ puts almost_palindrome_checker('abca') # => true puts almost_palindrome_checker('abc') -# => false \ No newline at end of file +# => false