Untitled

mail@pastecode.io avatar
unknown
plain_text
a month ago
743 B
2
Indexable
Never
class Solution {
public:
    int minimumTimeToInitialState(string word, int k) {
        int p=1; //time greater than 0
        int n=word.size();
        vector<int>z=z_function(word);

        for(;p*k<n;p++){
            if(z[k*p]>=n-k*p){
                return p;
            }
        }
        return p;// we had to remove all
    }

    vector<int> z_function(string &s) {
    int n = s.size();
    vector<int> z(n);
    int l = 0, r = 0;
    for(int i = 1; i < n; i++) {
        if(i < r) {
            z[i] = min(r - i, z[i - l]);
        }
        while(i + z[i] < n && s[z[i]] == s[i + z[i]]) {
            z[i]++;
        }
        if(i + z[i] > r) {
            l = i;
            r = i + z[i];
        }
    }
    return z;
}
};
Leave a Comment