Valid Palindrome II
In Valid Palindrome II, you are given a string s. Your goal is to determine if it is possible to make the string a palindrome by deleting at most one character from it.
A palindrome is a string that reads the same forward and backward.
For example, s = "aba" is already a palindrome, so return true. If s = "abca", you can delete either u0027bu0027 or u0027cu0027 to make it "aca" or "aba", so return true. If s = "abc", no matter which character you delete, it wonu0027t be a palindrome, so return false.
Example Input & Output
Already a palindrome.
You can delete u0027cu0027 to get "aba".
Algorithm Flow

Solution Approach
A good way to solve this problem is to walk through the array once, collect every even number in the order it appears, convert those even values to strings, and finally join them with commas. The problem is not asking you to sort anything or change the order of the numbers. It only wants you to filter the array by one rule and then format the kept values as text.
The key rule is parity. A number is even when num % 2 === 0. That works for positive values, zero, and negative values too, so examples like -2 should still be included. If a number is odd, you simply skip it. The order matters, so you should append evens exactly as you encounter them instead of collecting them into a structure that changes their position.
In JavaScript, a very direct version looks like this:
This does the whole job in three clear stages. First, scan the array. Second, keep only the even values and convert them to strings. Third, use join(',') to build the final comma-separated result. If there are no even numbers, then evens stays empty and join(',') naturally returns an empty string, which is exactly what the problem expects.
This also handles edge cases cleanly. If the input is empty, the answer is an empty string. If there is one even number, the output is just that number with no extra commas. If all numbers are even, all of them appear in the output in their original order.
The time complexity is O(n) because you inspect each element once. The extra space is O(n) in the worst case if all numbers are even and all get stored in the result list before joining.
So the full strategy is: scan the array, keep the even numbers in order, convert them to strings, and join them with commas to produce the final formatted string.
Best Answers
import java.util.stream.*;class Solution{public String even_to_string(Object n){int[]a=(int[])n;return IntStream.of(a).filter(x->x%2==0).mapToObj(String::valueOf).collect(Collectors.joining(","));}}Comments (0)
Join the Discussion
Share your thoughts, ask questions, or help others with this Challenge.
