Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // Question at:
- // https://www.facebook.com/groups/Javagroup123/permalink/10156296803156664/
- import junit.framework.TestCase;
- public class DeletePanendromeTest extends TestCase {
- public boolean validPalindrome(String s) {
- int len = s.length();
- int left = 0,right = len-1;
- while(left < right){
- if(s.charAt(left) == s.charAt(right)){
- left++;
- right--;
- }else{
- return isPalindrome(s.substring(0,left) + s.substring(left+1,len)) ||
- isPalindrome(s.substring(0,right) + s.substring(right+1,len));
- }
- }
- return true;
- }
- private boolean isPalindrome(String s){
- int left = 0,right = s.length()-1;
- while(left < right){
- if(s.charAt(left++) != s.charAt(right--)) return false;
- }
- return true;
- }
- public void testEmpty() {
- assertPalendromeResultEquals("Delete no characters; is still a palendrome;", true, "");
- }
- public void testOneChar() {
- assertPalendromeResultEquals("Do nothing or delete one to get empty string;", true, "x");
- }
- public void test_xy() {
- assertPalendromeResultEquals("Delete either char;", true, "xy");
- }
- public void test_xx() {
- assertPalendromeResultEquals("Do nothing or delete either char;", true, "xx");
- }
- public void test_xyz() {
- assertPalendromeResultEquals("Can't be made a palendrome;", false, "xyz");
- }
- public void test_xyx() {
- assertPalendromeResultEquals("Do nothing or delete middle char;", true, "xyx");
- }
- public void test_xyy() {
- assertPalendromeResultEquals("Delete 1st char;", true, "xyy");
- }
- public void test_xxy() {
- assertPalendromeResultEquals("Delete last char;", true, "xxy");
- }
- public void test_abba() {
- assertPalendromeResultEquals("Do nothing or delete either 'b';", true, "abba");
- }
- public void test_abcba() {
- assertPalendromeResultEquals("Do nothing or delete the center 'c';", true, "abcba");
- }
- public void test_abccba() {
- assertPalendromeResultEquals("Do nothing or delete either center 'c';", true, "abccba");
- }
- public void test_abaaaaafa() {
- assertPalendromeResultEquals("Can't delete both 'b' and 'f';", false, "abaaaaafa");
- }
- public void test_ababababab() {
- assertPalendromeResultEquals("Delete first or last char;", true, "ababababab");
- }
- public void test_abababxbabab() {
- assertPalendromeResultEquals("Must delete first char;", true, "abababxbabab");
- }
- public void test_ababaxababab() {
- assertPalendromeResultEquals("Must delete last char;", true, "ababaxababab");
- }
- public void test_abcdedcbfa() {
- assertPalendromeResultEquals("Delete the 'f';", true, "abcdedcbfa");
- }
- void assertPalendromeResultEquals(final String message, final boolean expected, final String inputString) {
- if (isPalindrome(inputString)) {
- final String doNothingPrefix = "Do nothing or ";
- assertTrue("Expecting the message <" + message + "> to start with the text <" + doNothingPrefix + ">;",
- message.startsWith(doNothingPrefix) || inputString.length() == 0);
- }
- assertEquals(message, expected, validPalindrome(inputString));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement