JavaAlgorithms/Others/Palindrome.java

27 lines
607 B
Java
Raw Normal View History

2017-07-16 23:14:29 +08:00
class Palindrome {
2018-02-03 14:14:46 +08:00
private String reverseString(String x){ //*helper method
2017-07-16 23:14:29 +08:00
String output = "";
for(int i=x.length()-1; i>=0; i--){
output += x.charAt(i); //addition of chars create String
}
return output;
}
2018-02-03 14:14:46 +08:00
public Boolean FirstWay(String x){ //*palindrome method, returns true if palindrome
2017-07-16 23:14:29 +08:00
return (x.equalsIgnoreCase(reverseString(x)));
}
2018-02-03 14:14:46 +08:00
public boolean SecondWay(String x)
{
if (x.length() == 0 || x.length() == 1)
return true;
if (x.charAt(0) != x.charAt(x.length() - 1))
return false;
return SecondWay(x.substring(1 , x.length() - 1));
}
2017-07-16 23:14:29 +08:00
}