mirror of
https://github.com/TheAlgorithms/Java.git
synced 2025-07-08 18:32:56 +08:00
Update Palindrome.java
This commit is contained in:
@ -1,28 +1,23 @@
|
|||||||
class Palindrome {
|
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))
|
private String reverseString(String x) { // *helper method
|
||||||
return false;
|
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));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Reference in New Issue
Block a user