What is a palindrome??
Quote
Make a note that the solution has to be in O(n).
| |
|
Welcome to KnowledgeSutra - Dear Guest | |
Posted 29 October 2007 - 02:54 PM
Quote
Posted 30 October 2007 - 12:26 AM
for (0 to halfway through string) {
push letter onto stack
}
is_palindrome = true
for (halfway through string to end of string) {
if letter != (pop letter from stack)
is_palindrome = false
end for loop
}
}
(substr($str,0,length($str)/2) eq reverse(substr($str,-length($str)/2)))
Posted 30 October 2007 - 08:51 AM
pointybirds, on Oct 30 2007, 05:56 AM, said:
for (0 to halfway through string) {
push letter onto stack
}
is_palindrome = true
for (halfway through string to end of string) {
if letter != (pop letter from stack)
is_palindrome = false
end for loop
}
}
(substr($str,0,length($str)/2) eq reverse(substr($str,-length($str)/2)))
Posted 23 November 2007 - 12:34 PM
pointybirds, on Oct 30 2007, 01:26 AM, said:
(substr($str,0,length($str)/2) eq reverse(substr($str,-length($str)/2)))
$str eq reverse($str)That is actually the very definition of the plaindrome, and it further saves your fingers, beyond being easier to understand (which I know isn't what we look for in Perl
Edited by apicolet, 23 November 2007 - 12:35 PM.
Posted 31 August 2008 - 10:22 AM
// lets take testString="SAS"
boolean isPalindrome(String testString){
int length=testString.length();
for(int i=0;i<length/2;i++)
if(testString[i]!=testString[length-i+1])
return false;
//else it's palindrome
return true;
}
Posted 31 March 2009 - 03:40 AM
Your method will not work because your String is not an Array but this is as small as I could get it in Java. I apologize if it does not come out in the code box thingy but only if you care.
Public class palindrome{Boolean isPalindrome(String testString){ return(testString.Equals(reverse(testString)));}public static String reverse(String s){ String and; if (s.Length() <= 1) return s; else{ char lastC = s.CharAt(s.Length()-1); String stringLeft = s.Substring(0, s.Length() -1); return and = lastC + reverse(stringLeft); } }}
-reply by That Guy

0 members, 1 guests, 0 anonymous users