Skip to content

Commit 7e715e2

Browse files
authored
Merge pull request #11 from kotler-dev/exercise/id3
exercise/id3
2 parents 7ec6e1b + 3975d76 commit 7e715e2

File tree

4 files changed

+169
-2
lines changed

4 files changed

+169
-2
lines changed

README.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ My Kotlin exercises with tests from [Leetcode](https://leetcode.com/kotlerdev)
1717

1818
[//]: # (- [Linked List](#linked-list))
1919

20-
[//]: # (- [String](#string))
20+
- [String](#string)
2121

2222
[//]: # (- [Binary Tree](#binary-tree))
2323

@@ -55,7 +55,13 @@ My Kotlin exercises with tests from [Leetcode](https://leetcode.com/kotlerdev)
5555
| 217 | [Contains Duplicate](src/main/kotlin/exercise/easy/id217/Description217.md) | [solution](src/main/kotlin/exercise/easy/id217/Solution217.kt) | [test](src/test/kotlin/exercise/easy/id217/Solution217Test.kt) | Easy |
5656
| 283 | [Move Zeroes](src/main/kotlin/exercise/easy/id283/Description283.md) | [solution](src/main/kotlin/exercise/easy/id283/Solution283.kt) | [test](src/test/kotlin/exercise/easy/id283/Solution283Test.kt) | Easy |
5757
| 977 | [Squares of a Sorted Array](src/main/kotlin/exercise/easy/id977/Description977.md) | [solution](src/main/kotlin/exercise/easy/id977/Solution977.kt) | [test](src/test/kotlin/exercise/easy/id977/Solution977Test.kt) | Easy |
58-
| 57 | [Insert Internal](src/main/kotlin/exercise/medium/id57/Description57.md) | [solution](src/main/kotlin/exercise/medium/id57/Solution57.kt) | [test](src/test/kotlin/exercise/medium/id57/Solution57Test.kt) | Medium |
58+
| 57 | [Insert Internal](src/main/kotlin/exercise/medium/id57/Description57.md) | [solution](src/main/kotlin/exercise/medium/id57/Solution57.kt) | [test](src/test/kotlin/exercise/medium/id57/Solution57Test.kt) | Medium |
59+
60+
#### String
61+
62+
| ID | Description | Solution | Test | Difficulty |
63+
|:--:|:------------------------------------------------------------------------------------------------------|:------------------------------------------------------------:|:------------------------------------------------------------:|:----------:|
64+
| 3 | [Longest Substring Without Repeating Characters](src/main/kotlin/exercise/medium/id3/Description3.md) | [solution](src/main/kotlin/exercise/medium/id3/Solution3.kt) | [test](src/test/kotlin/exercise/medium/id3/Solution3Test.kt) | Medium |
5965

6066
[//]: # (https://www.techinterviewhandbook.org/grind75?weeks=26&hours=40&grouping=topics)
6167

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
[//]: # (Copyright [2023] [Anton Kotler kotler.developer@gmail.com] License MIT)
2+
3+
# Longest Substring Without Repeating Characters
4+
5+
![Difficulty](https://img.shields.io/badge/Difficulty-Medium-548af7)
6+
7+
Given a string `s`, find the length of the longest substring without repeating characters.
8+
9+
Example 1:
10+
11+
```
12+
Input: s = "abcabcbb"
13+
Output: 3
14+
```
15+
16+
Example 2:
17+
18+
```
19+
Input: s = "bbbbb"
20+
Output: 1
21+
Explanation: The answer is `b`, with the length of 1.
22+
```
23+
24+
Example 3:
25+
26+
```
27+
Input: s = "pwwkew"
28+
Output: 3
29+
Explanation: The answer is `wke`, with the length of 3.
30+
Notice that the answer must be a substring, `pwke` is a subsequence and not a substring.
31+
```
32+
33+
Constraints:
34+
35+
- `0 <= s.length <= 5 * 10^4`
36+
- `s` consists of English letters, digits, symbols and spaces.
37+
38+
| ID | Description | Solution | Test | Difficulty |
39+
|:--:|:-----------------------------------------------|:--------------------------:|:------------------------------------------------------------------------------:|:----------:|
40+
| 3 | Longest Substring Without Repeating Characters | [solution](./Solution3.kt) | [test](../../../../../../src/test/kotlin/exercise/medium/id3/Solution3Test.kt) | Medium |
41+
42+
:top: [Back to all topics](https://github.com/kotler-dev/kotlin-leetcode)
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
package exercise.medium.id3
2+
3+
4+
class Solution3 {
5+
fun lengthOfLongestSubstring(s: String): Int {
6+
7+
if (s.length == 0) return 0
8+
if (s.length == 1) return 1
9+
10+
var longest: String = ""
11+
var maxLength: Int = 0
12+
var start = 0
13+
14+
15+
for (l in start..<s.length) {
16+
17+
longest = ""
18+
if (s.length - 1 - l < maxLength) break
19+
20+
for (i in l..<s.length) {
21+
if (s[i] !in longest) {
22+
longest += s[i]
23+
if (longest.length > maxLength) maxLength = longest.length
24+
} else {
25+
if (longest.length > maxLength) maxLength = longest.length
26+
println("s: ${s}, longest: ${longest}")
27+
longest = "" + s[i]
28+
}
29+
}
30+
start += longest.length - 1
31+
}
32+
return maxLength
33+
}
34+
35+
/*
36+
37+
// Output Limit Exceeded
38+
fun lengthOfLongestSubstring(s: String): Int {
39+
40+
if (s.length == 0) return 0
41+
if (s.length == 1) return 1
42+
43+
var longest: String = ""
44+
var maxLength: Int = 0
45+
46+
for (l in 0..<s.length) {
47+
longest = ""
48+
for (i in l..<s.length) {
49+
if (s[i] !in longest) {
50+
longest += s[i]
51+
if (longest.length > maxLength) maxLength = longest.length
52+
} else {
53+
if (longest.length > maxLength) maxLength = longest.length
54+
println("s: ${s}, longest: ${longest}")
55+
longest = "" + s[i]
56+
}
57+
}
58+
}
59+
return maxLength
60+
}
61+
62+
fun lengthOfLongestSubstring(s: String): Int {
63+
64+
if (s.length == 0) return 0
65+
if (s.length == 1) return 1
66+
67+
var longest: String = ""
68+
var maxLength: Int = 0
69+
70+
// прохождение по символу всей строки
71+
// символ + 1 != (входит в подстроку) увеличить макс длину вложенной строки
72+
// если встретился повторный символ, сброс подстроки
73+
74+
for (index in 0..<s.length) {
75+
if (s[index] !in longest) {
76+
longest += s[index]
77+
if (longest.length > maxLength) maxLength = longest.length
78+
} else {
79+
if (longest.length > maxLength) maxLength = longest.length
80+
println("s: ${s}, longest: ${longest}")
81+
longest = "" + s[index]
82+
}
83+
}
84+
return maxLength
85+
}
86+
87+
*/
88+
}

src/test/kotlin/exercise/medium/id3/Solution3Test.kt

Lines changed: 31 additions & 0 deletions
Large diffs are not rendered by default.

0 commit comments

Comments
 (0)