<commit_before>/**
 * Question Link: https://leetcode.com/problems/longest-substring-without-repeating-characters/
 * Primary idea: Use a set to hold characters and then iterate the string, 
 *               update maxLen, set, startIdx encountering duplicates
 * 
 * Note: Swift does not have a way to access a character in a string with O(1), 
 *       thus we have to first transfer the string to a character array
 * Time Complexity: O(n), Space Complexity: O(n)
 *
 */

class LongestSubstringWithoutRepeatingCharacters {
    func lengthOfLongestSubstring(s: String) -> Int {
        var left = 0, right = 0, substring = Set<Character>(), longest = 0
        let sChars = Array(s)
        
        while right < sChars.count {
            let currentChar = sChars[right]
            
            if substring.contains(currentChar) {
                longest = max(longest, right - left)
                substring.remove(sChars[left])
                left += 1
            } else {
                substring.insert(currentChar)
                right += 1
                
                if right == sChars.count {
                    longest = max(longest, right - left)
                }
            }
        }
        
        return longest
    }
}<commit_msg>[String] Update solution to Longest Substring Without Repeating Characters<commit_after>/**
 * Question Link: https://leetcode.com/problems/longest-substring-without-repeating-characters/
 * Primary idea: Use a set to hold characters and then iterate the string, 
 *               update maxLen, set, startIdx encountering duplicates
 * 
 * Note: Swift does not have a way to access a character in a string with O(1), 
 *       thus we have to first transfer the string to a character array
 * Time Complexity: O(n), Space Complexity: O(n)
 *
 */

class LongestSubstringWithoutRepeatingCharacters {
    func lengthOfLongestSubstring(_ s: String) -> Int {
        var longest = 0, left = 0, set = Set<Character>()
        let sChars = Array(s)
        
        for (i, char) in sChars.enumerated() {
            if set.contains(char) {
                
                longest = max(longest, i - left)
                
                while sChars[left] != char {
                    set.remove(sChars[left])
                    left += 1
                }
                left += 1

            } else {
                set.insert(char)
            }
        }
        
        return max(longest, sChars.count - left)
    }
}
{'fraction_non_alphanumeric': 0.08664259927797834, 'fraction_numerical': 0.004412354592860008, 'mean_word_length': 1.722707423580786, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 7, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5624957', 'n_tokens_mistral': 704, 'n_tokens_neox': 651, 'n_words': 210}