Last active
March 13, 2021 01:34
-
-
Save jbasinger/ddd08a4c0f3b550e5e199326e51ca4fd to your computer and use it in GitHub Desktop.
Leetcode Problem 3
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public int LengthOfLongestSubstring(string s) | |
{ | |
if(s.Length == 0) | |
return 0; | |
if (s.Length == 1) | |
return 1; | |
var maxLength = 1; | |
var curLength = 0; | |
var chars= new Dictionary<char, bool>(); | |
for (var i = 0; i < s.Length; i++) | |
{ | |
chars.Clear(); | |
var firstChar = s[i]; | |
chars[firstChar] = true; | |
curLength = 1; | |
for (var j = i+1; j < s.Length; j++) | |
{ | |
var currentChar = s[j]; | |
if (chars.ContainsKey(currentChar)) | |
{ | |
if (j == s.Length - 1) | |
{ | |
return maxLength; | |
} | |
break; | |
} | |
chars[currentChar] = true; | |
curLength++; | |
if (maxLength <= curLength) | |
maxLength = curLength; | |
if (j == s.Length - 1) | |
{ | |
return maxLength; | |
} | |
} | |
} | |
return maxLength; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[Test] | |
public void ShouldReturnTheWholeThing() | |
{ | |
var str = "qwertyuiop"; | |
var res = _sut.LengthOfLongestSubstring(str); | |
res.ShouldBe(str.Length); | |
} | |
[Test] | |
public void ShouldReturnZeroIfEmpty() | |
{ | |
var res = _sut.LengthOfLongestSubstring(""); | |
res.ShouldBe(0); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[Test] | |
public void ShouldReturnOneIfOneCharacter() | |
{ | |
var str = "o"; | |
var res = _sut.LengthOfLongestSubstring(str); | |
res.ShouldBe(str.Length); | |
} | |
[Test] | |
public void ShouldReturnOneIfAllTheSame() | |
{ | |
var str = "bbbbb"; | |
var res = _sut.LengthOfLongestSubstring(str); | |
res.ShouldBe(1); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[Test] | |
public void ShouldReturnLongStringAtBeginning() | |
{ | |
var str = "poiuytytweqwrtwyr"; | |
var res = _sut.LengthOfLongestSubstring(str); | |
res.ShouldBe(6); | |
} | |
[Test] | |
public void ShouldReturnLongStringAtTheEnd() | |
{ | |
var str = "rywtrwqewtytyuiop"; | |
var res = _sut.LengthOfLongestSubstring(str); | |
res.ShouldBe(6); | |
} | |
[Test] | |
public void ShouldReturnLongStringInTheMiddle() | |
{ | |
var str = "qweqrrplikfudjggtyr"; | |
var res = _sut.LengthOfLongestSubstring(str); | |
res.ShouldBe(10); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment