package leetcode;

/**
 * https://leetcode.com/problems/count-substrings-that-differ-by-one-character/
 */
public class Problem1638 {
    public int countSubstrings(String s, String t) {
        int answer = 0;
        int minLength = Math.max(s.length(), t.length());
        for (int length = 1; length <= minLength; length++) {
            for (int i = 0; i <= s.length() - length; i++) {
                String sub1 = s.substring(i, i + length);
                for (int j = 0; j <= t.length() - length; j++) {
                    String sub2 = t.substring(j, j + length);
                    int count = 0;
                    for (int k = 0; k < length; k++) {
                        if (sub1.charAt(k) != sub2.charAt(k)) {
                            count++;
                        }
                        if (count > 1) {
                            break;
                        }
                    }
                    if (count == 1) {
                        answer++;
                    }
                }
            }
        }
        return answer;
    }

    public static void main(String[] args) {
        Problem1638 prob = new Problem1638();
        System.out.println(prob.countSubstrings("aba", "baba")); // 6
        System.out.println(prob.countSubstrings("ab", "bb")); // 3
        System.out.println(prob.countSubstrings("a", "a")); // 0
        System.out.println(prob.countSubstrings("abe", "bbc")); // 10
        System.out.println(prob.countSubstrings("computer", "computation")); // 90
    }
}

@@ -208,132 +208,8 @@
  0;%0A
-        int minLength = Math.max(s.length(), t.length());%0A        for (int length = 1; length %3C= minLength; length++) %7B%0A    
     
@@ -231,17 +231,16 @@
 = 0; i %3C
-=
  s.lengt
@@ -242,25 +242,16 @@
 length()
- - length
 ; i++) %7B
@@ -255,70 +255,8 @@
 ) %7B%0A
-                String sub1 = s.substring(i, i + length);%0A    
     
@@ -282,17 +282,16 @@
 = 0; j %3C
-=
  t.lengt
@@ -297,92 +297,17 @@
 th()
- - length; j++) %7B%0A                    String sub2 = t.substring(j, j + length);%0A    
+; j++) %7B%0A
     
@@ -329,28 +329,24 @@
  count = 0;%0A
-    
             
@@ -366,24 +366,58 @@
 k = 
-0
+i, l = j
 ; k %3C 
+s.
 length
-; k
+() && l %3C t.length(); k++, l
 ++) 
@@ -442,20 +442,13 @@
     
-   
- 
 if (s
-ub1
 .cha
@@ -461,20 +461,17 @@
  != 
-sub2
+t
 .charAt(
 k)) 
@@ -470,18 +470,14 @@
 rAt(
-k
+l
 )) %7B%0A
-    
     
@@ -521,32 +521,26 @@
             
-    %7D%0A  
+%7D%0A
             
@@ -539,34 +539,32 @@
                 
-  
 if (count %3E 1) %7B
@@ -560,28 +560,24 @@
 ount %3E 1) %7B%0A
-    
             
@@ -607,36 +607,32 @@
                 
-    
 %7D%0A              
@@ -641,621 +641,89 @@
     
-%7D%0A                    if (count == 1) %7B%0A                        answer++;%0A                    %7D%0A                %7D%0A            %7D%0A        %7D%0A        return answer;%0A    %7D%0A%0A    public static void main(String%5B%5D args) %7B%0A        Problem1638 prob = new Problem1638();%0A        System.out.println(prob.countSubstrings(%22aba%22, %22baba%22)); // 6%0A        System.out.println(prob.countSubstrings(%22ab%22, %22bb%22)); // 3%0A        System.out.println(prob.countSubstrings(%22a%22, %22a%22)); // 0%0A        System.out.println(prob.countSubstrings(%22abe%22, %22bbc%22)); // 10%0A        System.out.println(prob.countSubstrings(%22computer%22, %22computation%22)); // 90
+answer += count;%0A                %7D%0A            %7D%0A        %7D%0A        return answer;
 %0A   

Solve problem 1638