You are viewing a single comment's thread. Return to all comments →
@Raulkg, nice approach without the complication of running out of heap memory with a StringBuilder.
Please check your solution for the test case of
ababa
with 3 as the length. As @pruvi007 pointed out, the output is incorrect.
Here's my modified code:
public static void main(String[] args) { Scanner in = new Scanner(System.in); String s = in.next(); long n = in.nextLong(); long count = 0; if(n > s.length()) { long repeat = n/s.length(); long diff = n % repeat; for(char c: s.toCharArray()) { if(c == 'a') count++; } count = count * repeat; for(int i=0;i<diff;i++) { if(s.charAt(i) == 'a') count++; } } else { int len = (int) n; s = s.substring(0,len+1); for(char c: s.toCharArray()) { if(c == 'a') count++; } } System.out.println(count); }
Seems like cookies are disabled on this browser, please enable them to open this website
Repeated String
You are viewing a single comment's thread. Return to all comments →
@Raulkg, nice approach without the complication of running out of heap memory with a StringBuilder.
Please check your solution for the test case of
ababa
with 3 as the length. As @pruvi007 pointed out, the output is incorrect.
Here's my modified code: