O(N) C# Solution with simple code & explanation
The idea is store index of each char in dictionary, if repeating char is encountered  - start calculating from stored index, since there cannot be string greate
The idea is store index of each char in dictionary, if repeating char is encountered  - start calculating from stored index, since there cannot be string greater than string from character starting from any index before index of repeating char as in future there will be repeating char of stored index. This helps in avoiding range of chars to check again. Put in other way, we are remembering that this is max length till repeating char is encountered, if it happens we can start from that index.\n\nAlso, dictionary addition and contains is O(1).\n\n```  Csharp\npublic static int LengthOfLongestSubstring(string s)\n{\n    Dictionary<char, int> letters = new Dictionary<char, int>();\n    int length = 0;\n    for (int i = 0; i < s.Length; i++)\n    {\n        if (letters.TryGetValue(s[i], out int index))\n        {\n            length = Math.Max(length, letters.Count);\n            i = index;\n            letters.Clear();\n        }\n        else\n        {\n            letters.Add(s[i], i);\n        }\n    }\n    length = Math.Max(length, letters.Count);\n    return length;\n}\n```
[]