File tree Expand file tree Collapse file tree 1 file changed +42
-0
lines changed
longest-substring-without-repeating-characters Expand file tree Collapse file tree 1 file changed +42
-0
lines changed Original file line number Diff line number Diff line change 1+ //
2+ // 3. Longest Substring Without Repeating Characters
3+ // https://leetcode.com/problems/longest-substring-without-repeating-characters/description/
4+ // Dale-Study
5+ //
6+ // Created by WhiteHyun on 2024/06/08.
7+ //
8+
9+ class Solution {
10+ func lengthOfLongestSubstring( _ s: String ) -> Int {
11+ var longest : Int = . min
12+ let array = Array ( s)
13+
14+ var set : Set < Character > = [ ]
15+ var temp : Int = 0
16+ var startIndex = 0
17+ for index in array. indices {
18+ if set. contains ( array [ index] ) == false {
19+ set. insert ( array [ index] )
20+ temp += 1
21+ continue
22+ }
23+
24+ if longest < temp {
25+ longest = temp
26+ }
27+
28+ while array [ startIndex] != array [ index] {
29+ set. remove ( array [ startIndex] )
30+ temp -= 1
31+ startIndex += 1
32+ }
33+ startIndex += 1
34+ }
35+
36+ if longest < temp {
37+ longest = temp
38+ }
39+
40+ return longest
41+ }
42+ }
You can’t perform that action at this time.
0 commit comments