Skip to content

Commit 9441d80

Browse files
solves valid palindrome
1 parent 2f1f51a commit 9441d80

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,8 @@
4343
| 118 | [Pascal's Triangle](https://leetcode.com/problems/pascals-triangle) | Easy | [![Java](https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png)](https://github.com/anishLearnsToCode/leetcode-algorithms/blob/master/src/PascalsTriangle.java) |
4444
| 119 | [Pascal's Triangle II](https://leetcode.com/problems/pascals-triangle-ii) | Easy | [![Java](https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png)](https://github.com/anishLearnsToCode/leetcode-algorithms/blob/master/src/PascalsTriangleII.java) |
4545
| 121 | [Best Time to Buy and Sell Stocks](https://leetcode.com/problems/best-time-to-buy-and-sell-stock) | Easy | [![Java](https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png)](https://github.com/anishLearnsToCode/leetcode-algorithms/blob/master/src/BestTimeToBuyAndSellStock.java) |
46-
| 122 | [Best Time to Buy and Sell Stocks II](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii) | Easy |
47-
| 125 | [Valid Palindrome](https://leetcode.com/problems/valid-palindrome) | Easy |
46+
| 122 | [Best Time to Buy and Sell Stocks II](https://leetcode.com/problems/best-time-to-buy-and-sell-stock-ii) | Easy | [![Java](https://img.icons8.com/color/40/000000/java-coffee-cup-logo.png)](https://github.com/anishLearnsToCode/leetcode-algorithms/blob/master/src/BestTimeToBuyAndSellStockII.java) |
47+
| 125 | [Valid Palindrome](https://leetcode.com/problems/valid-palindrome) | Easy |
4848
| 136 | [Single Number](https://leetcode.com/problems/single-number) | Easy |
4949
| 141 | [Linked List Cycle](https://leetcode.com/problems/linked-list-cycle) | Easy |
5050
| 155 | [Min Stack](https://leetcode.com/problems/min-stack) | Easy |

src/ValidPalindrome.java

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
public class ValidPalindrome {
2+
public boolean isPalindrome(String string) {
3+
final StringBuilder alphanumeric = toAlphaNumeric(string);
4+
for (int index = 0 ; index < alphanumeric.length() / 2 ; index++) {
5+
if (alphanumeric.charAt(index) != alphanumeric.charAt(alphanumeric.length() - index - 1)) {
6+
return false;
7+
}
8+
}
9+
return true;
10+
}
11+
12+
private static StringBuilder toAlphaNumeric(String string) {
13+
StringBuilder result = new StringBuilder();
14+
for (int index = 0 ; index < string.length() ; index++) {
15+
char character = string.charAt(index);
16+
if (Character.isAlphabetic(character) || Character.isDigit(character)) {
17+
result.append(Character.toLowerCase(character));
18+
}
19+
}
20+
return result;
21+
}
22+
}

0 commit comments

Comments
 (0)