CodeforcesSep 03, 2025

Remove the Substring (easy version)

Hazrat Ali

Codeforces

You are given a string s and a string t, both consisting only of lowercase Latin letters. It is guaranteed that t can be obtained from s by removing some (possibly, zero) number of characters (not necessary contiguous) from s without changing order of remaining characters (in other words, it is guaranteed that t is a subsequence of s).

For example, the strings "test", "tst", "tt", "et" and "" are subsequences of the string "test". But the strings "tset", "se", "contest" are not subsequences of the string "test".

You want to remove some substring (contiguous subsequence) from s of maximum possible length such that after removing this substring t will remain a subsequence of s.

If you want to remove the substring s[l;r] then the string s will be transformed to s1s2sl1sr+1sr+2s|s|1s|s| (where |s| is the length of s).

Your task is to find the maximum possible length of the substring you can remove so that t is still a subsequence of s.

Input

The first line of the input contains one string s consisting of at least 1 and at most 200 lowercase Latin letters.

The second line of the input contains one string t consisting of at least 1 and at most 200 lowercase Latin letters.

It is guaranteed that t is a subsequence of s.

Output

Print one integer — the maximum possible length of the substring you can remove so that t is still a subsequence of s.

Examples
Input
bbaba
bb
Output
3
Input
baaba
ab
Output
2
Input
abcde
abcde
Output
0
Input
asdfasdf
fasd
Output
3

Solution
#include <bits/stdc++.h>
using namespace std;

bool subseq(string& s, string& t) {
  int i = 0, j = 0;
  while (i < s.size() and j < t.size()) {
    if (s[i] == t[j]) {
      j++;
    }
    i++;
  }
  return j == t.size();
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  string s, t;
  cin >> s >> t;
  int ans = 0;
  for (int l = 0; l < s.size(); l++) {
    for (int r = 0; r < s.size(); r++) {
      string ns = s.substr(0, l) + s.substr(r + 1);
      if (subseq(ns, t)) {
        ans = max(ans, r - l + 1);
      }
    }
  }
  cout << ans << endl;
  return 0;
}



Comments