Repeated String Hackerrank Solution - java
For Explanation Watch Video :
Code :
import java.io.*;
import java.math.*;
import java.security.*;
import java.text.*;
import java.util.*;
import java.util.concurrent.*;
import java.util.function.*;
import java.util.regex.*;
import java.util.stream.*;
import static java.util.stream.Collectors.joining;
import static java.util.stream.Collectors.toList;
class Result {
public static int noOfA(String s,long rem){
int count = 0;
for(int i=0;i<rem;i++){
if(s.charAt(i)=='a'){
count++;
}
}
return count;
}
public static long repeatedString(String s, long n) {
int a_count = 0;
for(int i=0;i<s.length();i++){
if(s.charAt(i)=='a'){
a_count++;
}
}
long rem = n % s.length();
if(rem==0){
return (n/s.length()) * a_count;
}
int count = noOfA(s,rem);
return (n/s.length()) * a_count + count;
}
}
public class Solution {
public static void main(String[] args) throws IOException {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bufferedWriter = new BufferedWriter(new FileWriter(System.getenv("OUTPUT_PATH")));
String s = bufferedReader.readLine();
long n = Long.parseLong(bufferedReader.readLine().trim());
long result = Result.repeatedString(s, n);
bufferedWriter.write(String.valueOf(result));
bufferedWriter.newLine();
bufferedReader.close();
bufferedWriter.close();
}
}
Comments
Post a Comment