February 22, 2022 Rookie SRM 10 Editorial
OddSum
This is pretty straightforward, and all we need is a loop… whenever we come across an odd number, we add it to the running sum.
public int getSum(int[] x) {
int ret = 0;
for (int i = 0; i < x.length; i++)
if (x[i] % 2 == 1)
ret += x[i];
return ret;
}
EllysPronunciation
Here, we loop though each word, and for each word, count how many consonants and vowels there are, noting the ones that have an equal number of each.
public int getGood(String[] words) {
int good = 0;
for (int i = 0; i < words.length; i++) {
int vowels = 0;
for (char ch : words[i].toCharArray())
if (ch == 'a' || ch == 'e' || ch == 'i' || ch == 'o' || ch == 'u')
vowels++;
if (vowels * 2 == words[i].length())
good++;
}
return good;
}
DevuAndGoodPalindromicString
Here, the problem gets a lot easier if we make one simple key observation: we only need to check for palindromes of length 2 or 3. Why? Because anything longer than that will still have a shorter palindrome at its center. So, we go from left to right, comparing each character to that which is adjacent or two characters away, in search of a palindrome.
public String isGoodPalindrome(String s) {
for (int i = 0; i < s.length(); i++) {
if (i - 1 >= 0 && s.charAt(i) == s.charAt(i - 1)) {
return "good";
}
if (i - 1 >= 0 && i + 1 < s.length() && s.charAt(i - 1) == s.charAt(i + 1)) {
return "good";
}
}
return "not good";
}
KassanErinn
Guest Blogger