Expedia Interview Question
Java DevelopersCountry: United States
Interview Type: Phone Interview
Lets assume the length of the string is given.
Compare the first and last characters, if equal, compare the second and second last characters, and so on.
If at any point comparison fails, return false.
bool is_palindrome(char* string,int length)
{
int start = 0;
int end = length;
while(start<end)
{
if (string[start]!==string[end])
return false;
start++;end--;
}
return true;
}
This is in Java
public class StringPalindromeOrNot {
public static void main(String [] args) {
String palindrome = "MALAYALAM";
String notPalindrome = "MALADYLAM";
boolean palindromeTrue = isPalindrome(palindrome);
boolean palindromeFalse = isPalindrome(notPalindrome);
System.out.println(palindrome + " = " + palindromeTrue);
System.out.println(notPalindrome + " = " + palindromeFalse);
}
private static boolean isPalindrome(String word) {
if(word == null || word.length() == 0) {
throw new IllegalArgumentException("InvalidWord");
}
int front = 0;
int back = word.length() - 1;
while (front < back) {
if(word.charAt(front) != word.charAt(back)) {
return false;
}
else {
front++;
back--;
}
}
return true;
}
}
Below code works. Please comment for any improvement.
public class IsStringPalindrome{
public static void main(String args[]){
try{
if(args[0]==null){
}
}catch(ArrayIndexOutOfBoundsException e){
System.out.println("NOT");
System.exit(0);
}
if(isPalindrome(args[0]))
System.out.println("YES");
else
System.out.println("NOT");
}
public static boolean isPalindrome(String str){
System.out.println("str:" + str);
int i=0, j=(str.length()-1);
while(i<j){
if(str.charAt(i)!= str.charAt(j)){
System.out.println(str.charAt(i) + " " + str.charAt(j));
return false;
}
i++; j--;
}
return true;
}
}
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
String givenString = scan.nextLine();
String reverseString = reverseString(givenString);
if (reverseString.equalsIgnoreCase(givenString)) {
System.out.println("Palindrome");
} else {
System.out.println("Not Palindrome");
}
}
private static String reverseString(String str) {
char[] reversed = str.toCharArray();
for (int i = str.length() - 1, j = 0; i >= 0 && j < str.length()-1; i--, j++) {
reversed[j] = reversed[i];
}
return String.valueOf(reversed);
}
public class Solutions1
{
public static void main(String[] args)
{
String str = "Bob";
System.out.println(isPalin(str));
}
private static boolean isPalin(String str)
{
int i = 0;
int j = str.length() - 1;
while(i < j)
{
if(str.toLowerCase().charAt(i++) != str.toLowerCase().charAt(j--))
{
return false;
}
}
return true;
}
}
public class Solutions1
{
public static void main(String[] args)
{
String str = "Bob";
System.out.println(isPalin(str));
}
private static boolean isPalin(String str)
{
int i = 0;
int j = str.length() - 1;
while(i < j)
{
if(str.toLowerCase().charAt(i++) != str.toLowerCase().charAt(j--))
{
return false;
}
}
return true;
}
}
For checking palindrome of a string we need to traverse the mid of the string and keep checking that the first and the last characters of the string should not be unequal if they are then, return false else at the end of the string we will return true.
Implementation:
bool findpalindrome(string str){
int i = 0;
int j = str.length() - 1;
while(i != j || i < j)
if(str[i] != str[j])
return false;
i++;
j--;
}
return true;
}
public class TestPalindrome
- danqianchen April 19, 2012{
public static void main(String[] args)
{
String newString = "Murder for a jar of red rum.";
String otherString = newString.replaceAll( "\\W", "" );
System.out.println(otherString);
String otherString2 = reverse2(otherString);
System.out.println(otherString2);
System.out.println(otherString);
if(otherString.equalsIgnoreCase(otherString2))
System.out.println("Palindrome");
else
System.out.println("not Palindrome");
}
public static String reverse2(String str)
{
char[] a = str.toCharArray();
int middle = str.length()/2;
int length = str.length() - 1;
for(int i = 0; i <= middle; i++)
{
char temp = a[i];
//System.out.println("temp is:" + temp);
//System.out.println("a[i] is:" + a[i]);
//System.out.println("a[length-i] is:" + a[length-i]);
a[i] = a[length-i];
a[length-i] = temp;
}
return new String(a);
}
}