mirror of
https://github.com/TheAlgorithms/Ruby
synced 2025-02-06 08:46:04 +01:00
Merge branch 'TheAlgorithms:master' into master
This commit is contained in:
commit
1f36e635a6
3 changed files with 70 additions and 0 deletions
|
@ -36,6 +36,7 @@
|
||||||
* [Sort Squares Of An Array](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/sort_squares_of_an_array.rb)
|
* [Sort Squares Of An Array](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/sort_squares_of_an_array.rb)
|
||||||
* [Sorted Arrays Intersection](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/sorted_arrays_intersection.rb)
|
* [Sorted Arrays Intersection](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/sorted_arrays_intersection.rb)
|
||||||
* Strings
|
* 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)
|
* [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)
|
* [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)
|
* [Palindrome](https://github.com/TheAlgorithms/Ruby/blob/master/data_structures/arrays/strings/palindrome.rb)
|
||||||
|
|
|
@ -3,6 +3,8 @@
|
||||||
[![Gitter chat](https://img.shields.io/badge/Chat-Gitter-ff69b4.svg?label=Chat&logo=gitter&style=flat-square)](https://gitter.im/TheAlgorithms)
|
[![Gitter chat](https://img.shields.io/badge/Chat-Gitter-ff69b4.svg?label=Chat&logo=gitter&style=flat-square)](https://gitter.im/TheAlgorithms)
|
||||||
[![contributions welcome](https://img.shields.io/static/v1.svg?label=Contributions&message=Welcome&color=0059b3&style=flat-square)](https://github.com/TheAlgorithms/Ruby/blob/master/CONTRIBUTING.md)
|
[![contributions welcome](https://img.shields.io/static/v1.svg?label=Contributions&message=Welcome&color=0059b3&style=flat-square)](https://github.com/TheAlgorithms/Ruby/blob/master/CONTRIBUTING.md)
|
||||||
![](https://img.shields.io/github/repo-size/TheAlgorithms/Ruby.svg?label=Repo%20size&style=flat-square)
|
![](https://img.shields.io/github/repo-size/TheAlgorithms/Ruby.svg?label=Repo%20size&style=flat-square)
|
||||||
|
[![Discord chat](https://img.shields.io/discord/808045925556682782.svg?logo=discord&colorB=7289DA&style=flat-square)](https://discord.gg/c7MnfGFGa6)
|
||||||
|
|
||||||
### All algorithms implemented in Ruby (for education)
|
### All algorithms implemented in Ruby (for education)
|
||||||
|
|
||||||
These implementations are for learning purposes only. Therefore they may be less efficient than the implementations in the Ruby standard library.
|
These implementations are for learning purposes only. Therefore they may be less efficient than the implementations in the Ruby standard library.
|
||||||
|
|
67
data_structures/arrays/strings/almost_palindrome_checker.rb
Normal file
67
data_structures/arrays/strings/almost_palindrome_checker.rb
Normal file
|
@ -0,0 +1,67 @@
|
||||||
|
# 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
|
||||||
|
#
|
||||||
|
|
||||||
|
# Complexity Analysis:
|
||||||
|
#
|
||||||
|
# Time Complexity: O(n)
|
||||||
|
# Space Complexity: O(1)
|
||||||
|
|
||||||
|
|
||||||
|
def almost_palindrome_checker(string)
|
||||||
|
p1 = 0
|
||||||
|
p2 = string.length - 1
|
||||||
|
array = string.split('')
|
||||||
|
|
||||||
|
while p1 < p2
|
||||||
|
if array[p1] != array[p2]
|
||||||
|
return palindrome_checker(array, p1, p2 - 1) || palindrome_checker(array, p1 + 1, p2)
|
||||||
|
end
|
||||||
|
p1 += 1
|
||||||
|
p2 -= 1
|
||||||
|
end
|
||||||
|
|
||||||
|
true
|
||||||
|
end
|
||||||
|
|
||||||
|
def palindrome_checker(array, p1, p2)
|
||||||
|
while p1 < p2
|
||||||
|
if array[p1] != array[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
|
Loading…
Add table
Reference in a new issue