mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-08 02:04:31 +08:00
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:
@ -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)
|
||||||
|
Reference in New Issue
Block a user