File tree Expand file tree Collapse file tree 1 file changed +13
-19
lines changed
src/main/kotlin/g3501_3600/s3557_find_maximum_number_of_non_intersecting_substrings Expand file tree Collapse file tree 1 file changed +13
-19
lines changed Original file line number Diff line number Diff line change 11package g3501_3600.s3557_find_maximum_number_of_non_intersecting_substrings
22
3- // #Medium #2025_05_25_Time_57_ms_(100.00%)_Space_55.58_MB_(100.00%)
4-
5- import java.util.LinkedList
6- import kotlin.math.max
3+ // #Medium #String #Hash_Table #Dynamic_Programming #Greedy
4+ // #2025_05_27_Time_28_ms_(70.59%)_Space_49.63_MB_(70.59%)
75
86class Solution {
97 fun maxSubstrings (s : String ): Int {
10- val last: Array <LinkedList <Int >> = Array (26 ) { LinkedList () }
11- val n = s.length
12- val dp = IntArray (n + 1 )
13- for (i in 0 .. < n) {
14- val c = s[i].code - ' a' .code
15- dp[i + 1 ] = dp[i]
16- for (j in last[c]) {
17- if (i - j + 1 >= 4 ) {
18- dp[i + 1 ] = max(dp[i + 1 ], dp[j] + 1 )
19- }
20- }
21- last[c].addLast(i)
22- if (last[c].size > 4 ) {
23- last[c].removeFirst()
8+ val prev = IntArray (26 )
9+ var r = 0
10+ prev.fill(- 1 )
11+ for (i in 0 .. < s.length) {
12+ val j = s[i].code - ' a' .code
13+ if (prev[j] != - 1 && i - prev[j] + 1 >= 4 ) {
14+ ++ r
15+ prev.fill(- 1 )
16+ } else if (prev[j] == - 1 ) {
17+ prev[j] = i
2418 }
2519 }
26- return dp[n]
20+ return r
2721 }
2822}
You can’t perform that action at this time.
0 commit comments