class Pal { public static void main(String args[]) { //System.out.println(findPalIter(args[0])); System.out.println(findPalRec(args[0])); } public static boolean findPalIter(String str) { char[] orig = str.toCharArray(); char[] backward = new char[orig.length]; int j=0; for( int i=orig.length-1; i >=0; i-- ) { backward[j++] = orig[i]; } if( str.equals( new String(backward))) return true; return false; } private static boolean findPalRec(String str) { char[] chars = str.toCharArray(); if( chars.length == 0 || chars.length == 1 ) return true; if( chars[0] == chars[chars.length-1]) return findPalRec( str.substring( 1, chars.length-1 ) ); else return false; } }https://github.com/asharif/StringPalindrome
Wednesday, July 18, 2012
String Palindrome Algorithm
The last of these old school algorithms. Funny how trivial they should be after all these years but they still trip me up. Hence these posts and practices!
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment