Skip to content

Commit

Permalink
feat: add a solution for S0125
Browse files Browse the repository at this point in the history
  • Loading branch information
AnthonySchuijlenburg committed May 16, 2024
1 parent 6aad189 commit bd478a5
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ Contributions are welcome! If you have a more efficient solution or want to add
| 83 | Remove Duplicates from Sorted List | 0 ms | 43.5 MB | [Problem](src/main/java/com/anthonyschuijlenburg/S0083/Problem.md) |
| 88 | Merge Sorted Array | 0 ms | 41.8 MB | [Problem](src/main/java/com/anthonyschuijlenburg/S0088/Problem.md) |
| 94 | Binary Tree Inorder Traversal | 0 ms | 41.3 MB | [Problem](src/main/java/com/anthonyschuijlenburg/S0094/Problem.md) |
| 125 | Valid Palindrome <- Could improve | 15 ms | 44.8 MB | [Problem](src/main/java/com/anthonyschuijlenburg/S0125/Problem.md) |
| 206 | Reverse Linked List | 1 ms | 42.4 MB | [Problem](src/main/java/com/anthonyschuijlenburg/S0206/Problem.md) |
| 506 | Relative Ranks <- Needs work! | 27 ms | 45.4 MB | [Problem](src/main/java/com/anthonyschuijlenburg/S0506/Problem.md) |
| 786 | K-th Smallest Prime Fraction <- Needs work! | 672 ms | 106.5 MB | [Problem](src/main/java/com/anthonyschuijlenburg/S0786/Problem.md) |
Expand Down
32 changes: 32 additions & 0 deletions src/main/java/com/anthonyschuijlenburg/S0125/Problem.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
# 125. Valid Palindrome

Topics: Two Pointers , String

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.

Given a string s, return true if it is a palindrome, or false otherwise.

### 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.

### Example 3:

Input: s = " "
Output: true
Explanation: s is an empty string "" after removing non-alphanumeric characters.
Since an empty string reads the same forward and backward, it is a palindrome.

### Constraints:

1 <= s.length <= 2 * 105
s consists only of printable ASCII characters.

9 changes: 9 additions & 0 deletions src/main/java/com/anthonyschuijlenburg/S0125/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.anthonyschuijlenburg.S0125;

public class Solution {
public boolean isPalindrome(String s) {
s = s.replaceAll("[^a-zA-Z0-9]", "").toLowerCase();

return new StringBuilder(s).reverse().toString().equals(s);
}
}
41 changes: 41 additions & 0 deletions src/test/java/com/anthonyschuijlenburg/S0125/SolutionTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.anthonyschuijlenburg.S0125;

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

class SolutionTest {

@Test
void isPalindrome_testCase0() {
Solution solutionGenerator = new Solution();

boolean result = solutionGenerator.isPalindrome("");
assertTrue(result);
}

@Test
void isPalindrome_testCase1() {
Solution solutionGenerator = new Solution();

boolean result = solutionGenerator.isPalindrome("A man, a plan, a canal: Panama");
assertTrue(result);
}

@Test
void isPalindrome_testCase2() {
Solution solutionGenerator = new Solution();

boolean result = solutionGenerator.isPalindrome("racecar");
assertTrue(result);
}

@Test
void isPalindrome_testCase3() {
Solution solutionGenerator = new Solution();

boolean result = solutionGenerator.isPalindrome("race a car");
assertFalse(result);
}
}

0 comments on commit bd478a5

Please sign in to comment.