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.