Skip to content

Commit c957612

Browse files
committed
feat: Add solution for LeetCode problem 3
1 parent fbff392 commit c957612

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
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+
}

0 commit comments

Comments
 (0)