Skip to content

Commit 6944cfd

Browse files
committed
feat: add two-pointer approach for isPalindrome function with detailed comments on complexity
1 parent 1acbb5e commit 6944cfd

File tree

2 files changed

+73
-3
lines changed

2 files changed

+73
-3
lines changed

valid-palindrome/hongseoupyun.py

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,44 @@
1+
2+
import re
3+
4+
class Solution:
15
# Palindrome is a word, phrase, number, or other sequences of characters that reads the same forward and backward (ignoring non-alphanumeric characters - letters and number).
26
# the solution should first filter out non-alphanumeric characters and convert the string to lowercase.
37
# Then, it should check if the cleaned string is equal to its reverse.
48

59
# Time complexity = O(n), as it checks, replaces, converts to lowercase, and reverses all characters in the string
610
# Space complexity = O(n), as it creates a new string with a maximum length equal to the original string
7-
import re
8-
9-
class Solution:
1011
def isPalindrome(self, s: str) -> bool:
1112
filtered_string = re.sub(r'[^a-zA-Z0-9]','',s).lower()
1213
reversed_string = filtered_string[::-1]
1314
return reversed_string == filtered_string
15+
16+
17+
# Using Two-pointer; It searches from given string directly so the Space complexity is O(n) - better method
18+
# By using two index, searchs and compares from start and end of the string like this -><-
19+
# Do this while start index is less than end index
20+
# Skip non-alphanumeric characters
21+
# If both pointer are in characters, then convert to lowercase and compare
22+
# If not equal, return false
23+
# If all characters are equal, return true
24+
25+
# Time complexity = O(n), as it checks and converts to lowercase all characters in the string
26+
# Space complexity = O(1), as it uses only a constant amount of extra space
27+
def isPalindromeTwoPointer(self, s: str) -> bool:
28+
left = 0
29+
right = len(s) - 1
30+
31+
while left < right:
32+
if not s[left].isalnum():
33+
left+=1
34+
continue
35+
if not s[right].isalnum():
36+
right-=1
37+
continue
38+
if s[left].lower() != s[right].lower():
39+
return False
40+
41+
left+=1
42+
right-=1
43+
44+
return True

valid-palindrome/hongseoupyun.ts

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,42 @@ function isPalindrome(s: string): boolean {
1515
return false
1616
}
1717
};
18+
19+
//Palindrom example: "A man, a plan, a canal: Panama"
20+
21+
// Using Two-pointer; It searches from given string directly so the Space complexity is O(n) - better method
22+
// By using two index, searchs and compares from start and end of the string like this-><-
23+
// Do this while start index is less than end index
24+
// Skip non-alphanumeric characters
25+
// If both pointer are in characters, then convert to lowercase and compare
26+
// If not equal, return false
27+
// If all characters are equal, return true
28+
29+
// Time complexity = O(n), as it checks and converts to lowercase all characters in the string
30+
// Space complexity = O(1), as it uses only a constant amount of extra space
31+
function isPalindrome2(s: string): boolean {
32+
33+
let left: number = 0;
34+
let right: number = s.length - 1;
35+
36+
let isAlphaNum = (str: string): boolean => {
37+
return /^[a-zA-Z0-9]$/.test(str)
38+
}
39+
40+
while (left < right) {
41+
if (!isAlphaNum(s[left])) {
42+
left++
43+
continue
44+
}
45+
if (!isAlphaNum(s[right])) {
46+
right--
47+
continue
48+
}
49+
if (s[left].toLowerCase() !== s[right].toLowerCase()) {
50+
return false
51+
}
52+
left++
53+
right--
54+
}
55+
return true
56+
};

0 commit comments

Comments
 (0)