Tuesday, 5 September 2017

java - is string palindrome ?

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
public class _String {
    static boolean is_palindrome(String s){
        char[] chars = s.toCharArray();
        for (int i = 0; i < chars.length; i++) chars[i] = Character.toLowerCase(chars[i]);

        return is_palindrome(chars);
    }

    static boolean is_palindrome(char[] chars){
        if(chars.length < 2)
            return true;

        int start = 0;
        int end = chars.length - 1;

        while(start <= end){
            if(chars[start++] != chars[end--])
                return false;
        }
        return true;
    }
}

styled using hilite.me

No comments:

Post a Comment