2575. Find the Divisibility Array of a String#
You are given a string of length , starting from index 0, consisting of digits from to . You are also given a positive integer .
The divisibility array of , denoted as , is an integer array of length that satisfies:
- If the value represented by is divisible by , then
- Otherwise,
Return the divisibility array of .
Example 1:
Input: word = "998244353", m = 3
Output: [1,1,0,0,0,1,1,0,0]
Explanation: Only 4 prefixes can be divided by 3: "9", "99", "998244", and "9982443".
Example 2:
Input: word = "1010", m = 10
Output: [0,1,0,1]
Explanation: Only 2 prefixes can be divided by 10: "10" and "1010".
Note:
- consists of digits from to
Solution#
class Solution {
public:
vector<int> divisibilityArray(string word, int m) {
vector<int> res;
long long num = 0;
for (const auto &c : word) {
num = (num * 10 + (c - '0')) % m;
res.push_back(num == 0);
}
return res;
}
};