Update Palindrome.java

Test cases where String x is null or has a length of 0 or 1 for FirstWay method.
This commit is contained in:
jasonptong
2018-12-22 17:27:31 -08:00
committed by GitHub
parent 084548d96e
commit a5bf69fcbd

View File

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