Skip to content

Commit

Permalink
feat: add valid palindrome solution
Browse files Browse the repository at this point in the history
  • Loading branch information
sandrig committed Jul 25, 2023
1 parent ae39686 commit 2b0d5b7
Showing 1 changed file with 36 additions and 0 deletions.
36 changes: 36 additions & 0 deletions typescript/src/validPalindrome/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ Since an empty string reads the same forward and backward, it is a palindrome.
## Solution

```typescript
// Solution 1
function isPalindrome(s: string): boolean {
// Remove all characters except letters and numbers and convert to lowercase
const cleanedString = s.replace(/[^a-zA-Z0-9]/g, "").toLowerCase();
Expand All @@ -53,6 +54,41 @@ function isPalindrome(s: string): boolean {

return true;
}

// Solution 2
function isPalindrome(s: string): boolean {
// Function to check if a character is a letter or a digit
function isAlphanumeric(char: string): boolean {
const charCode = char.charCodeAt(0);
return (
(charCode >= 48 && charCode <= 57) || // Numbers 0-9
(charCode >= 65 && charCode <= 90) || // Letters A-Z
(charCode >= 97 && charCode <= 122) // Letters a-z
);
}

// Remove all characters except letters and numbers and convert the string to lowercase
let cleanedString = "";
for (let i = 0; i < s.length; i++) {
if (isAlphanumeric(s[i])) {
cleanedString += s[i].toLowerCase();
}
}

// Checking if the string being cleared is a palindrome
let left = 0;
let right = cleanedString.length - 1;

while (left < right) {
if (cleanedString[left] !== cleanedString[right]) {
return false;
}
left++;
right--;
}

return true;
}
```

## Complexity Analysis
Expand Down

0 comments on commit 2b0d5b7

Please sign in to comment.