No repeating of the longest string of characters: 48 face questions

#include <iostream>
using namespace std;

int main()
{
    int char_hash[26] = { 0 };
    for (int i = 0; i < 26; i++) {
        char_hash[i] = -1;
    }
    
    int max = 0;
    int head = 0;
    string s = "asdafg";
    int i;
    for (i = 0; i < s.length(); i++) {
        if (char_hash[s[i] - 'a'] < head) {
            char_hash[s[i]-'a'] = i;
        }
        else {
            if (i - head > max) {
                max = i - head;
            }
            head = char_hash[s[i] - 'a'] + 1;
            char_hash[s[i] - 'a'] = i;
            
        }
    }

    if (i - head > max) {
        max = i - head;
    }



    cout << max << endl;

    return 0;
}

Published 92 original articles · won praise 2 · Views 3419

Guess you like

Origin blog.csdn.net/zxc120389574/article/details/105035304