-
Notifications
You must be signed in to change notification settings - Fork 0
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
1 parent
7d9f506
commit d1d55b5
Showing
2 changed files
with
67 additions
and
0 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
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; | ||
} | ||
} |
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,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)); | ||
|
||
|
||
} | ||
} |