简体   繁体   中英

Given two string S and T. Determine a substring of S that has minimum difference with T?

I have two string S and T where length of S >= length of T . I have to determine a substring of S which has same length as T and has minimum difference with T . Here difference between two strings of same length means, the number of indexes where they differ. For example: "ABCD" and "ABCE" differ at 3rd index, so their difference is 1.

I know I can use KMP(Knuth Morris Pratt) Pattern Searching algorithm to search T within S. But, what if S doesn't contain T as a substring? So, I have coded a brute force approach to solve this:

int main() {
    string S, T;
    cin >> S >> T;

    int SZ_S = S.size(), SZ_T = T.size(), MinDifference = INT_MAX;
    string ans;

    for (int i = 0; i + SZ_T <= SZ_S; i++) {    // I generate all the substring of S
        int CurrentDifference = 0;              // and check their difference with T
        for (int j = 0; j < SZ_T; j++) {        // and store the substring with minimum difference
            if (S[i + j] != T[j])
                CurrentDifference++;
        }
        if (CurrentDifference < MinDifference) {
            ans = S.substr (i, SZ_T);
            MinDifference = CurrentDifference;
        }
    }
    cout << ans << endl;
}

But, my approach only works when S and T has shorter length. But, the problem is S and T can have length as large as 2 * 10^5 . How can I approach this?

Let's maximize the number of characters that match. We can solve the problem for each character of the alphabet separately, and then sum up the results for substrings. To solve the problem for a particular character, give string S and T as sequences 0 and 1 and multiply them using the FFT https://en.wikipedia.org/wiki/Fast_Fourier_transform .

Complexity O(|A| * N log N) where |A| size of the alphabet (for an uppercase letter is 26).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM