Skip to content

Commit 97978b3

Browse files
committed
feat: valid-palindrome
1 parent d105720 commit 97978b3

File tree

1 file changed

+12
-15
lines changed

1 file changed

+12
-15
lines changed

valid-palindrome/minji-go.java

+12-15
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,19 @@
1-
/*
2-
Problem: https://leetcode.com/problems/valid-palindrome/
3-
Description: return true if it is a palindrome, alphanumeric characters(letters and numbers) reads the same forward and backward
4-
Concept: Two Pointers, String
5-
Time Complexity: O(n), Runtime: 10ms
6-
Space Complexity: O(n), Memory: 58.6MB
7-
*/
1+
/**
2+
* <a href="https://leetcode.com/problems/valid-palindrome/">week03-1.valid-palindrome</a>
3+
* <li>Description: return true if it is a palindrome </li>
4+
* <li>Topics: Two Pointers, String </li>
5+
* <li>Time Complexity: O(N), Runtime 13ms </li>
6+
* <li>Space Complexity: O(N), Memory 45.55MB </li>
7+
*/
88
class Solution {
99
public boolean isPalindrome(String s) {
10-
String regex ="[^A-Za-z0-9]";
11-
String palindrome = s.replaceAll(regex,"").toLowerCase(); //replaceAll(), toLowerCase(): O(n)
10+
String str = s.toLowerCase().replaceAll("[^0-9a-z]", "");
1211

13-
boolean answer = true;
14-
for(int i=0; i<palindrome.length()/2; i++){
15-
if(palindrome.charAt(i) != palindrome.charAt(palindrome.length()-1-i)) {
16-
answer = false;
17-
break;
12+
for (int i = 0; i < str.length() / 2; i++) {
13+
if (str.charAt(i) != str.charAt(str.length() - 1 - i)) {
14+
return false;
1815
}
1916
}
20-
return answer;
17+
return true;
2118
}
2219
}

0 commit comments

Comments
 (0)