3. Longest Substring Without Repeating Characters

Given a string, find the length of the longest substring without repeating characters.
Examples:
Given "abcabcbb", the answer is "abc", which the length is 3.
Given "bbbbb", the answer is "b", with the length of 1.
Given "pwwkew", the answer is "wke", with the length of 3. Note that the answer must be a substring, "pwke" is a subsequence and not a substring.


Two versions:

No.1 Using Hashmap

Time complexity is O(N*m), N is the size of the string s, m is the max size of the substring without repeating characters. This method is very easy to understand but low efficient.

int lengthOfLongestSubstring(string s) {
  int maxLength = 0;
  for(int i = 0; i < s.length(); i++){
    unordered_map<char,int> hash;
    int j = i;
    for(; j < s.length(); j++){
      hash[s[j]]++;
      if(hash[s[j]] > 1) break;
    }
    maxLength = max(maxLength, j - i);
  }
  return maxLength;
}

No.2 Using vector

The key idea there is that before we start traverse the string, we use a vector to store each character's newest position we have seen. From the beginning, if we have not met a duplicate character, we keep update the index from -1 to their index. When we see the first duplicate, we update our start position to the character's previous position we have stored in the vector so as to find another substring without duplicate characters.
Time complexity is O(N), much more efficient!

int lengthOfLongestSubstring(string s) {
    vector<int> dict(256, -1);
    int maxLen = 0, start = -1;
    for (int i = 0; i != s.length(); i++) {
        if (dict[s[i]] > start)
            start = dict[s[i]];
        dict[s[i]] = i;
        maxLen = max(maxLen, i - start);
    }
    return maxLen;
}
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子恋沃,更是在濱河造成了極大的恐慌女责,老刑警劉巖踢步,帶你破解...
    沈念sama閱讀 221,198評論 6 514
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件髓抑,死亡現(xiàn)場離奇詭異飞蚓,居然都是意外死亡,警方通過查閱死者的電腦和手機曲饱,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,334評論 3 398
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來珠月,“玉大人扩淀,你說我怎么就攤上這事∏盼拢” “怎么了引矩?”我有些...
    開封第一講書人閱讀 167,643評論 0 360
  • 文/不壞的土叔 我叫張陵狼讨,是天一觀的道長鞋屈。 經常有香客問我,道長指么,這世上最難降的妖魔是什么掏觉? 我笑而不...
    開封第一講書人閱讀 59,495評論 1 296
  • 正文 為了忘掉前任区端,我火速辦了婚禮,結果婚禮上澳腹,老公的妹妹穿的比我還像新娘织盼。我一直安慰自己杨何,他們只是感情好,可當我...
    茶點故事閱讀 68,502評論 6 397
  • 文/花漫 我一把揭開白布沥邻。 她就那樣靜靜地躺著危虱,像睡著了一般。 火紅的嫁衣襯著肌膚如雪唐全。 梳的紋絲不亂的頭發(fā)上埃跷,一...
    開封第一講書人閱讀 52,156評論 1 308
  • 那天,我揣著相機與錄音邮利,去河邊找鬼弥雹。 笑死,一個胖子當著我的面吹牛延届,可吹牛的內容都是我干的剪勿。 我是一名探鬼主播,決...
    沈念sama閱讀 40,743評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼方庭,長吁一口氣:“原來是場噩夢啊……” “哼厕吉!你這毒婦竟也來了?” 一聲冷哼從身側響起二鳄,我...
    開封第一講書人閱讀 39,659評論 0 276
  • 序言:老撾萬榮一對情侶失蹤赴涵,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后订讼,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體髓窜,經...
    沈念sama閱讀 46,200評論 1 319
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 38,282評論 3 340
  • 正文 我和宋清朗相戀三年欺殿,在試婚紗的時候發(fā)現(xiàn)自己被綠了寄纵。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 40,424評論 1 352
  • 序言:一個原本活蹦亂跳的男人離奇死亡脖苏,死狀恐怖程拭,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情棍潘,我是刑警寧澤恃鞋,帶...
    沈念sama閱讀 36,107評論 5 349
  • 正文 年R本政府宣布,位于F島的核電站亦歉,受9級特大地震影響恤浪,放射性物質發(fā)生泄漏。R本人自食惡果不足惜肴楷,卻給世界環(huán)境...
    茶點故事閱讀 41,789評論 3 333
  • 文/蒙蒙 一水由、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧赛蔫,春花似錦砂客、人聲如沸泥张。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,264評論 0 23
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽媚创。三九已至,卻和暖如春齿诉,著一層夾襖步出監(jiān)牢的瞬間筝野,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,390評論 1 271
  • 我被黑心中介騙來泰國打工粤剧, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人挥唠。 一個月前我還...
    沈念sama閱讀 48,798評論 3 376
  • 正文 我出身青樓抵恋,卻偏偏與公主長得像,于是被迫代替她去往敵國和親宝磨。 傳聞我的和親對象是個殘疾皇子弧关,可洞房花燭夜當晚...
    茶點故事閱讀 45,435評論 2 359

推薦閱讀更多精彩內容