### LeetCode 3. Longest Substring Without Repeating Characters

LeetCode 第三題 "Longest Substring Without Repeating Characters"

## Step 1: 新增測試案例，s 長度為 1 時，應回傳 1

    [TestClass]
public class UnitTest1
{
[TestMethod]
public void s_is_a_should_return_1()
{
var s = "a";
Assert.AreEqual(1, new Solution().LengthOfLongestSubstring(s));
}
}

    public class Solution
{
public int LengthOfLongestSubstring(string s)
{
throw new NotImplementedException();
}
}

## 最終生產代碼版本

    public class Solution
{
public int LengthOfLongestSubstring(string s)
{
var charArray = s.ToCharArray();

var longestWord = new Dictionary<char, int>();
var index = 0;
var lastLongestWordLength = 0;

while (index < charArray.Length)
{
var c = charArray[index];

if (longestWord.ContainsKey(c))
{
lastLongestWordLength = Math.Max(lastLongestWordLength, longestWord.Count);
index = longestWord[c];
longestWord.Clear();
}
else
{
}

index++;
}

return Math.Max(lastLongestWordLength, longestWord.Count);
}
}

## 結論

GitHub commit history: LeetCode_3_Longest_Substring