#2575
Medium Algorithms Find the divisibility array of a string
Array Math String
35.7% acceptance
Feb 25, 2026
586
25
You are given a 0-indexed string word of length n consisting of digits, and a positive integer m.
The divisibility array div of word is an integer array of length n such that:
div[i] = 1 if the numeric value of word[0,...,i] is divisible by m, or
div[i] = 0 otherwise.
Return the divisibility array of word.
Solution
Rust
Time O(n)
Space O(1)
impl Solution {
pub fn divisibility_array(word: String, m: i32) -> Vec<i32> {
let m = m as i64;
let mut rem = 0i64;
word.bytes()
.map(|b| {
rem = (rem * 10 + (b - b'0') as i64) % m;
if rem == 0 { 1 } else { 0 }
})
.collect()
}
}