Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ My Kotlin exercises with tests from [Leetcode](https://leetcode.com/kotlerdev)

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

[//]: # (- [String](#string))
- [String](#string)

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

Expand Down Expand Up @@ -55,7 +55,13 @@ My Kotlin exercises with tests from [Leetcode](https://leetcode.com/kotlerdev)
| 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 |
| 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 |
| 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 |
| 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 |
| 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 |

#### String

| ID | Description | Solution | Test | Difficulty |
|:--:|:------------------------------------------------------------------------------------------------------|:------------------------------------------------------------:|:------------------------------------------------------------:|:----------:|
| 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 |

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

Expand Down
42 changes: 42 additions & 0 deletions src/main/kotlin/exercise/medium/id3/Description3.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
[//]: # (Copyright [2023] [Anton Kotler kotler.developer@gmail.com] License MIT)

# Longest Substring Without Repeating Characters

![Difficulty](https://img.shields.io/badge/Difficulty-Medium-548af7)

Given a string `s`, find the length of the longest substring without repeating characters.

Example 1:

```
Input: s = "abcabcbb"
Output: 3
```

Example 2:

```
Input: s = "bbbbb"
Output: 1
Explanation: The answer is `b`, with the length of 1.
```

Example 3:

```
Input: s = "pwwkew"
Output: 3
Explanation: The answer is `wke`, with the length of 3.
Notice that the answer must be a substring, `pwke` is a subsequence and not a substring.
```

Constraints:

- `0 <= s.length <= 5 * 10^4`
- `s` consists of English letters, digits, symbols and spaces.

| ID | Description | Solution | Test | Difficulty |
|:--:|:-----------------------------------------------|:--------------------------:|:------------------------------------------------------------------------------:|:----------:|
| 3 | Longest Substring Without Repeating Characters | [solution](./Solution3.kt) | [test](../../../../../../src/test/kotlin/exercise/medium/id3/Solution3Test.kt) | Medium |

:top: [Back to all topics](https://github.com/kotler-dev/kotlin-leetcode)
88 changes: 88 additions & 0 deletions src/main/kotlin/exercise/medium/id3/Solution3.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package exercise.medium.id3


class Solution3 {
fun lengthOfLongestSubstring(s: String): Int {

if (s.length == 0) return 0
if (s.length == 1) return 1

var longest: String = ""
var maxLength: Int = 0
var start = 0


for (l in start..<s.length) {

longest = ""
if (s.length - 1 - l < maxLength) break

for (i in l..<s.length) {
if (s[i] !in longest) {
longest += s[i]
if (longest.length > maxLength) maxLength = longest.length
} else {
if (longest.length > maxLength) maxLength = longest.length
println("s: ${s}, longest: ${longest}")
longest = "" + s[i]
}
}
start += longest.length - 1
}
return maxLength
}

/*

// Output Limit Exceeded
fun lengthOfLongestSubstring(s: String): Int {

if (s.length == 0) return 0
if (s.length == 1) return 1

var longest: String = ""
var maxLength: Int = 0

for (l in 0..<s.length) {
longest = ""
for (i in l..<s.length) {
if (s[i] !in longest) {
longest += s[i]
if (longest.length > maxLength) maxLength = longest.length
} else {
if (longest.length > maxLength) maxLength = longest.length
println("s: ${s}, longest: ${longest}")
longest = "" + s[i]
}
}
}
return maxLength
}

fun lengthOfLongestSubstring(s: String): Int {

if (s.length == 0) return 0
if (s.length == 1) return 1

var longest: String = ""
var maxLength: Int = 0

// прохождение по символу всей строки
// символ + 1 != (входит в подстроку) увеличить макс длину вложенной строки
// если встретился повторный символ, сброс подстроки

for (index in 0..<s.length) {
if (s[index] !in longest) {
longest += s[index]
if (longest.length > maxLength) maxLength = longest.length
} else {
if (longest.length > maxLength) maxLength = longest.length
println("s: ${s}, longest: ${longest}")
longest = "" + s[index]
}
}
return maxLength
}

*/
}
31 changes: 31 additions & 0 deletions src/test/kotlin/exercise/medium/id3/Solution3Test.kt

Large diffs are not rendered by default.