forked from RyanFehr/HackerRank
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Solution.java
48 lines (37 loc) · 1.23 KB
/
Solution.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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
//Problem: https://www.hackerrank.com/challenges/repeated-string
//Java 8
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;
public class Solution {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String s = in.next();
long n = in.nextLong();
long countForSubstring = 0;
long totalCount = 0;
//Determines how many a's are in a substring
for(int i = 0; i < s.length(); i++)
{
if(s.charAt(i) == 'a')
{
countForSubstring++;
}
}
//Determines how many complete substrings we will analyze
long divisor = n / s.length();
totalCount += divisor * countForSubstring;
//Determines how many characters in we will analyze towards the end of our n range
long remainder = n % s.length();
for(int i = 0; i < remainder; i++)
{
if(s.charAt(i) == 'a')
{
totalCount++;
}
}
System.out.println(totalCount);
}
}