Skip to content

Commit 436ec73

Browse files
committed
Improved 3448
1 parent a847798 commit 436ec73

File tree

1 file changed

+17
-16
lines changed
  • src/main/kotlin/g3401_3500/s3448_count_substrings_divisible_by_last_digit

1 file changed

+17
-16
lines changed

src/main/kotlin/g3401_3500/s3448_count_substrings_divisible_by_last_digit/Solution.kt

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
package g3401_3500.s3448_count_substrings_divisible_by_last_digit
22

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%)
44

5+
@Suppress("kotlin:S107")
56
class Solution {
67
fun countSubstrings(s: String): Long {
78
val n = s.length
@@ -11,17 +12,17 @@ class Solution {
1112
computeModArrays(s, p3, p7, p9)
1213
val freq3 = LongArray(3)
1314
val freq9 = LongArray(9)
14-
val freq7 = Array<LongArray?>(6) { LongArray(7) }
15+
val freq7 = Array<LongArray>(6) { LongArray(7) }
1516
val inv7 = intArrayOf(1, 5, 4, 6, 2, 3)
1617
return countValidSubstrings(s, p3, p7, p9, freq3, freq9, freq7, inv7)
1718
}
1819

1920
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
2324
for (i in 1..<s.length) {
24-
val dig = s.get(i).code - '0'.code
25+
val dig = s[i].code - '0'.code
2526
p3[i] = (p3[i - 1] * 10 + dig) % 3
2627
p7[i] = (p7[i - 1] * 10 + dig) % 7
2728
p9[i] = (p9[i - 1] * 10 + dig) % 9
@@ -35,17 +36,17 @@ class Solution {
3536
p9: IntArray,
3637
freq3: LongArray,
3738
freq9: LongArray,
38-
freq7: Array<LongArray?>,
39+
freq7: Array<LongArray>,
3940
inv7: IntArray,
4041
): Long {
4142
var ans: Long = 0
4243
for (j in 0..<s.length) {
43-
val d = s.get(j).code - '0'.code
44+
val d = s[j].code - '0'.code
4445
if (d != 0) {
4546
ans += countDivisibilityCases(s, j, d, p3, p7, p9, freq3, freq9, freq7, inv7)
4647
}
4748
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
4950
freq9[p9[j]]++
5051
}
5152
return ans
@@ -60,7 +61,7 @@ class Solution {
6061
p9: IntArray,
6162
freq3: LongArray,
6263
freq9: LongArray,
63-
freq7: Array<LongArray?>,
64+
freq7: Array<LongArray>,
6465
inv7: IntArray,
6566
): Long {
6667
var ans: Long = 0
@@ -84,7 +85,7 @@ class Solution {
8485
if (j == 0) {
8586
return 1
8687
}
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)
8889
return (if (num % 4 == 0) j + 1 else 1).toLong()
8990
}
9091

@@ -93,20 +94,20 @@ class Solution {
9394
return 1
9495
}
9596
if (j == 1) {
96-
val num = (s.get(0).code - '0'.code) * 10 + 8
97+
val num = (s[0].code - '0'.code) * 10 + 8
9798
return (if (num % 8 == 0) 2 else 1).toLong()
9899
}
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
101102
return (if (num3 % 8 == 0) j - 1 else 0) + (if (num2 % 8 == 0) 1 else 0) + 1L
102103
}
103104

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 {
105106
var ans = (if (p7[j] == 0) 1L else 0L)
106107
for (m in 0..5) {
107108
val idx = ((j % 6) - m + 6) % 6
108109
val req = (p7[j] * inv7[m]) % 7
109-
ans += freq7[idx]!![req]
110+
ans += freq7[idx][req]
110111
}
111112
return ans
112113
}

0 commit comments

Comments
 (0)