Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 085d4e1

Browse files
committed
Add valid palindrome solution
1 parent 51bf540 commit 085d4e1

File tree

4 files changed

+136
-3
lines changed

4 files changed

+136
-3
lines changed

‎.gitignore‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ cython_debug/
165165
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
166166
# and can be added to the global gitignore or merged into this file. For a more nuclear
167167
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
168-
#.idea/
168+
.idea/
169169

170170
# Abstra
171171
# Abstra is an AI-powered process automation framework.

‎125_Valid_Palindrome/README.md‎

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
# Valid Palindrome
2+
3+
## Problem Description
4+
A phrase is a palindrome if, after converting all uppercase letters into lowercase letters and removing all non-alphanumeric characters, it reads the same forward and backward. Alphanumeric characters include letters and numbers.
5+
6+
Given a string `s`, return `True` if it is a palindrome, or `False` otherwise.
7+
8+
**Example 1:**
9+
Input: `s = "A man, a plan, a canal: Panama"`
10+
Output: `true`
11+
Explanation: "amanaplanacanalpanama" is a valid palindrome.
12+
13+
**Example 2:**
14+
Input: `s = "race a car"`
15+
Output: `false`
16+
Explanation: "raceacar" is not a palindrome.
17+
18+
## Solutions
19+
20+
### Solution 1: String Comparison
21+
22+
```python
23+
def is_palindrome(s: str) -> bool:
24+
s = ''.join([char.lower() for char in s if char.isalnum()])
25+
return s == s[::-1]
26+
```
27+
28+
* **Time Complexity:** O(n)
29+
* Linear pass to filter/convert characters
30+
* O(n) string reversal comparison
31+
32+
* **Space Complexity:** O(n)
33+
* Stores cleaned version of input string
34+
35+
### Solution 2: Two Pointers (In-Place)
36+
37+
```pethon
38+
def is_palindrome2(s: str) -> bool:
39+
left = 0
40+
right = len(s) - 1
41+
42+
while left < right:
43+
while (left < right) and not s[left].isalnum():
44+
left += 1
45+
while (left < right) and not s[right].isalnum():
46+
right -= 1
47+
if s[left].lower() != s[right].lower():
48+
return False
49+
50+
left += 1
51+
right -= 1
52+
53+
return True
54+
```
55+
56+
* **Time Complexity:** O(n)
57+
* Each character processed at most twice (once per pointer)
58+
* **Space Complexity:** O(1)
59+
* Operates directly on input with pointer variables
60+
61+
### Implementation Notes for Two-Pointer Solution
62+
63+
* **Case Conversion is Critical**
64+
65+
Always convert compared characters to lowercase using .lower() before comparison.
66+
Example: 'A' vs 'a' should be treated as equal.
67+
* **Update Pointers After Comparison**
68+
69+
Remember to increment left and decrement right at the end of the loop.
70+
Failure to update pointers causes infinite loops.

‎125_Valid_Palindrome/solution.py‎

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
def is_palindrome(s: str) -> bool:
2+
"""
3+
First solution. Time complexity is O(n), memory complexity is O(n).
4+
Creates a cleaned string (lowercase alphanumeric characters) and checks if it reads the same forwards and backwards.
5+
:param s: Input string to check
6+
:return: True if the string is a valid palindrome, False otherwise
7+
"""
8+
s = ''.join([char.lower() for char in s if char.isalnum()])
9+
return s == s[::-1]
10+
11+
assert is_palindrome("A man, a plan, a canal: Panama") == True, 'Test 1 failed'
12+
assert is_palindrome("race a car") == False, 'Test 2 failed'
13+
assert is_palindrome(" ") == True, 'Test 3 failed'
14+
15+
def is_palindrome2(s: str) -> bool:
16+
"""
17+
Second solution. Time complexity is O(n), memory complexity is O(1).
18+
Uses two pointers to compare characters from both ends, skipping non-alphanumeric characters.
19+
Case-insensitive comparison is performed for alphanumeric characters.
20+
21+
:param s: Input string to check
22+
:return: True if the string is a valid palindrome, False otherwise
23+
"""
24+
left = 0
25+
right = len(s) - 1
26+
27+
while left < right:
28+
while (left < right) and not s[left].isalnum():
29+
left += 1
30+
while (left < right) and not s[right].isalnum():
31+
right -= 1
32+
if s[left].lower() != s[right].lower():
33+
return False
34+
35+
left += 1
36+
right -= 1
37+
38+
return True
39+
40+
assert is_palindrome2("A man, a plan, a canal: Panama") == True, 'Test 1 failed'
41+
assert is_palindrome2("race a car") == False, 'Test 2 failed'
42+
assert is_palindrome2(" ") == True, 'Test 3 failed'

‎README.md‎

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,23 @@
1-
# leetcode-interviews
2-
Leetcode tasks from my interviews
1+
# LeetCode Interview Solutions
2+
3+
A curated collection of algorithmic solutions to problems encountered during technical interviews at various companies. This repository serves as both a personal reference and a resource for others preparing for coding interviews.
4+
5+
## 📁 Repository Structure
6+
7+
Solutions are organized by problem name. Each solution includes:
8+
9+
- Implementation in one or more programming languages
10+
- Brief problem description
11+
- Complexity analysis
12+
- Key insights/approach
13+
14+
## 🔍 Problem Format
15+
16+
Each problem directory contains:
17+
18+
* `README.md` with:
19+
* Problem statement
20+
* Example I/O
21+
* Approach explanation
22+
* Time & space complexity
23+
* Solution files (`solution.{ext}`)

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /