fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6.  
  7. /* Name of the class has to be "Main" only if the class is public. */
  8. class Ideone
  9. {
  10. public static void main (String[] args) throws java.lang.Exception
  11. {
  12. String input = "abcb!bca";
  13. System.out.println(input + ": " + palindromeMinusOneChar(input));
  14. }
  15. static boolean palindromeMinusOneChar(String str)
  16. {
  17. return pm(str, true) || pm(str, false);
  18. }
  19. static boolean pm(String str, boolean rtl)
  20. {
  21. if (str == null) return true;
  22. char[] a = str.toCharArray();
  23. boolean deleted = false;
  24.  
  25. for (int l = 0, r = a.length - 1; l < r; l++, r--) {
  26. if (a[l] == a[r]) continue;
  27. if (!deleted) {
  28. deleted = true;
  29. if (rtl) {
  30. if (a[l] == a[r-1]) { r--; continue; } // delete from right
  31. if (a[l+1] == a[r]) { l++; continue; } // delete from left
  32. } else {
  33. if (a[l+1] == a[r]) { l++; continue; } // delete from left
  34. if (a[l] == a[r-1]) { r--; continue; } // delete from right
  35. }
  36. return false; // can't delete
  37. }
  38. return false; // already deleted
  39. }
  40. return true;
  41. }
  42. }
Success #stdin #stdout 0.07s 380160KB
stdin
xyzhgjhgjhgkjhghkjg jhjgvhkvhgv jhhbjkvbhgkv 
stdout
abcb!bca: true