-
Notifications
You must be signed in to change notification settings - Fork 44
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
Showing
4 changed files
with
76 additions
and
11 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
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
65 changes: 65 additions & 0 deletions
65
leetcode_java/src/main/java/LeetCodeJava/String/ValidPalindrome.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,65 @@ | ||
package LeetCodeJava.String; | ||
|
||
// https://leetcode.com/problems/valid-palindrome/description/?envType=list&envId=xoqag3yj | ||
|
||
/** | ||
* 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. | ||
* | ||
* | ||
* NOTE !!! | ||
* | ||
* "and removing all non-alphanumeric characters, " | ||
* -> so we need to consider both alphabet and numeric element | ||
*/ | ||
|
||
public class ValidPalindrome { | ||
|
||
// V0 | ||
public boolean isPalindrome(String s) { | ||
|
||
if (s == null || s.length() == 0){ | ||
return true; | ||
} | ||
|
||
String sUpper = ""; | ||
for (int idx = 0; idx < s.length(); idx++){ | ||
if (Character.isLetterOrDigit(s.charAt(idx))){ | ||
sUpper += String.valueOf(s.charAt(idx)).toUpperCase(); | ||
} | ||
} | ||
|
||
//System.out.println("sUpper = " + sUpper); | ||
//String sUpper = s.toUpperCase(); | ||
|
||
// 2 pointer | ||
int l = 0; | ||
int r = sUpper.length() - 1; | ||
while (r > l){ | ||
if (sUpper.charAt(l) != sUpper.charAt(r)){ | ||
return false; | ||
} | ||
l += 1; | ||
r -= 1; | ||
} | ||
return true; | ||
} | ||
|
||
// V1 | ||
// https://leetcode.com/problems/valid-palindrome/solutions/4787889/simple-solution-using-java/?envType=list&envId=xoqag3yj | ||
public boolean isPalindrome_1(String s) { | ||
String str=s.replaceAll("[^a-zA-Z0-9]","").toLowerCase(); | ||
StringBuilder sb=new StringBuilder(str); | ||
sb.reverse(); | ||
String ans=sb.toString(); | ||
for(int i=0;i<str.length();i++){ | ||
if(str.charAt(i)!=ans.charAt(i)){ | ||
return false; | ||
} | ||
} | ||
return true; | ||
} | ||
|
||
} |