Problem: Check if word is a Palindrome

eg. lol, a, god-dog, mattam

1
2
3
4
5
6
7
8
9
10
11
public static boolean palindrom(String s, int i, int j) {
    if( i > j || i == j) {
        return true;
    }
    
    if(s.charAt(i) == s.charAt(j)) {
        return palindrom(s, i+1, j-1);
    } else {
        return false;
    }
}

For this if think it is easiest to do it recursively because you can start from the first and last character and move inwards. If the word is odd then will terminate or the two iterators will cross each other and terminate when . The recursion will stop as soon as it finds a character is not equal.

Code