Thinking Process

this question is relatively easy to beginners and friendly.Method is two pointers. when the number of characters between pointer 'l' and pointer has been up to 26 different letters, update the length and only move l to plus 1. It's not necessary to move 'r' pointer because 'l'~'r' range has been proved that it's legal.

Code

// ykjygvedtysvyymzfizzwkjamefxjnrnphqwnfhrnbhwjhqcgqnplodeestu
#include<iostream>
#include<string.h>
#include<math.h>
int num[26];
using namespace std;
string s;
int main() {
    cin >> s ;
    int len = s.length();
    int res = 0xffffff, cnt = 0, r = 0;
    for(int l = 0 ; l < len;l ++) {
        while(r < len && cnt != 26) {
            if(num[s[r] - 'a'] == 0) cnt ++;
     //       printf("现在的字母是%c现在的cnt=%d\n", s[r], cnt);
            num[s[r] - 'a'] ++;
            r ++;
        }
        if(cnt == 26) res = min(res, r - l);
   //     printf("res = %d\n", res);
        num[s[l] - 'a'] --;
        if(num[s[l] - 'a'] == 0) cnt --;
        
    }
    printf("%d", res);
}