forked from adityabisoi/ds-algo-solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
solution.java
69 lines (56 loc) · 2.27 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
/*
Understanding question
- We need to return number of 'a' prsent in when string s is multiplied to form a string of length atlaest n
Solution - We will calculate number of 'a' present in strng s without appending s to it's end
- So once we have calculated that s contains say x a initially so then this just a unitary method question
if say string length is m then m contains x a's so n length dtring s will contain (x*n)/m
- There might be a case when n is not divisible by m and leaves remainder k
- Then we will traverse upto k in string s and add number of a in string s upto length k
*/
import java.io.*;
import java.util.*;
public class solution {
static long repeatedString(String s, long n) {
// count stores the number of a's in string s initially
long count=0;
int i=0;
// Calculating number of a's in string s initially
while(i<s.length()){
// When we encounter a in s increase count by one
if(s.charAt(i)=='a'){
count++;
}
i++;
}
// div is n/m as stated above
long div=n/s.length();
count=count*div;
//When n is not evenly divisible by m so we will traverse upto k in string s
// for k refer top comment section
if(div*s.length()!=n){
long rem=n-div*s.length(); //k
i=0;
while(i<rem){
// Increase count when we encounter a in s
if(s.charAt(i)=='a'){
count++;
}
i++;
}
}
//return the number of count of in s
return count;
}
private static final Scanner scanner = new Scanner(System.in);
public static void main(String[] args) throws IOException {
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));
String s = scanner.nextLine();
long n = scanner.nextLong();
scanner.skip("(\r\n|[\n\r\u2028\u2029\u0085])?");
long result = repeatedString(s, n);
bufferedWriter.write(String.valueOf(result));
bufferedWriter.newLine();
bufferedWriter.close();
scanner.close();
}
}