Java Clean Code


  • 0
    C
    public class Solution {
        public boolean isPalindrome(String s) {
            return isPalindromeInternal(sanitizeAndLowerCase(s));
        }
        
        private boolean isPalindromeInternal(String string) {
            if (string.isEmpty()) {
                return true;
            } else {
                int mid = string.length() / 2;
                int i = string.length() % 2 == 0 ? mid - 1 : mid;
                int j = mid;
                
                char[] charArr = string.toCharArray();
                for (; i >= 0 && j < string.length(); i--, j++) {
                    if (charArr[i] != charArr[j]) {
                        return false;
                    }
                }
                return true;
            }
        }
        
        private String sanitizeAndLowerCase(String string) {
            StringBuilder sb = new StringBuilder();
            for (char c : string.toCharArray()) {
                if (Character.isLetterOrDigit(c)) {
                    sb.append(Character.toLowerCase(c));
                }
            }
            return sb.toString();
        }
    }

Log in to reply
 

Looks like your connection to LeetCode Discuss was lost, please wait while we try to reconnect.