-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRepeatedString.cpp
47 lines (40 loc) · 1.39 KB
/
RepeatedString.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <bits/stdc++.h>
using namespace std;
// problem : https://www.hackerrank.com/challenges/repeated-string/problem
// soultion ==============================================================
long repeatedString(string s, long n) {
// in case s = 'a'
if(s.size() < 1 && s[0] == 'a') return n;
// for store last substring important for correct calculating
string S ="";
// k => store last substring length
// as => total a's
long k = 0 , as = 0;
// first loop just for getting how many a's in s
for(long i = 0; i < s.size();i+=1){
if(s[i] == 'a') as+=1;
}
// if a's 0 return is 0 :)
if(as == 0) return 0;
// else we calculating how many a's in 'n' length
/*
for example : n = 10 & s = "aba"
as = as * floor( n/s.size() ) => 2 * floor( 10 / 3 ) = 6
*/
as = as * (floor(n/s.size()));
// substring size is how many s in 'n' length
/*
for example : n = 10 & s.len is 3
k = n - ( floor( n/s.size() ) * s.size() )
k = 10 - (floor( 10/3 ) * 3) = 1
*/ // soo substring length must be 1
k = n - (floor(n/s.size()) * s.size());
// S = substring we want for last operation
S = s.substr(0,k);
// last operation is get how many a's in substring & updating a's value
for(long i = 0 ; i < S.size() ; i +=1){
if(S[i] == 'a') as +=1;
}
return as;
}
// =======================================================================