public class Solution {
public int LengthOfLongestSubstring(string s) {
if(string.IsNullOrEmpty(s)) return 0;
int maxLength = 1;
int tempLength = 1;
//key: character, value: first index at string
Dictionary<char, int> dict = new Dictionary<char, int>();
for(int i = 0; i < s.Length; i++){
if(i == 0){
dict[s[0]] = 0;
}
for(int j = i + 1; j < s.Length; j++){
//condition: 1. has duplicate char
//2. duplicate char in range [i,j]
if(dict.ContainsKey(s[j]) && dict[s[j]] >= i && dict[s[j]] < j){
i = dict[s[j]];
dict[s[j]] = j;
break;
}
else{
dict[s[j]] = j;
tempLength = j - i + 1;
if(tempLength > maxLength){
maxLength = tempLength;
}
}
}
}
return maxLength;
}
}
3. Longest Substring Without Repeating Characters
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。
相關(guān)閱讀更多精彩內(nèi)容
- 題目 原題鏈接Given a string, find the length of the longest sub...
- LeetCode Problems Solutions question description:問(wèn)題描述 Giv...
- 最近好久沒(méi)有更新博客了,因?yàn)椴┲髯罱τ趯W(xué)校的一些學(xué)業(yè)工作。大三下學(xué)期嘛,還是挺忙的-,-。下面我們來(lái)看一下這道題...
- Given a string, find the length of the longest substring ...