-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrepeated_string.js
78 lines (61 loc) · 1.63 KB
/
repeated_string.js
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/* The problem statement for this challenge can be viewed at:
https://www.hackerrank.com/challenges/repeated-string/problem?h_l=interview&playlist_slugs%5B%5D=interview-preparation-kit&playlist_slugs%5B%5D=warmup */
// The boilerplate (lines 6-30 and 64-78) has been taken from the above source as well
/*
'use strict';
const fs = require('fs');
process.stdin.resume();
process.stdin.setEncoding('utf-8');
let inputString = '';
let currentLine = 0;
process.stdin.on('data', function(inputStdin) {
inputString += inputStdin;
});
process.stdin.on('end', function() {
inputString = inputString.split('\n');
main();
});
function readLine() {
return inputString[currentLine++];
}
*/
/*
* Complete the 'repeatedString' function below.
*
* The function is expected to return a LONG_INTEGER.
* The function accepts following parameters:
* 1. STRING s
* 2. LONG_INTEGER n
*/
function repeatedString(s, n) {
let str = '', index = 0;
// Getting the desired string of length n
while (index < s.length) {
str += s.charAt(index);
index += 1;
if (index === s.length) {
index = 0;
}
if (str.length === n) {
break;
}
}
let count = 0;
// Counting the number of a's
for (let i = 0; i < str.length; i++) {
if (str.charAt(i) === 'a') {
count += 1;
}
}
return count;
}
/*
function main() {
const ws = fs.createWriteStream(process.env.OUTPUT_PATH);
const s = readLine();
const n = parseInt(readLine().trim(), 10);
const result = repeatedString(s, n);
ws.write(result + '\n');
ws.end();
}
*/