leetcode 3. The longest substring solution code without repeating characters

I don't know why I can't go to this station. I can only go to China station. Because the topic is relatively simple, it's just code.
The idea of this topic is very simple, so I won't go over it. Here I reflect on a small problem of my own, that is, at the beginning, I thought that the title would only give 26 letters, so I directly used a number as a bit operation to use as a set, but later I found something wrong, so I used the set of STL.

#include <string>
#include <iostream>
#include <set>
using namespace std;
/*
    Longest unrepeated substring
    Idea: use a number for bitwise operations instead of sets

    Obviously, duplicate characters separate strings, so consider the beginning of the enumeration substring.
        If a repeating character appears, a new enumeration starts after the repeating character inside the substring. If it is already the last bit, enumeration begins with the repeated characters encountered
*/
class Solution {
public:
    int lengthOfLongestSubstring(string s) {
        int maxNumber = 0;
        int next = 1;
        set<char> repeat;
        for(unsigned int i=0;i<s.length(); i = next)
        {
            repeat.clear();
            next = i+1;
            int current = 0;
            for(unsigned int j=i;j<s.length();j++)
            {
                if(repeat.find(s[j])==repeat.end())
                {
                    repeat.insert(s[j]);
                    current++;
                }
                else//There are duplicate characters
                {
                    for(unsigned int z=i;z<j;z++)
                    {
                        if(s[z]==s[j])//Find the next digit of the repeating character in the substring
                        {
                            next = z+1;
                            break;
                        }
                    }

                    break;
                }
            }
            if(current>maxNumber)
            {
                maxNumber = current;
            }

        }
        return maxNumber;
    }

};

int main(void)
{
    Solution s;
    cout<<s.lengthOfLongestSubstring("abc!abcbb")<<endl;

    return 0;
}

Posted by salman_ahad@yahoo.com on Fri, 27 Dec 2019 10:20:52 -0800