-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRepeatedString.js
More file actions
29 lines (25 loc) · 785 Bytes
/
RepeatedString.js
File metadata and controls
29 lines (25 loc) · 785 Bytes
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
/**
* https://www.hackerrank.com/challenges/repeated-string/problem?h_l=interview&playlist_slugs%5B%5D=interview-preparation-kit&playlist_slugs%5B%5D=warmup
*/
function repeatedString(s, n) {
const sArr = s.split("");
const sLen = s.length;
const aOriginNum = sArr.filter(c => c === "a").length;
let aInfiniteNum = aOriginNum * Math.floor(n / sLen);
let gap = n % sLen;
if (gap === 0) return aInfiniteNum;
const cutString = s.substring(0, gap);
const cutArr = cutString.split("");
const aCutNum = cutArr.filter(c => c === "a").length;
aInfiniteNum += aCutNum;
return aInfiniteNum;
}
let s, n, ans;
s = "aba";
n = 10;
ans = repeatedString(s, n);
console.log(ans); //7
s = "a";
n = 1000000000000;
ans = repeatedString(s, n);
console.log(ans); //1000000000000