美团点评 2018 春招 字符串距离

import java.util.*;

public class Main {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

        while (true){
            String s = sc.nextLine();
            String t = sc.nextLine();

            int a = 0, b = 0;
            for(int i = 0; i <= s.length() - t.length(); i ++){
                a += (s.charAt(i) == 'a') ? 1 : 0;
                b += (s.charAt(i) == 'b') ? 1 : 0;
            }

            int ans = 0;
            for(int i = 0; i - t.length() < 0; i ++) {
                ans += (t.charAt(i) == 'a') ? b : a;
                if(s.charAt(i) == 'a') a --;
                else b --;
                if( i + s.length() - t.length() + 1 < s.length() && s.charAt(i + s.length() - t.length() + 1) == 'a') a ++;
                else b++;
            }
            System.out.println(ans);
        }
    }


}

你可能感兴趣的:(面试)