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 count = 0;
long div = n / s.length();
long rem = n % s.length();
if (n > s.length()) {
for (int i = 0; i < s.length(); i++) {
if (s.charAt(i) == 'a') {
count++;
}
}
if (div > 0) {
count *= div;
}
if (rem > 0) {
for (int i = 0; i < rem; i++) {
if (s.charAt(i) == 'a') {
count++;
}
}
}
} else {
for (int i = 0; i < n; i++) {
if (s.charAt(i) == 'a') {
count++;
}
}
}
return count;
}
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 →
java 8