From bd478a56dd5aa791e64bf810209b7efcf85a4a95 Mon Sep 17 00:00:00 2001 From: AnthonySchuijlenburg Date: Thu, 16 May 2024 11:34:01 +0200 Subject: [PATCH] feat: add a solution for S0125 --- README.md | 1 + .../com/anthonyschuijlenburg/S0125/Problem.md | 32 +++++++++++++++ .../anthonyschuijlenburg/S0125/Solution.java | 9 ++++ .../S0125/SolutionTest.java | 41 +++++++++++++++++++ 4 files changed, 83 insertions(+) create mode 100644 src/main/java/com/anthonyschuijlenburg/S0125/Problem.md create mode 100644 src/main/java/com/anthonyschuijlenburg/S0125/Solution.java create mode 100644 src/test/java/com/anthonyschuijlenburg/S0125/SolutionTest.java diff --git a/README.md b/README.md index 5685435..abb85a9 100644 --- a/README.md +++ b/README.md @@ -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) | diff --git a/src/main/java/com/anthonyschuijlenburg/S0125/Problem.md b/src/main/java/com/anthonyschuijlenburg/S0125/Problem.md new file mode 100644 index 0000000..43312db --- /dev/null +++ b/src/main/java/com/anthonyschuijlenburg/S0125/Problem.md @@ -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. + diff --git a/src/main/java/com/anthonyschuijlenburg/S0125/Solution.java b/src/main/java/com/anthonyschuijlenburg/S0125/Solution.java new file mode 100644 index 0000000..3b2cf1e --- /dev/null +++ b/src/main/java/com/anthonyschuijlenburg/S0125/Solution.java @@ -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); + } +} diff --git a/src/test/java/com/anthonyschuijlenburg/S0125/SolutionTest.java b/src/test/java/com/anthonyschuijlenburg/S0125/SolutionTest.java new file mode 100644 index 0000000..5f136a2 --- /dev/null +++ b/src/test/java/com/anthonyschuijlenburg/S0125/SolutionTest.java @@ -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); + } +} \ No newline at end of file