1
1
package g3401_3500.s3448_count_substrings_divisible_by_last_digit
2
2
3
- // #Hard #String #Dynamic_Programming #2025_02_11_Time_29_ms_ (77.78%)_Space_41.05_MB_ (77.78%)
3
+ // #Hard #String #Dynamic_Programming #2025_02_11_Time_28_ms_ (77.78%)_Space_40.27_MB_ (77.78%)
4
4
5
+ @Suppress(" kotlin:S107" )
5
6
class Solution {
6
7
fun countSubstrings (s : String ): Long {
7
8
val n = s.length
@@ -11,17 +12,17 @@ class Solution {
11
12
computeModArrays(s, p3, p7, p9)
12
13
val freq3 = LongArray (3 )
13
14
val freq9 = LongArray (9 )
14
- val freq7 = Array <LongArray ? >(6 ) { LongArray (7 ) }
15
+ val freq7 = Array <LongArray >(6 ) { LongArray (7 ) }
15
16
val inv7 = intArrayOf(1 , 5 , 4 , 6 , 2 , 3 )
16
17
return countValidSubstrings(s, p3, p7, p9, freq3, freq9, freq7, inv7)
17
18
}
18
19
19
20
private fun computeModArrays (s : String , p3 : IntArray , p7 : IntArray , p9 : IntArray ) {
20
- p3[0 ] = (s.get( 0 ) .code - ' 0' .code) % 3
21
- p7[0 ] = (s.get( 0 ) .code - ' 0' .code) % 7
22
- p9[0 ] = (s.get( 0 ) .code - ' 0' .code) % 9
21
+ p3[0 ] = (s[ 0 ] .code - ' 0' .code) % 3
22
+ p7[0 ] = (s[ 0 ] .code - ' 0' .code) % 7
23
+ p9[0 ] = (s[ 0 ] .code - ' 0' .code) % 9
23
24
for (i in 1 .. < s.length) {
24
- val dig = s.get(i) .code - ' 0' .code
25
+ val dig = s[i] .code - ' 0' .code
25
26
p3[i] = (p3[i - 1 ] * 10 + dig) % 3
26
27
p7[i] = (p7[i - 1 ] * 10 + dig) % 7
27
28
p9[i] = (p9[i - 1 ] * 10 + dig) % 9
@@ -35,17 +36,17 @@ class Solution {
35
36
p9 : IntArray ,
36
37
freq3 : LongArray ,
37
38
freq9 : LongArray ,
38
- freq7 : Array <LongArray ? >,
39
+ freq7 : Array <LongArray >,
39
40
inv7 : IntArray ,
40
41
): Long {
41
42
var ans: Long = 0
42
43
for (j in 0 .. < s.length) {
43
- val d = s.get(j) .code - ' 0' .code
44
+ val d = s[j] .code - ' 0' .code
44
45
if (d != 0 ) {
45
46
ans + = countDivisibilityCases(s, j, d, p3, p7, p9, freq3, freq9, freq7, inv7)
46
47
}
47
48
freq3[p3[j]]++
48
- freq7[j % 6 ]!! [p7[j]] = freq7[j % 6 ]!! [p7[j]] + 1
49
+ freq7[j % 6 ][p7[j]] = freq7[j % 6 ][p7[j]] + 1
49
50
freq9[p9[j]]++
50
51
}
51
52
return ans
@@ -60,7 +61,7 @@ class Solution {
60
61
p9 : IntArray ,
61
62
freq3 : LongArray ,
62
63
freq9 : LongArray ,
63
- freq7 : Array <LongArray ? >,
64
+ freq7 : Array <LongArray >,
64
65
inv7 : IntArray ,
65
66
): Long {
66
67
var ans: Long = 0
@@ -84,7 +85,7 @@ class Solution {
84
85
if (j == 0 ) {
85
86
return 1
86
87
}
87
- val num = (s.get( j - 1 ) .code - ' 0' .code) * 10 + (s.get(j) .code - ' 0' .code)
88
+ val num = (s[ j - 1 ] .code - ' 0' .code) * 10 + (s[j] .code - ' 0' .code)
88
89
return (if (num % 4 == 0 ) j + 1 else 1 ).toLong()
89
90
}
90
91
@@ -93,20 +94,20 @@ class Solution {
93
94
return 1
94
95
}
95
96
if (j == 1 ) {
96
- val num = (s.get( 0 ) .code - ' 0' .code) * 10 + 8
97
+ val num = (s[ 0 ] .code - ' 0' .code) * 10 + 8
97
98
return (if (num % 8 == 0 ) 2 else 1 ).toLong()
98
99
}
99
- val num3 = (s.get( j - 2 ) .code - ' 0' .code) * 100 + (s.get( j - 1 ) .code - ' 0' .code) * 10 + 8
100
- val num2 = (s.get( j - 1 ) .code - ' 0' .code) * 10 + 8
100
+ val num3 = (s[ j - 2 ] .code - ' 0' .code) * 100 + (s[ j - 1 ] .code - ' 0' .code) * 10 + 8
101
+ val num2 = (s[ j - 1 ] .code - ' 0' .code) * 10 + 8
101
102
return (if (num3 % 8 == 0 ) j - 1 else 0 ) + (if (num2 % 8 == 0 ) 1 else 0 ) + 1L
102
103
}
103
104
104
- private fun countDivisibilityBy7 (j : Int , p7 : IntArray , freq7 : Array <LongArray ? >, inv7 : IntArray ): Long {
105
+ private fun countDivisibilityBy7 (j : Int , p7 : IntArray , freq7 : Array <LongArray >, inv7 : IntArray ): Long {
105
106
var ans = (if (p7[j] == 0 ) 1L else 0L )
106
107
for (m in 0 .. 5 ) {
107
108
val idx = ((j % 6 ) - m + 6 ) % 6
108
109
val req = (p7[j] * inv7[m]) % 7
109
- ans + = freq7[idx]!! [req]
110
+ ans + = freq7[idx][req]
110
111
}
111
112
return ans
112
113
}
0 commit comments