Skip to content

Commit 0803691

Browse files
authored
Improved task 30
1 parent 67fecce commit 0803691

File tree

2 files changed

+31
-34
lines changed

2 files changed

+31
-34
lines changed

README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1527,7 +1527,7 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.34'
15271527
|-|-|-|-|-|-
15281528
| 0209 |[Minimum Size Subarray Sum](src/main/kotlin/g0201_0300/s0209_minimum_size_subarray_sum/Solution.kt)| Medium | Array, Binary_Search, Prefix_Sum, Sliding_Window | 315 | 96.73
15291529
| 0003 |[Longest Substring Without Repeating Characters](src/main/kotlin/g0001_0100/s0003_longest_substring_without_repeating_characters/Solution.kt)| Medium | Top_100_Liked_Questions, Top_Interview_Questions, String, Hash_Table, Sliding_Window, Big_O_Time_O(n)_Space_O(1), AI_can_be_used_to_solve_the_task | 201 | 87.28
1530-
| 0030 |[Substring with Concatenation of All Words](src/main/kotlin/g0001_0100/s0030_substring_with_concatenation_of_all_words/Solution.kt)| Hard | String, Hash_Table, Sliding_Window | 182 | 100.00
1530+
| 0030 |[Substring with Concatenation of All Words](src/main/kotlin/g0001_0100/s0030_substring_with_concatenation_of_all_words/Solution.kt)| Hard | String, Hash_Table, Sliding_Window | 14 | 98.62
15311531
| 0076 |[Minimum Window Substring](src/main/kotlin/g0001_0100/s0076_minimum_window_substring/Solution.kt)| Hard | Top_100_Liked_Questions, Top_Interview_Questions, String, Hash_Table, Sliding_Window, Big_O_Time_O(s.length())_Space_O(1) | 191 | 96.38
15321532

15331533
#### Top Interview 150 Matrix
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,44 @@
11
package g0001_0100.s0030_substring_with_concatenation_of_all_words
22

33
// #Hard #String #Hash_Table #Sliding_Window #Top_Interview_150_Sliding_Window
4-
// #2023_07_03_Time_182_ms_(100.00%)_Space_37.9_MB_(100.00%)
4+
// #2025_03_04_Time_14_ms_(98.62%)_Space_39.70_MB_(91.72%)
55

66
class Solution {
77
fun findSubstring(s: String, words: Array<String>): List<Int> {
8-
val indices: MutableList<Int> = ArrayList()
9-
if (words.size == 0) {
10-
return indices
8+
val ans: MutableList<Int> = ArrayList<Int>()
9+
val n1 = words[0].length
10+
val n2 = s.length
11+
val map1: MutableMap<String, Int> = HashMap<String, Int>()
12+
for (ch in words) {
13+
map1.put(ch, map1.getOrDefault(ch, 0) + 1)
1114
}
12-
// Put each word into a HashMap and calculate word frequency
13-
val wordMap: MutableMap<String, Int> = HashMap()
14-
for (word in words) {
15-
wordMap[word] = wordMap.getOrDefault(word, 0) + 1
16-
}
17-
val wordLength = words[0].length
18-
val window = words.size * wordLength
19-
for (i in 0 until wordLength) {
20-
// move a word's length each time
15+
for (i in 0..<n1) {
16+
var left = i
2117
var j = i
22-
while (j + window <= s.length) {
23-
// get the subStr
24-
val subStr = s.substring(j, j + window)
25-
val map: MutableMap<String, Int> = HashMap()
26-
// start from the last word
27-
for (k in words.indices.reversed()) {
28-
// get the word from subStr
29-
val word = subStr.substring(k * wordLength, (k + 1) * wordLength)
30-
val count = map.getOrDefault(word, 0) + 1
31-
// if the num of the word is greater than wordMap's, move (k * wordLength) and
32-
// break
33-
if (count > wordMap.getOrDefault(word, 0)) {
34-
j = j + k * wordLength
35-
break
36-
} else if (k == 0) {
37-
indices.add(j)
38-
} else {
39-
map[word] = count
18+
var c = 0
19+
val map2: MutableMap<String, Int> = HashMap<String, Int>()
20+
while (j + n1 <= n2) {
21+
val word1 = s.substring(j, j + n1)
22+
j += n1
23+
if (map1.containsKey(word1)) {
24+
map2.put(word1, map2.getOrDefault(word1, 0) + 1)
25+
c++
26+
while (map2[word1]!! > map1[word1]!!) {
27+
val word2 = s.substring(left, left + n1)
28+
map2.put(word2, map2[word2]!! - 1)
29+
left += n1
30+
c--
31+
}
32+
if (c == words.size) {
33+
ans.add(left)
4034
}
35+
} else {
36+
map2.clear()
37+
c = 0
38+
left = j
4139
}
42-
j = j + wordLength
4340
}
4441
}
45-
return indices
42+
return ans
4643
}
4744
}

0 commit comments

Comments
 (0)