-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrepeatedString.java
More file actions
47 lines (39 loc) · 1.34 KB
/
repeatedString.java
File metadata and controls
47 lines (39 loc) · 1.34 KB
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
class repeatedString {
/*
* 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
*/
public static long repeatedString(String s, long n) {
// Write your code here
long s_in_n = n / s.length(); // number of strings in n
long timesInString = 0;
long remainder = n % s.length(); // remainder of s
// count number of a's in s
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) == 'a') timesInString ++;
}
timesInString *= s_in_n;
// count number of a's in remainder
for (int i = 0; i < remainder; i++) {
if (s.charAt(i) == 'a') timesInString ++;
}
return timesInString;
}
public static void main(String[] args) {
String s = "abcac";
long n = 10;
System.out.println(repeatedString(s, n));
}
}
/*
* https://www.hackerrank.com/challenges/repeated-string/problem?utm_campaign=challenge-recommendation&utm_medium=email&utm_source=24-hour-campaign
* count a's in original string
* count how many times s fits in n: times = n/s.length
* count remainder of partial string: a's inRemainder = n % s.length
* repeats = times + a's inRemainder
*
*/