Question:


Method: A Map is used to record the character and index, and if the next character is present in the Map, the recount is started, so that the maximum string length can be obtained in one loop.

class LongestSubstringWithoutRepeatingCharacters { fun lengthOfLongestSubstring(s: String): Int { if (s.length < 2) { return s.length } var maxLength = 1 val map = mutableMapOf<Char, Int>() map[s[0]] = 0 var index = 1 while (index <= s.lastIndex) { val ch = s[index] if (map.containsKey(ch)) { map[ch]? .let { index = it + 1 map.clear() } continue } map[ch] = index if (map.size > maxLength) { maxLength = map.size } index++ } return maxLength } } fun main() { val input = "dvdf" val longestSubstringWithoutRepeatingCharacters = LongestSubstringWithoutRepeatingCharacters() print(longestSubstringWithoutRepeatingCharacters.lengthOfLongestSubstring(input)) }Copy the code

Feel free to communicate if you have any questions

Specific code implementation can refer to Github