[LeetCode] Find the Divisibility Array of a String

2575. Find the Divisibility Array of a String

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

class Solution {
public:
vector<int> divisibilityArray(string word, int m) {
vector<int> res;
long long r = 0;
for(int i = 0; i < word.size();i++) {
r = (r * 10 + (word[i] - '0')) % m;
if(r) res.push_back(0);
else res.push_back(1);
}
return res;
}
};

Author: Song Hayoung
Link: https://songhayoung.github.io/2023/02/26/PS/LeetCode/find-the-divisibility-array-of-a-string/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.