Advertisement
JeffGrigg

DeletePanendromeTest

Apr 24th, 2018
414
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 3.43 KB | None | 0 0
  1. // Question at:
  2. //  https://www.facebook.com/groups/Javagroup123/permalink/10156296803156664/
  3.  
  4. import junit.framework.TestCase;
  5.  
  6. public class DeletePanendromeTest extends TestCase {
  7.  
  8.     public boolean validPalindrome(String s) {
  9.         int len = s.length();
  10.         int left = 0,right = len-1;
  11.  
  12.         while(left < right){
  13.             if(s.charAt(left) == s.charAt(right)){
  14.                 left++;
  15.                 right--;
  16.             }else{
  17.                 return isPalindrome(s.substring(0,left) + s.substring(left+1,len)) ||
  18.                         isPalindrome(s.substring(0,right) + s.substring(right+1,len));
  19.             }
  20.         }
  21.  
  22.  
  23.         return true;
  24.     }
  25.  
  26.     private boolean isPalindrome(String s){
  27.         int left = 0,right = s.length()-1;
  28.         while(left < right){
  29.             if(s.charAt(left++) != s.charAt(right--)) return false;
  30.         }
  31.         return true;
  32.     }
  33.  
  34.     public void testEmpty() {
  35.         assertPalendromeResultEquals("Delete no characters; is still a palendrome;", true, "");
  36.     }
  37.  
  38.     public void testOneChar() {
  39.         assertPalendromeResultEquals("Do nothing or delete one to get empty string;", true, "x");
  40.     }
  41.  
  42.     public void test_xy() {
  43.         assertPalendromeResultEquals("Delete either char;", true, "xy");
  44.     }
  45.  
  46.     public void test_xx() {
  47.         assertPalendromeResultEquals("Do nothing or delete either char;", true, "xx");
  48.     }
  49.  
  50.     public void test_xyz() {
  51.         assertPalendromeResultEquals("Can't be made a palendrome;", false, "xyz");
  52.     }
  53.  
  54.     public void test_xyx() {
  55.         assertPalendromeResultEquals("Do nothing or delete middle char;", true, "xyx");
  56.     }
  57.  
  58.     public void test_xyy() {
  59.         assertPalendromeResultEquals("Delete 1st char;", true, "xyy");
  60.     }
  61.  
  62.     public void test_xxy() {
  63.         assertPalendromeResultEquals("Delete last char;", true, "xxy");
  64.     }
  65.  
  66.     public void test_abba() {
  67.         assertPalendromeResultEquals("Do nothing or delete either 'b';", true, "abba");
  68.     }
  69.  
  70.     public void test_abcba() {
  71.         assertPalendromeResultEquals("Do nothing or delete the center 'c';", true, "abcba");
  72.     }
  73.  
  74.     public void test_abccba() {
  75.         assertPalendromeResultEquals("Do nothing or delete either center 'c';", true, "abccba");
  76.     }
  77.  
  78.     public void test_abaaaaafa() {
  79.         assertPalendromeResultEquals("Can't delete both 'b' and 'f';", false, "abaaaaafa");
  80.     }
  81.  
  82.     public void test_ababababab() {
  83.         assertPalendromeResultEquals("Delete first or last char;", true, "ababababab");
  84.     }
  85.  
  86.     public void test_abababxbabab() {
  87.         assertPalendromeResultEquals("Must delete first char;", true, "abababxbabab");
  88.     }
  89.  
  90.     public void test_ababaxababab() {
  91.         assertPalendromeResultEquals("Must delete last char;", true, "ababaxababab");
  92.     }
  93.  
  94.     public void test_abcdedcbfa() {
  95.         assertPalendromeResultEquals("Delete the 'f';", true, "abcdedcbfa");
  96.     }
  97.  
  98.     void assertPalendromeResultEquals(final String message, final boolean expected, final String inputString) {
  99.  
  100.         if (isPalindrome(inputString)) {
  101.             final String doNothingPrefix = "Do nothing or ";
  102.             assertTrue("Expecting the message <" + message + "> to start with the text <" + doNothingPrefix + ">;",
  103.                     message.startsWith(doNothingPrefix) || inputString.length() == 0);
  104.         }
  105.  
  106.         assertEquals(message, expected, validPalindrome(inputString));
  107.     }
  108.  
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement