Possible palindromes
// This is a sandbox to experiment with CoderPad's execution capabilities.
// It's a temporary, throw-away session only visible to you.
import java.io.*;
import java.util.*;
/*
* To execute Java, please define "static void main" on a class
* named Solution.
*
* If you need more classes, simply define them inline.
*/
class Solution {
public static void main(String[] args) {
Solution solution =new Solution();
String[] input = {
null,
"",
"GOOGLE"
};
for(int i=0;i< input.length;i++) {
System.out.println("Input: " + input[i] +
", Result: " + solution.possiblePalindromes(input[i]));
}
}
public Set<String> possiblePalindromes(String input){
Set<String> result = new HashSet<>();
if(input == null || input.length() < 1) {
return result;
}
for(int i=0;i<input.length();i++){
expand(i, i, input, result); // odd
expand(i, i+1, input, result); // even
}
return result;
}
public void expand(int low, int high, String input, Set<String> result) {
while(low >=0 && high < input.length() &&
input.charAt(low) == input.charAt(high)){
result.add(input.substring(low, high + 1));
low--;
high++;
}
}
}