This page is still under construction.

Parts of this page are still being built. What you see may change.

Almost Same Substring

Time limit4sMemory limit512 MB

Summary
Count the length-|T'| substrings of S that differ from T' in exactly one character.
Level

Medium6 of 10

Topics
String, String matching, Hash map, Binary search
Solved
No attempts yet

Problem

The unlucky Ikuta had his precious string TT overwritten by a virus into a different string T′T'. He knows the virus changed exactly one character of TT into a different character. That is, TT and T′T' differ in exactly one character. To recover TT, Ikuta prepared a document SS in which he believes TT appears. As a step toward recovering TT, he wants to count the substrings of SS that could possibly match TT.

Given the string T′T' and the document SS, find the number of substrings a_ka_k+1...a_k+∣T′∣−1a\_{k} a\_{k+1} ... a\_{k+|T'|-1} of S=a_1a_2a_3...a_∣S∣S = a\_{1} a\_{2} a\_{3} ... a\_{|S|} of length ∣T′∣|T'| (1≤k≤∣S∣−∣T′∣+11 \leq k \leq |S| - |T'| + 1) that differ from T′T' in exactly one character.

Input

Each variable in the input satisfies the following constraints.

  • 1≤∣S∣≤300,0001 \leq |S| \leq 300,000

  • 1≤∣T′∣≤∣S∣1 \leq |T'| \leq |S|

Output

Print the number of substrings satisfying the condition on one line.

Examples3

  1. Example 1

    Input
    abcbcdbc
    abc
    
    Expected output
    2
    
  2. Example 2

    Input
    aaaaaa
    aaaaaa
    
    Expected output
    0
    
  3. Example 3

    Input
    baaaaaaaa
    b
    
    Expected output
    8