Skip to content

Commit

Permalink
ValidPalindrome
Browse files Browse the repository at this point in the history
  • Loading branch information
shamstabrez16 committed Oct 17, 2023
1 parent 7d9f506 commit d1d55b5
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 0 deletions.
29 changes: 29 additions & 0 deletions src/main/java/String/ValidPalindrome.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package String;

public class ValidPalindrome {
public boolean isPalindrome(String s) {
if(s.isEmpty()){
return true;
}
int l = 0;
int r = s.length() - 1;
while (l<=r){
char leftChar = s.charAt(l);
char rightChar = s.charAt(r);
if(!Character.isLetterOrDigit(leftChar)){
l++;
}
else if(!Character.isLetterOrDigit(rightChar)){
r--;
}
else{
if(Character.toLowerCase(leftChar)!= Character.toLowerCase(rightChar)){
return false;
}
l++;
r--;
}
}
return true;
}
}
38 changes: 38 additions & 0 deletions src/test/java/TestValidPalindrome.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import org.junit.Assert;
import org.junit.Before;
import String.ValidPalindrome;
import org.junit.Test;

public class TestValidPalindrome {
private ValidPalindrome validPalindrome;
@Before
public void setUp(){
validPalindrome = new ValidPalindrome();
}

@Test
public void TestCase1(){
String s = "A man, a plan, a canal: Panama";
Assert.assertTrue(validPalindrome.isPalindrome(s));

}
@Test
public void TestCase2(){
String s = " ";
Assert.assertTrue(validPalindrome.isPalindrome(s));
}
@Test
public void TestCase3(){
String s = "race a car";
Assert.assertFalse(validPalindrome.isPalindrome(s));


}
@Test
public void TestCase4(){
String s = "0P";
Assert.assertFalse(validPalindrome.isPalindrome(s));


}
}

0 comments on commit d1d55b5

Please sign in to comment.