diff --git a/README.md b/README.md index 10778774c..f64885647 100644 --- a/README.md +++ b/README.md @@ -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 diff --git a/src/main/kotlin/g0001_0100/s0030_substring_with_concatenation_of_all_words/Solution.kt b/src/main/kotlin/g0001_0100/s0030_substring_with_concatenation_of_all_words/Solution.kt index f0f60f39c..b32dcb62c 100644 --- a/src/main/kotlin/g0001_0100/s0030_substring_with_concatenation_of_all_words/Solution.kt +++ b/src/main/kotlin/g0001_0100/s0030_substring_with_concatenation_of_all_words/Solution.kt @@ -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): List { - val indices: MutableList = ArrayList() - if (words.size == 0) { - return indices + val ans: MutableList = ArrayList() + val n1 = words[0].length + val n2 = s.length + val map1: MutableMap = HashMap() + 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 = 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.. = 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 = HashMap() + 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 } }