Algorithmic Question-the Longest Substring without Repetitive Characters

1. When doing this problem, the first idea is to adopt the method of violent cracking, that is, to convert strings into arrays, traverse arrays, and use List to determine whether duplication occurs. If duplicate characters occur, record the length of the current list once, and only retain the maximum length value to return. Of course, the actual code execution efficiency is very low.

public int LengthOfLongestSubstring(string s) {
         int _result = 0;
            if (s!= "")
            {
                List<char> _charList = new List<char>(s.ToCharArray());
                List<char> _tempList = new List<char>();
                for (int i = 0; i < _charList.Count; i++)
                {
                    char _c = _charList[i];
                    _tempList.Clear();
                    _tempList.Add(_c);
                    for (int j = i + 1; j < _charList.Count; j++)
                    {
                        if (!_tempList.Contains(_charList[j]))
                        {
                            _tempList.Add(_charList[j]);
                        }
                        else
                        {
                            break;
                        }
                    }
                    if (_tempList.Count > _result)
                    {
                        _result = _tempList.Count;
                    }
                }

            }

            return _result;
    }

 

2. In the second attempt, we changed our thinking and used queuing method to judge. First, we converted strings into arrays, and then created a list to queue. Then, we added the array of strings into the list one by one. If we found that there were duplicate elements in the list, we recorded the length of the current list once, and then found the index of duplicate elements, and then we could find the duplicate elements and their previous elements. Clean up, continue to add elements for judgment. Finally, the longest length value calculated will be returned.

The code is as follows:

 public int LengthOfLongestSubstring(string s) {
         int _maxLength = 0;
            List<char> _charList = s.ToList();
            List<char> _tempList = new List<char>();
            for(int i=0;i< _charList.Count; i++)
            {
                if (!_tempList.Contains(_charList[i]))
                {
                    _tempList.Add(_charList[i]);
                    if (_maxLength < _tempList.Count) { _maxLength = _tempList.Count; }
                }
                else
                {
                    //Delete the sequence before the discovery of duplicate elements
                    int _index = _tempList.IndexOf(_charList[i]);
                    for (int j=0;j<= _index; j++)
                    {
                        _tempList.RemoveAt(0);
                    }
                    _tempList.Add(_charList[i]);
                }
            }
            return _maxLength;
    }

Posted by melefire on Sat, 05 Oct 2019 03:47:22 -0700