vivemoire

vivemoire vivemoire

Niner since 2005

Comments

  • Gary Daniels and Evan Goldring - Mock whiteboard problem

    A bit too late (like a year or so, but just stumbled on this video preparing for my own interview...). Anyway here is my 2 cents in Java (handles case, punctuation, spaces by ignoring them). Tried to compress the algorithm as much as possible, but still keep the readability:

        public static boolean findPolyndrom(String polynd) {

            if (polynd == null || "".equals(polynd = polynd.toLowerCase().trim())) {
                return false;
            }
           
            int index = 0;
            int length = polynd.length();

            char forward = 0;
            char reverse = 0;
           
            boolean isValidString = false;
           
            while (index < length) {
               
                while(!Character.isLetterOrDigit(forward = polynd.charAt(index++))) {
                    if (index >= length) {
                        return isValidString;
                    }
                }
               
                while(!Character.isLetterOrDigit(reverse = polynd.charAt(--length))) {
                    if (length <= index) {
                        return isValidString;
                    }
                }

                isValidString = true;
               
                if (forward != reverse) {
                    return false;
                }

                if (length <= index) {
                    break;
                }
            }
            return true;
        }