mirror of
https://github.com/TheAlgorithms/Ruby
synced 2025-01-18 10:26:54 +01:00
Add palindrome challenge
This commit is contained in:
parent
a472f9a6ff
commit
c2bd60c223
2 changed files with 45 additions and 0 deletions
|
@ -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)
|
||||
|
|
44
data_structures/arrays/strings/palindrome.rb
Normal file
44
data_structures/arrays/strings/palindrome.rb
Normal file
|
@ -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.
|
Loading…
Reference in a new issue