Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class Solution {
- public:
- int minimumScore(string s, string t) {
- int sn = s.size(), tn = t.size();
- vector<int> pref(sn), post(sn);
- pref[0] = s[0] == t[0] ? 1 : 0;
- for (int i = 1; i < sn; ++i) {
- if (pref[i-1] < tn)
- pref[i] = s[i] == t[pref[i-1]] ? pref[i-1] + 1 : pref[i-1];
- else
- pref[i] = tn;
- }
- post[sn-1] = s[sn-1] == t[tn-1] ? 1 : 0;
- for (int i = sn - 2; i >= 0; --i) {
- if (post[i+1] < tn)
- post[i] = s[i] == t[tn - 1 - post[i+1]] ? post[i+1] + 1 : post[i+1];
- else
- post[i] = tn;
- }
- int ret = INT_MAX;
- for (int i = 0; i + 1 < sn; ++i) ret = min(ret, max(tn - (pref[i] + post[i + 1]), 0));
- ret = min(ret, tn - pref[sn - 1]);
- ret = min(ret, tn - post[0]);
- return ret;
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement