We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
public static long repeatedString(String s, long n) {
// Write your code here
long strLength = s.length();
// Count occurrences of 'a' in the original string
long countInOriginal = s.chars().filter(ch -> ch == 'a').count();
// Calculate full repetitions of `s` in `n` characters
long fullRepetitions = n / strLength;
// Calculate remaining characters
long remainder = n % strLength;
// Count occurrences of 'a' in the remainder substring
long countInRemainder = s.substring(0, (int) remainder).chars().filter(ch -> ch == 'a').count();
// Total occurrences of 'a'
return (fullRepetitions * countInOriginal) + countInRemainder;
}
Cookie support is required to access HackerRank
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 →
public static long repeatedString(String s, long n) { // Write your code here long strLength = s.length();