๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Algorithm๐Ÿฐ/๋ฆฌํŠธ์ฝ”๋“œ

[๋ฆฌํŠธ์ฝ”๋“œ] 3. Longest Substring Without Repeating Characters

by Jouureee 2021. 7. 16.

๋ฌธ์ œ :

https://leetcode.com/problems/longest-substring-without-repeating-characters/

 

Longest Substring Without Repeating Characters - LeetCode

Level up your coding skills and quickly land a job. This is the best place to expand your knowledge and get prepared for your next interview.

leetcode.com

 

๋‚œ์ด๋„ : Medium

 

ํ’€์ด ๋ฐฉ๋ฒ• : ๋ธŒ๋ฃจํŠธ ํฌ์Šค O(n^2)

 

๋ฌธ์ž์—ด s์—์„œ ์ค‘๋ณต๋˜๋Š” ๋ฌธ์ž ์—†์ด ๊ฐ€์žฅ ๊ธด ์„œ๋ธŒ ์ŠคํŠธ๋ง์„ ์ฐพ๋Š” ๋ฌธ์ œ์˜€๋‹ค. 

์Šฌ๋ผ์ด๋”ฉ ์œˆ๋„์šฐ ๋ฐฉ์‹์„ ์‚ฌ์šฉํ•˜๋ฉด O(n) ๋งŒํผ์œผ๋กœ๋„ ํ•ด๊ฒฐ ํ•  ์ˆ˜ ์žˆ๋‹ค๊ณ  ํ•œ๋‹ค. ๋ฆฌํŠธ์ฝ”๋“œ์— ํ† ํ”ฝ ๋ณด๋Š” ๋ฐฉ๋ฒ•์„ ๋ชฐ๋ผ ํ’€๋ฆฌ๋Š” ๋Œ€๋กœ ํ’€์—ˆ๋Š”๋ฐ ์ด์ œ๋Š” ์ข€ ํšจ์œจ์ ์„ ํ‘ธ๋Š” ๋ฐฉ๋ฒ•๋„ ๊ฐ™์ด ๊ณต๋ถ€ํ•ด์•ผ๊ฒ ๋‹ค ํ•˜ํ•˜ 

 

c++ ์ฝ”๋“œ (๋ธŒ๋ฃจํŠธํฌ์Šค)

class Solution {
public:
    string answer = "";
    int answerLength = 0;

    
    int lengthOfLongestSubstring(string s) {
        
        if(s.length() == 1){
            return 1;
        }
        
        for(int i = 0; i< s.length(); i++){
            for(int j = i; j < s.length(); j ++){
                char findCharacter = s[j];
                 if(answer.find(findCharacter) == -1){ // can not find
                    answer += findCharacter;
                }else {
                   if(answerLength < answer.length()){
                        answerLength = answer.length();
                        }  
                     answer = "";
                    break;
                    }
                    
                }
            }
        return answerLength;
        }
    };

 

c++ ์Šฌ๋ผ์ด๋”ฉ ์œˆ๋„์šฐ ๋ฐฉ์‹ 

: ํ’€์–ด๋ณผ ์˜ˆ์ •

class Solution {
public:
    int lengthOfLongestSubstring(string s) {
        
        //SLIDING WINDOW  - TIME COMPLEXITY O(2n)
        //                  SPACE COMPLEXITY O(m)   //size of array
        
        int store[256]={0}; //array to store the occurences of all the characters
        int l=0;    //left pointer
        int r=0;    //right pointer
        int ans=0;  //initializing the required length as 0
        
        while(r<s.length())     //iterate over the string till the right pointer reaches the end of the string 
        {
            store[s[r]]++;      //increment the count of the character present in the right pointer 
            
            while(store[s[r]]>1)    //if the occurence become more than 1 means the char is repeated
            { 
                store[s[l]]--;   //reduce the occurence of temp as it might be present ahead also in the string
                l++;         //contraction of the present window till the occurence of the 't' char becomes 1
            }
            
            ans = max(ans,r-l+1);    //As the index starts from 0 , ans will be (right pointer-left pointer + 1)
            r++;        // now will increment the right pointer 
        }
        return ans;
    }
};

๋Œ“๊ธ€