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 String funnyString(String s) {
int n = s.length();
// Since the differences are symmetric, you only need to iterate up to the midpoint of the string
for (int i = 0; i < n / 2; i++) {
// Calculate the differences for the forward and backward traversals
int leftDiff = Math.abs(s.charAt(i) - s.charAt(i + 1));
int rightDiff = Math.abs(s.charAt(n - i - 1) - s.charAt(n - i - 2));
if (leftDiff != rightDiff) {
return "Not Funny";
}
}
return "Funny";
}
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Funny String
You are viewing a single comment's thread. Return to all comments →