Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package company.molocoads;
- /**
- * @Author: Lin Tao
- * @Date: 6/15/2019 11:10 AM
- */
- public class Question1 {
- boolean equalsWhenOneCharRemoved(String x, String y) {
- int m = x.length();
- int n = y.length();
- if (Math.abs(m - n) > 1) {
- return false;
- }
- if (m > n) {
- return equalsWhenOneCharRemoved(y, x);
- }
- for (int i = 0; i < m; i++) {
- if (x.charAt(i) != y.charAt(i)) {
- if (m == n) {
- return x.substring(i + 1).equals(y.substring(i));
- }
- return x.substring(i).equals(y.substring(i + 1));
- }
- }
- return m != n;
- }
- public static void main(String[] args) {
- Question1 q = new Question1();
- boolean result1 = q.equalsWhenOneCharRemoved("x", "y");
- boolean result2 = q.equalsWhenOneCharRemoved("x", "XX");
- boolean result3 = q.equalsWhenOneCharRemoved("yy", "yx");
- boolean result4 = q.equalsWhenOneCharRemoved("abcd", "abxcd");
- boolean result5 = q.equalsWhenOneCharRemoved("xyz", "xz");
- System.out.println(result1);
- System.out.println(result2);
- System.out.println(result3);
- System.out.println(result4);
- System.out.println(result5);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement