package dev.egonr.leetcode

/* Medium
Given a string s, find the length of the longest
substring
without repeating characters.



Example 1:

Input: s = "abcabcbb"
Output: 3
Explanation: The answer is "abc", with the length of 3.

Example 2:

Input: s = "bbbbb"
Output: 1
Explanation: The answer is "b", with the length of 1.

Example 3:

Input: s = "pwwkew"
Output: 3
Explanation: The answer is "wke", with the length of 3.
Notice that the answer must be a substring, "pwke" is a subsequence and not a substring.



Constraints:

    0 <= s.length <= 5 * 104
    s consists of English letters, digits, symbols and spaces.
 */

class LongestSubstring {
    fun test() {
        println(lengthOfLongestSubstring("abcabcbb")) // "abc" length 3
        println(lengthOfLongestSubstring("bbbbb")) // "b" length 1
        println(lengthOfLongestSubstring("pwwkew")) // "wke" length 3
        println(lengthOfLongestSubstring(" ")) // " " length 1
        println(lengthOfLongestSubstring("au")) // "au" length 2
        println(lengthOfLongestSubstring("dvdf")) // "vdf" length 3
        println(lengthOfLongestSubstring("anviaj")) // "nviaj" length 5
    }

    fun lengthOfLongestSubstring(s: String): Int {
        if (s.isEmpty()) {
            return 0
        }

        var longestSubstrCount = 1
        val substr = HashSet<Char>()

        var checkedUntil = 0
        var i = 0
        while (i < s.length) {
            val currentChar = s[i]
            println("$s -> $currentChar@$i")
            if (substr.contains(currentChar) && i > checkedUntil) {
                val substrCount = substr.count()
                if (substrCount > longestSubstrCount) {
                    longestSubstrCount = substrCount
                }
                println(substr)
                i -= substr.count()
                i++
                checkedUntil = i
                substr.clear()
            } else {
                substr.add(currentChar)
                i++
            }
        }

        val substrCount = substr.count()
        if (substrCount > longestSubstrCount) {
            println(substr)
            longestSubstrCount = substrCount
        }

        return longestSubstrCount
    }
}
package dev.egonr.leetcode

/* Medium
Given a string s, find the length of the longest
substring
without repeating characters.



Example 1:

Input: s = "abcabcbb"
Output: 3
Explanation: The answer is "abc", with the length of 3.

Example 2:

Input: s = "bbbbb"
Output: 1
Explanation: The answer is "b", with the length of 1.

Example 3:

Input: s = "pwwkew"
Output: 3
Explanation: The answer is "wke", with the length of 3.
Notice that the answer must be a substring, "pwke" is a subsequence and not a substring.



Constraints:

    0 <= s.length <= 5 * 104
    s consists of English letters, digits, symbols and spaces.
 */

class LongestSubstring {
    fun test() {
        println(lengthOfLongestSubstring("abcabcbb")) // "abc" length 3
        println(lengthOfLongestSubstring("bbbbb")) // "b" length 1
        println(lengthOfLongestSubstring("pwwkew")) // "wke" length 3
        println(lengthOfLongestSubstring(" ")) // " " length 1
        println(lengthOfLongestSubstring("au")) // "au" length 2
        println(lengthOfLongestSubstring("dvdf")) // "vdf" length 3
        println(lengthOfLongestSubstring("anviaj")) // "nviaj" length 5
    }

    fun lengthOfLongestSubstring(s: String): Int {
        if (s.isEmpty()) {
            return 0
        }

        var longestSubstrCount = 1
        val substr = HashSet<Char>()

        var checkedUntil = 0
        var i = 0
        while (i < s.length) {
            val currentChar = s[i]
            println("$s -> $currentChar@$i")
            if (substr.contains(currentChar) && i > checkedUntil) {
                val substrCount = substr.count()
                if (substrCount > longestSubstrCount) {
                    longestSubstrCount = substrCount
                }
                println(substr)
                i -= substr.count()
                i++
                checkedUntil = i
                substr.clear()
            } else {
                substr.add(currentChar)
                i++
            }
        }

        val substrCount = substr.count()
        if (substrCount > longestSubstrCount) {
            println(substr)
            longestSubstrCount = substrCount
        }

        return longestSubstrCount
    }
}