abc177_b - Substring

Try all possible positions for string $T$ and keep the minimum number of differences for all of them.

Time complexity: $O(n^2)$

Memory complexity: $O(n)$

Click to show code.


using namespace std;
using ll = long long;
using ii = pair<int, int>;
using vi = vector<int>;
int main(void)
{
    ios::sync_with_stdio(false), cin.tie(NULL);
    string s, t;
    cin >> s >> t;
    int n = (int)(s).size(), m = (int)(t).size(), ans = m;
    for (int i = 0; i <= n - m; ++i)
    {
        int cur = m;
        for (int j = 0; j < m; ++j)
            if (t[j] == s[i + j])
                cur--;
        ans = min(ans, cur);
    }
    cout << ans << endl;
    return 0;
}