Click here to Skip to main content
15,890,123 members

Comments by Animesh _1SG20IS008 (Top 12 by date)

Animesh _1SG20IS008 23-May-23 6:58am View    
Already did
Animesh _1SG20IS008 23-May-23 1:38am View    
Got the solution from youtube but can you please explain the significance of 'sz' variable.
string kPeriodic(string s, int k){
// code here
int n=s.size();
vector<int>m(26);
for (auto e:s) m[e-'a']++;
int sz=0;
int id=0;
string ans=string(n, '_');

while(ans[id]=='_'){
sz++;

ans[id]='.';
id+=k;
id%=n;

}

for(auto e:m) if(e%sz!=0) return "-1";
ans=string(n, '_');
id=0;
int cnt=0, val=0;
while(cnt!=n){
while(ans [id]!='_')id++; //find a blank pos

while(m[val]==0)val++;

while(ans[id]=='_'){
cnt++;

ans[id]='a'+val;
id+=k;
id%=n;

}

m[val]-=sz;
}
return ans;
}
Animesh _1SG20IS008 23-May-23 1:30am View    
Dynamic Programming
Animesh _1SG20IS008 23-May-23 1:30am View    
Dynamic Programming
Animesh _1SG20IS008 22-May-23 23:52pm View    
You can look on chatgpt for DP solution.As I'm not familiar with DP that's why came here looking for non DP solution.