Advertisement
Guest User

Untitled

a guest
Jun 27th, 2019
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.28 KB | None | 0 0
  1. package company.molocoads;
  2.  
  3. /**
  4. * @Author: Lin Tao
  5. * @Date: 6/15/2019 11:10 AM
  6. */
  7. public class Question1 {
  8. boolean equalsWhenOneCharRemoved(String x, String y) {
  9. int m = x.length();
  10. int n = y.length();
  11. if (Math.abs(m - n) > 1) {
  12. return false;
  13. }
  14. if (m > n) {
  15. return equalsWhenOneCharRemoved(y, x);
  16. }
  17. for (int i = 0; i < m; i++) {
  18. if (x.charAt(i) != y.charAt(i)) {
  19. if (m == n) {
  20. return x.substring(i + 1).equals(y.substring(i));
  21. }
  22. return x.substring(i).equals(y.substring(i + 1));
  23. }
  24. }
  25. return m != n;
  26. }
  27.  
  28. public static void main(String[] args) {
  29. Question1 q = new Question1();
  30. boolean result1 = q.equalsWhenOneCharRemoved("x", "y");
  31. boolean result2 = q.equalsWhenOneCharRemoved("x", "XX");
  32. boolean result3 = q.equalsWhenOneCharRemoved("yy", "yx");
  33. boolean result4 = q.equalsWhenOneCharRemoved("abcd", "abxcd");
  34. boolean result5 = q.equalsWhenOneCharRemoved("xyz", "xz");
  35. System.out.println(result1);
  36. System.out.println(result2);
  37. System.out.println(result3);
  38. System.out.println(result4);
  39. System.out.println(result5);
  40. }
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement