Skip to content

Improved task 30 #769

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Mar 4, 2025
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1527,7 +1527,7 @@ implementation 'com.github.javadev:leetcode-in-kotlin:1.34'
|-|-|-|-|-|-
| 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
| 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
| 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
| 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
| 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

#### Top Interview 150 Matrix
Expand Down
Original file line number Diff line number Diff line change
@@ -1,47 +1,44 @@
package g0001_0100.s0030_substring_with_concatenation_of_all_words

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

class Solution {
fun findSubstring(s: String, words: Array<String>): List<Int> {
val indices: MutableList<Int> = ArrayList()
if (words.size == 0) {
return indices
val ans: MutableList<Int> = ArrayList<Int>()
val n1 = words[0].length
val n2 = s.length
val map1: MutableMap<String, Int> = HashMap<String, Int>()
for (ch in words) {
map1.put(ch, map1.getOrDefault(ch, 0) + 1)
}
// Put each word into a HashMap and calculate word frequency
val wordMap: MutableMap<String, Int> = HashMap()
for (word in words) {
wordMap[word] = wordMap.getOrDefault(word, 0) + 1
}
val wordLength = words[0].length
val window = words.size * wordLength
for (i in 0 until wordLength) {
// move a word's length each time
for (i in 0..<n1) {
var left = i
var j = i
while (j + window <= s.length) {
// get the subStr
val subStr = s.substring(j, j + window)
val map: MutableMap<String, Int> = HashMap()
// start from the last word
for (k in words.indices.reversed()) {
// get the word from subStr
val word = subStr.substring(k * wordLength, (k + 1) * wordLength)
val count = map.getOrDefault(word, 0) + 1
// if the num of the word is greater than wordMap's, move (k * wordLength) and
// break
if (count > wordMap.getOrDefault(word, 0)) {
j = j + k * wordLength
break
} else if (k == 0) {
indices.add(j)
} else {
map[word] = count
var c = 0
val map2: MutableMap<String, Int> = HashMap<String, Int>()
while (j + n1 <= n2) {
val word1 = s.substring(j, j + n1)
j += n1
if (map1.containsKey(word1)) {
map2.put(word1, map2.getOrDefault(word1, 0) + 1)
c++
while (map2[word1]!! > map1[word1]!!) {
val word2 = s.substring(left, left + n1)
map2.put(word2, map2[word2]!! - 1)
left += n1
c--
}
if (c == words.size) {
ans.add(left)
}
} else {
map2.clear()
c = 0
left = j
}
j = j + wordLength
}
}
return indices
return ans
}
}