Skip to content

Commit f5ddc7b

Browse files
committed
feat: add the solution of Longest Substring Without Repeating Characters(003) with kotlin.
1 parent 16a1604 commit f5ddc7b

File tree

3 files changed

+93
-1
lines changed

3 files changed

+93
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
| :-----------------: | ------------------------------------------------------------ | :---------: | :------------: |:-----------: | :-------------: |
2828
| [001][001-question] | [Two Sum][001-tips] | [Easy][E] | [][001-java] | [][001-js] | [][001-kotlin] |
2929
| [002][002-question] | [Add Two Numbers][002-tips] | [Medium][M] | [][002-java] | | [][002-kotlin] |
30-
| [003][003-question] | [Longest Substring Without Repeating Characters][003-tips] | [Medium][M] | [][003-java] | | |
30+
| [003][003-question] | [Longest Substring Without Repeating Characters][003-tips] | [Medium][M] | [][003-java] | | [][003-kotlin] |
3131
| [004][004-question] | [Median of Two Sorted Arrays][004-tips] | [Hard][H] | [][004-java] | | |
3232
| [005][005-question] | [Longest Palindromic Substring][005-tips] | [Medium][M] | [][005-java] | | |
3333
| [006][006-question] | [ZigZag Conversion][006-tips] | [Medium][M] | [][006-java] | | |
@@ -489,6 +489,7 @@
489489

490490
[001-kotlin]: ./src/_001/kotlin/Solution.kt
491491
[002-kotlin]: ./src/_002/kotlin/Solution.kt
492+
[003-kotlin]: ./src/_003/kotlin/Solution.kt
492493
[007-kotlin]: ./src/_007/kotlin/Solution.kt
493494
[009-kotlin]: ./src/_009/kotlin/Solution.kt
494495
[013-kotlin]: ./src/_013/kotlin/Solution.kt

src/_003/kotlin/Solution.kt

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
package _003.kotlin
2+
3+
import java.util.HashSet
4+
import java.util.HashMap
5+
6+
7+
8+
9+
10+
/**
11+
* @author relish
12+
* @since 2018/10/20
13+
*/
14+
class Solution {
15+
fun lengthOfLongestSubstring1(s: String): Int {
16+
val n = s.length
17+
val set = HashSet<Char>()
18+
var ans = 0
19+
var i = 0
20+
var j = 0
21+
while (i < n && j < n) {
22+
// try to extend the range [i, j]
23+
if (!set.contains(s[j])) {
24+
set.add(s[j++])
25+
ans = Math.max(ans, j - i)
26+
} else {
27+
set.remove(s[i++])
28+
}
29+
}
30+
return ans
31+
}
32+
33+
34+
fun lengthOfLongestSubstring2(s: String): Int {
35+
val n = s.length
36+
var ans = 0
37+
val map = HashMap<Char, Int>() // current index of character
38+
// try to extend the range [i, j]
39+
var j = 0
40+
var i = 0
41+
while (j < n) {
42+
if (map.containsKey(s[j])) {
43+
i = Math.max(map.get(s[j])!!, i)
44+
}
45+
ans = Math.max(ans, j - i + 1)
46+
map[s[j]] = j + 1
47+
j++
48+
}
49+
return ans
50+
}
51+
52+
fun lengthOfLongestSubstring3(s: String): Int {
53+
val n = s.length
54+
var ans = 0
55+
val index = IntArray(128) // current index of character
56+
// try to extend the range [i, j]
57+
var j = 0
58+
var i = 0
59+
while (j < n) {
60+
i = Math.max(index[s[j].toInt()], i)
61+
ans = Math.max(ans, j - i + 1)
62+
index[s[j].toInt()] = j + 1
63+
j++
64+
}
65+
return ans
66+
}
67+
}
68+
69+
fun main(args: Array<String>) {
70+
val str = "abcabcdsajdhjaskld"
71+
println(Solution().lengthOfLongestSubstring3(str))
72+
}

tips/003/README.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,25 @@ class Solution {
4040
}
4141
```
4242

43+
```kotlin
44+
class Solution {
45+
fun lengthOfLongestSubstring(s: String): Int {
46+
val n = s.length
47+
var ans = 0
48+
val index = IntArray(128) // current index of character
49+
// try to extend the range [i, j]
50+
var j = 0
51+
var i = 0
52+
while (j < n) {
53+
i = Math.max(index[s[j].toInt()], i)
54+
ans = Math.max(ans, j - i + 1)
55+
index[s[j].toInt()] = j + 1
56+
j++
57+
}
58+
return ans
59+
}
60+
}
61+
```
4362

4463
## 结语
4564

0 commit comments

Comments
 (0)