-
Notifications
You must be signed in to change notification settings - Fork 4
/
Solution.kt
72 lines (62 loc) · 1.64 KB
/
Solution.kt
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package _003.kotlin
import java.util.HashSet
import java.util.HashMap
/**
* @author relish
* @since 2018/10/20
*/
class Solution {
fun lengthOfLongestSubstring1(s: String): Int {
val n = s.length
val set = HashSet<Char>()
var ans = 0
var i = 0
var j = 0
while (i < n && j < n) {
// try to extend the range [i, j]
if (!set.contains(s[j])) {
set.add(s[j++])
ans = Math.max(ans, j - i)
} else {
set.remove(s[i++])
}
}
return ans
}
fun lengthOfLongestSubstring2(s: String): Int {
val n = s.length
var ans = 0
val map = HashMap<Char, Int>() // current index of character
// try to extend the range [i, j]
var j = 0
var i = 0
while (j < n) {
if (map.containsKey(s[j])) {
i = Math.max(map.get(s[j])!!, i)
}
ans = Math.max(ans, j - i + 1)
map[s[j]] = j + 1
j++
}
return ans
}
fun lengthOfLongestSubstring3(s: String): Int {
val n = s.length
var ans = 0
val index = IntArray(128) // current index of character
// try to extend the range [i, j]
var j = 0
var i = 0
while (j < n) {
i = Math.max(index[s[j].toInt()], i)
ans = Math.max(ans, j - i + 1)
index[s[j].toInt()] = j + 1
j++
}
return ans
}
}
fun main(args: Array<String>) {
val str = "abcabcdsajdhjaskld"
println(Solution().lengthOfLongestSubstring3(str))
}