Merge pull request #665 from jasonptong/patch-1

Update Palindrome.java
This commit is contained in:
Libin Yang
2018-12-24 10:02:21 +08:00
committed by GitHub

View File

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