-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
AnthonySchuijlenburg
committed
May 16, 2024
1 parent
6aad189
commit bd478a5
Showing
4 changed files
with
83 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
41
src/test/java/com/anthonyschuijlenburg/S0125/SolutionTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |