JavaAlgorithms/Others/Palindrome.java

24 lines
665 B
Java
Raw Normal View History

2017-07-16 23:14:29 +08:00
class Palindrome {
2018-02-03 14:14:46 +08:00
2018-12-24 10:01:45 +08:00
private String reverseString(String x) { // *helper method
StringBuilder output = new StringBuilder(x);
return output.reverse().toString();
}
2018-02-03 14:14:46 +08:00
2018-12-24 10:01:45 +08:00
public boolean FirstWay(String x) { // *palindrome method, returns true if palindrome
if (x == null || x.length() <= 1)
return true;
return x.equalsIgnoreCase(reverseString(x));
}
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));
}
}