Skip to content

Commit 72b63a1

Browse files
committed
Implement Knuth–Morris–Pratt Algorithm
1 parent de3bc6e commit 72b63a1

File tree

6 files changed

+77
-8
lines changed

6 files changed

+77
-8
lines changed

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77

88
## Easy level
99

10-
1. [First bad version](https://leetcode.com/problems/first-bad-version/)[solution](src/main/kotlin/ru/romanow/easy/FirstBadVersion.kt), [test](src/test/kotlin/ru/romanow/FirstBadVersionTest.kt)
10+
1. [First bad version](https://leetcode.com/problems/first-bad-version/)[solution](src/main/kotlin/ru/romanow/easy/FirstBadVersion.kt), [test](src/test/kotlin/ru/romanow/easy/FirstBadVersionTest.kt)
1111
2. [Merge sorted array](https://leetcode.com/problems/merge-sorted-array/)[solution](src/main/kotlin/ru/romanow/easy/MergeSortedArrays.kt), [test](src/test/kotlin/ru/romanow/easy/MergeSortedArraysTest.kt), разбор на [YouTube](https://youtube.com/watch?v=qEHYFV0f6FQ)
12-
3. [Find the Index of the First Occurrence in a String](https://leetcode.com/problems/find-the-index-of-the-first-occurrence-in-a-string/)[solution](src/main/kotlin/ru/romanow/FirstOccurrenceInAString.kt), [test](src/test/kotlin/ru/romanow/easy/FirstOccurrenceInAStringTest.kt), **TODO**
12+
3. [Find the Index of the First Occurrence in a String](https://leetcode.com/problems/find-the-index-of-the-first-occurrence-in-a-string/)[solution](src/main/kotlin/ru/romanow/easy/FirstOccurrenceInAString.kt), [test](src/test/kotlin/ru/romanow/easy/FirstOccurrenceInAStringTest.kt), **TODO**
1313
4. [Longest Common Prefix](https://leetcode.com/problems/longest-common-prefix/)[solution](src/main/kotlin/ru/romanow/easy/LongestCommonPrefix.kt), [test](src/test/kotlin/ru/romanow/easy/LongestCommonPrefixTest.kt)
1414
5. [Valid Palindrome](https://leetcode.com/problems/valid-palindrome/)[solution](src/main/kotlin/ru/romanow/easy/ValidPalindrome.kt), [test](src/test/kotlin/ru/romanow/easy/ValidPalindromeTest.kt), **TODO**
1515
6. [Two Sum](https://leetcode.com/problems/two-sum/)[solution](src/main/kotlin/ru/romanow/easy/TwoSum.kt), [test](src/test/kotlin/ru/romanow/easy/TwoSumTest.kt)
@@ -67,5 +67,6 @@
6767
9. In-order Deep First Search Traversal – [solution](src/main/kotlin/ru/romanow/algorithms/InOrderDeepFirstSearch.kt), [test](src/test/kotlin/ru/romanow/algorithms/InOrderDeepFirstSearchTest.kt)
6868
10. Post-order Deep First Search Traversal – [solution](src/main/kotlin/ru/romanow/algorithms/PostOrderDeepFirstSearch.kt), [test](src/test/kotlin/ru/romanow/algorithms/PostOrderDeepFirstSearchTest.kt)
6969
11. Breadth First Search Traversal – [solution](src/main/kotlin/ru/romanow/algorithms/BreadthFirstSearch.kt), [test](src/test/kotlin/ru/romanow/algorithms/BreadthFirstSearchTest.kt)
70+
12. Knuth–Morris–Pratt Algorithm – [solution](src/main/kotlin/ru/romanow/algorithms/KnuthMorrisPrattAlgorithm.kt), [test](src/test/kotlin/ru/romanow/algorithms/KnuthMorrisPrattAlgorithmTest.kt)
7071

7172
[//]: # (@formatter:on)
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package ru.romanow.algorithms
2+
3+
class KnuthMorrisPrattAlgorithm {
4+
fun strStr(haystack: String, needle: String): Int {
5+
val lps = computeLps(needle)
6+
var i = 0
7+
var j = 0
8+
while (i < haystack.length) {
9+
if (haystack[i] == needle[j]) {
10+
i++
11+
j++
12+
}
13+
14+
if (j == needle.length) {
15+
return i - j
16+
} else if (i < haystack.length && haystack[i] != needle[j]) {
17+
if (j != 0) {
18+
j = lps[j - 1]
19+
} else {
20+
i++
21+
}
22+
}
23+
}
24+
return -1
25+
}
26+
27+
private fun computeLps(pattern: String): IntArray {
28+
val lps = IntArray(pattern.length)
29+
var length = 0
30+
var idx = 1
31+
32+
while (idx < pattern.length) {
33+
if (pattern[idx] == pattern[length]) {
34+
length++
35+
lps[idx] = length
36+
idx++
37+
} else {
38+
if (length != 0) {
39+
length = lps[length - 1]
40+
} else {
41+
lps[idx] = 0
42+
idx++
43+
}
44+
}
45+
}
46+
47+
return lps
48+
}
49+
}

src/main/kotlin/ru/romanow/algorithms/RedBlackTree.kt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ class RedBlackTree {
2424
}
2525

2626
fun remove(key: Int) {
27-
2827
}
2928

3029
private fun insert(n: TreeNode?, key: Int, value: Int): TreeNode {

src/main/kotlin/ru/romanow/medium/LongestCommonSubsequence.kt

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,6 @@ package ru.romanow.medium
1212
*/
1313
class LongestCommonSubsequence {
1414
fun longestCommonSubsequence(text1: String, text2: String): Int {
15-
var k = 0
16-
for (i in text1.indices) {
17-
}
1815
return 0
1916
}
2017
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package ru.romanow.algorithms
2+
3+
import org.assertj.core.api.Assertions.assertThat
4+
import org.junit.jupiter.api.extension.ExtensionContext
5+
import org.junit.jupiter.params.ParameterizedTest
6+
import org.junit.jupiter.params.provider.Arguments
7+
import org.junit.jupiter.params.provider.Arguments.of
8+
import org.junit.jupiter.params.provider.ArgumentsProvider
9+
import org.junit.jupiter.params.provider.ArgumentsSource
10+
import java.util.stream.Stream
11+
12+
class KnuthMorrisPrattAlgorithmTest {
13+
14+
@ArgumentsSource(ValueProvider::class)
15+
@ParameterizedTest(name = "#{index} – Occurrence {1} index in a string {0} is {2}")
16+
fun strStr(haystack: String, needle: String, result: Int) {
17+
val obj = KnuthMorrisPrattAlgorithm()
18+
assertThat(obj.strStr(haystack, needle)).isEqualTo(result)
19+
}
20+
21+
internal class ValueProvider : ArgumentsProvider {
22+
override fun provideArguments(context: ExtensionContext): Stream<Arguments> =
23+
Stream.of(of("sadbutsad", "sad", 0), of("leetcode", "leeto", -1), of("a", "a", 0), of("a", "asd", -1))
24+
}
25+
}

src/test/kotlin/ru/romanow/algorithms/RedBlackTreeTest.kt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@ class RedBlackTreeTest {
1616
val obj = RedBlackTree()
1717
items.forEach { obj.put(it) }
1818
assertThat(obj.get(key)).isEqualTo(result)
19-
obj.remove(key)
20-
assertThat(obj.get(key)).isNull()
2119
}
2220

2321
internal class ValueProvider : ArgumentsProvider {

0 commit comments

Comments
 (0)