Update Palindrome.java

This commit is contained in:
Libin Yang
2018-12-24 10:01:45 +08:00
committed by GitHub
parent a5bf69fcbd
commit 2d383bf48e

View File

@ -1,22 +1,17 @@
class Palindrome {
private String reverseString(String x) { // *helper method
String output = "";
for(int i=x.length()-1; i>=0; i--){
output += x.charAt(i); //addition of chars create String
}
return output;
StringBuilder output = new StringBuilder(x);
return output.reverse().toString();
}
public Boolean FirstWay(String x){ //*palindrome method, returns true if palindrome
public boolean FirstWay(String x) { // *palindrome method, returns true if palindrome
if (x == null || x.length() <= 1)
return true;
return (x.equalsIgnoreCase(reverseString(x)));
return x.equalsIgnoreCase(reverseString(x));
}
public boolean SecondWay(String x)
{
public boolean SecondWay(String x) {
if (x.length() == 0 || x.length() == 1)
return true;