-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathLongestSub.swift
50 lines (38 loc) · 1.12 KB
/
LongestSub.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
//Sayed Mahmudul Alam
//Longest Substring Without Repeating Characters
class LongestSub {
func lengthOfLongestSubstring(_ s: String) -> Int {
var tempSet = Set<Character>()
var tempArray = [Character]()
var count = 0
var countArray = [Int]()
for char in s {
tempArray.append(char)
}
if(tempArray.count == 1) {
return 1
}
for i in 0..<tempArray.count {
for j in i..<tempArray.count {
if(tempSet.insert(tempArray[j]).0) {
count += 1
} else {
countArray.append(count)
count = 0
tempSet.removeAll()
break
}
}
}
return getLargest(array: countArray)
}
func getLargest(array: [Int]) -> Int {
var largest = 0
for i in array {
if(i > largest) {
largest = i
}
}
return largest
}
}