[Leetcode] 3. Longest Substring Without Repeating Characters
[Leetcode] 3. Longest Substring Without Repeating Characters

[Leetcode] 3. Longest Substring Without Repeating Characters

์–ธ์–ด
Python
C
๋‚œ์ด๋„
๋‹ค์‹œ ํ’€์–ด๋ณด๊ธฐ
๋‹ค์‹œ ํ’€์–ด๋ณด๊ธฐ
์•Œ๊ณ ๋ฆฌ์ฆ˜ ์œ ํ˜•
์ž‘์„ฑ์ž
๋ฐ•์šฉ์„ฑ๋ฐ•์šฉ์„ฑ
์ƒ์„ฑ ์ผ์‹œ
2024๋…„ 06์›” 02์ผ
ย 

๐Ÿ–ฅ๏ธย ์‹œ์ž‘ํ•˜๋ฉฐ

์ผ๋‹จ ์žˆ๋Š” ๊ทธ๋Œ€๋กœ ํ’€์ด๋ฅผ ์‹œ์ž‘ํ•˜๋ฉด, ํŒŒ์ด์ฌ์œผ๋กœ๋Š” ์Šฌ๋ผ์ด์‹ฑ์œผ๋กœ ๊ต‰์žฅํžˆ ์‰ฝ๊ฒŒ ํ•  ์ˆ˜ ์žˆ๋‹ค.
from typing import List class Solution: def lengthOfLongestSubstring(self, s: str) -> int: res = 0 for i in range(len(s)): # ๊ธธ์ด๋ฅผ ์ €์žฅํ•  ์ž„์‹œ ๋ณ€์ˆ˜ tmpres = 0 # substring์— ๋ฌธ์ž๊ฐ€ ๋” ๋“ค์–ด๊ฐ€๋Š”์ง€ ํ™•์ธ for j in range(i, len(s)): if s[j] in s[i:j]: break tmpres += 1 res = max(res, tmpres) return res s = Solution() print(s.lengthOfLongestSubstring("pwwkew"))
ํ•˜์ง€๋งŒ ์‹œ๊ฐ„์ด ๋„ˆ๋ฌด ์˜ค๋ž˜ ๊ฑธ๋ ค์„œ.. ๋” ์ข‹์€ ํ’€์ด๊ฐ€ ์žˆ๋‚˜ ์‹ถ์–ด ์ฐพ์•„๋ณด๋‹ค, ์ธ์ƒ์ ์ธ ํ’€์ด๋ฅผ ๋ณด๊ฒŒ ๋˜์—ˆ๋‹ค.
ย 
์—ญ์‹œ ์ด๋Ÿฐ ๋ฌธ์ œ๋Š” ์ง‘ํ•ฉ์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒŒ ๊ตญ๋ฃฐ์ธ ๊ฒƒ ๊ฐ™๋‹ค.
from typing import List class Solution: def lengthOfLongestSubstring(self, s: str) -> int: res = 0 visit = set() left = 0 # ์™ผ์ชฝ idx # ์˜ค๋ฅธ์ชฝ idx๋ถ€ํ„ฐ ์‹œ์ž‘ for right in range(len(s)): # visit์— ์žˆ๋‹ค๋ฉด, visit์—์„œ ์‚ญ์ œํ•˜๊ณ  left ์ธ๋ฑ์Šค ์ฆ๊ฐ€ # left์™€ right๋ฅผ ์ด์šฉํ•ด ์ „์ฒด ๊ธธ์ด ๊ตฌํ•จ while s[right] in visit: visit.remove(s[left]) left += 1 # visit์— ๋ฌธ์ž ์ถ”๊ฐ€ visit.add(s[right]) res = max(res, right - left + 1) return res s = Solution() print(s.lengthOfLongestSubstring("pwwkew"))
ย 
์˜ˆ๋ฅผ ๋“ค์–ด s = "pwwkew"์˜ ๊ฒฝ์šฐ:
  • ๋ฌธ์ž p๋ฅผ ์ฒ˜๋ฆฌํ•  ๋•Œ visit = {'p'}, res = 1.
  • ๋ฌธ์ž w๋ฅผ ์ฒ˜๋ฆฌํ•  ๋•Œ visit = {'p', 'w'}, res = 2.
  • ๋˜ ๋‹ค๋ฅธ w๋ฅผ ๋งŒ๋‚˜๋ฉด visit์—์„œ p๋ฅผ ์ œ๊ฑฐํ•˜๊ณ  l์„ ์ด๋™, visit = {'w'}, res = 2.
  • ๋ฌธ์ž k๋ฅผ ์ฒ˜๋ฆฌํ•  ๋•Œ visit = {'w', 'k'}, res = 2.
  • ๋ฌธ์ž e๋ฅผ ์ฒ˜๋ฆฌํ•  ๋•Œ visit = {'w', 'k', 'e'}, res = 3.
  • ๋งˆ์ง€๋ง‰์œผ๋กœ w๋ฅผ ์ฒ˜๋ฆฌํ•  ๋•Œ visit์—์„œ w ์ด์ „ ๋ฌธ์ž๋ฅผ ์ œ๊ฑฐํ•˜๊ณ , ์ตœ์ข…์ ์œผ๋กœ visit = {'k', 'e', 'w'}, res = 3.
ย 

โš™๏ธย Python

from typing import List class Solution: def lengthOfLongestSubstring(self, s: str) -> int: res = 0 visit = set() left = 0 # ์™ผ์ชฝ idx # ์˜ค๋ฅธ์ชฝ idx๋ถ€ํ„ฐ ์‹œ์ž‘ for right in range(len(s)): # visit์— ์žˆ๋‹ค๋ฉด, visit์—์„œ ์‚ญ์ œํ•˜๊ณ  left ์ธ๋ฑ์Šค ์ฆ๊ฐ€ # left์™€ right๋ฅผ ์ด์šฉํ•ด ์ „์ฒด ๊ธธ์ด ๊ตฌํ•จ while s[right] in visit: visit.remove(s[left]) left += 1 # visit์— ๋ฌธ์ž ์ถ”๊ฐ€ visit.add(s[right]) res = max(res, right - left + 1) return res s = Solution() print(s.lengthOfLongestSubstring("pwwkew"))

โš™๏ธย C++

#include <iostream> #include <set> #include <string> using namespace std; class Solution { public: int lengthOfLongestSubstring(string s) { int res = 0; set<char> visit; int left = 0; for (int right = 0; right < s.length(); ++right) { while (visit.find(s[right]) != visit.end()) { visit.erase(s[left]); left++; } visit.insert(s[right]); res = max(res, right - left + 1); } return res; } }; int main() { Solution solution; string test = "pwwkew"; cout << solution.lengthOfLongestSubstring(test) << endl; return 0; }
ย 
ย 

๐Ÿ“Œย ์†Œ๊ฐ

์•„.. ๋ฌธ์ž์—ด ๋งŒ์ง€๋Š” ๋ฌธ์ œ๋Š” ์–ธ์ œ๋‚˜ ์–ด๋ ค์šด ๊ฒƒ ๊ฐ™๋‹ค.
ย 

๐Ÿ”ย ๋ถ€๋ก

๐Ÿ”ย ์ฐธ๊ณ ๋ฌธํ—Œ


ย 

๋Œ“๊ธ€

guest