RainX_69

Find the Divisibility Array of a String | OA | TRICKY

May 9th, 2023
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.38 KB | Source Code | 0 0
  1. https://leetcode.com/problems/find-the-divisibility-array-of-a-string/
  2.  
  3. You are given a 0-indexed string word of length n consisting of digits, and a positive integer m.
  4. The divisibility array div of word is an integer array of length n such that:
  5. div[i] = 1 if the numeric value of word[0,...,i] is divisible by m, or
  6. div[i] = 0 otherwise.
  7. Return the divisibility array of word.
  8.  
  9. Example 1:
  10. Input: word = "998244353", m = 3
  11. Output: [1,1,0,0,0,1,1,0,0]
  12. Explanation: There are only 4 prefixes that are divisible by 3: "9", "99", "998244", and "9982443".
  13.  
  14. Example 2:
  15. Input: word = "1010", m = 10
  16. Output: [0,1,0,1]
  17. Explanation: There are only 2 prefixes that are divisible by 10: "10", and "1010".
  18.  
  19.  
  20. Constraints:
  21. 1 <= n <= 10^5
  22. word.length == n
  23. word consists of digits from 0 to 9
  24. 1 <= m <= 10^9
  25.  
  26. ------------------------------------------------------------------------------------------------------------------------------------
  27.  
  28. class Solution {
  29. public:
  30.     vector<int> divisibilityArray(string word, int m) {
  31.         vector<int> res;
  32.         long long runningRem=0;
  33.         for(auto x: word){
  34.             runningRem=(long long)10*runningRem+1LL*(x-'0')%m;
  35.             runningRem%=m;
  36.             if(runningRem==0){
  37.                 res.push_back(1);
  38.             }
  39.             else{
  40.                 res.push_back(0);
  41.             }
  42.         }
  43.         return res;
  44.     }
  45. };
Advertisement
Add Comment
Please, Sign In to add comment