-
Notifications
You must be signed in to change notification settings - Fork 5
/
RepeatedString.java
32 lines (27 loc) · 1.04 KB
/
RepeatedString.java
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
// https://www.hackerrank.com/challenges/repeated-string/problem
package warmup;
import java.util.Scanner;
public class RepeatedString {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
String string = scanner.next();
long length = scanner.nextLong();
System.out.println(occurrencesOfA(string, length));
}
private static long occurrencesOfA(String string, long length) {
long repetitions = length / string.length() ;
int remainder = (int) (length % string.length());
int occurrencesInString = occurrencesOfA(string);
int occurrencesInRemainder = occurrencesOfA(string.substring(0, remainder));
return occurrencesInRemainder + repetitions * occurrencesInString ;
}
private static int occurrencesOfA(String string) {
int count = 0;
for (int index = 0 ; index < string.length() ; index++) {
if (string.charAt(index) == 'a') {
count++;
}
}
return count;
}
}