From a5bf69fcbd6eff2858581d07b013d79ad68fa0ce Mon Sep 17 00:00:00 2001 From: jasonptong <43040084+jasonptong@users.noreply.github.com> Date: Sat, 22 Dec 2018 17:27:31 -0800 Subject: [PATCH 1/2] Update Palindrome.java Test cases where String x is null or has a length of 0 or 1 for FirstWay method. --- Others/Palindrome.java | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/Others/Palindrome.java b/Others/Palindrome.java index 0bbddb7544c8..52062c6c4666 100644 --- a/Others/Palindrome.java +++ b/Others/Palindrome.java @@ -1,6 +1,6 @@ class Palindrome { - private String reverseString(String x){ //*helper method + 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 @@ -10,7 +10,9 @@ private String reverseString(String x){ //*helper method 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) From 2d383bf48e7f058ec6d20b668e7a89edb3d51d43 Mon Sep 17 00:00:00 2001 From: Libin Yang Date: Mon, 24 Dec 2018 10:01:45 +0800 Subject: [PATCH 2/2] Update Palindrome.java --- Others/Palindrome.java | 45 +++++++++++++++++++----------------------- 1 file changed, 20 insertions(+), 25 deletions(-) diff --git a/Others/Palindrome.java b/Others/Palindrome.java index 52062c6c4666..d482dfd01bce 100644 --- a/Others/Palindrome.java +++ b/Others/Palindrome.java @@ -1,28 +1,23 @@ 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; - } - - - 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; + private String reverseString(String x) { // *helper method + StringBuilder output = new StringBuilder(x); + return output.reverse().toString(); + } - return SecondWay(x.substring(1 , x.length() - 1)); - } - } + 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)); + } +}