Advertisement
nikunjsoni

727-1

Jun 7th, 2021
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.97 KB | None | 0 0
  1. class Solution {
  2. public:
  3.     string minWindow(string s, string t) {
  4.         int sindex = 0, tindex = 0, slen = s.length(), tlen = t.length(), start = -1, len = slen;
  5.         while(sindex < slen) {
  6.             if(s[sindex] == t[tindex]) {
  7.                 if(++tindex == tlen) {
  8.                     //check feasibility from left to right of T
  9.                     int end = sindex+1;
  10.                     //check optimization from right to left of T
  11.                     while(--tindex >= 0) {
  12.                         while(s[sindex--] != t[tindex]);
  13.                     }
  14.                     ++sindex;
  15.                     ++tindex;
  16.                     //record the current smallest candidate
  17.                     if(end - sindex < len) {
  18.                         len = end - sindex;
  19.                         start = sindex;
  20.                     }
  21.                 }
  22.             }
  23.             ++sindex;
  24.         }
  25.         return (start == -1) ? "" : s.substr(start, len);
  26.     }
  27. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement