Skip to content

Commit b9aacd4

Browse files
authored
Merge pull request TheAlgorithms#665 from jasonptong/patch-1
Update Palindrome.java
2 parents 084548d + 2d383bf commit b9aacd4

File tree

1 file changed

+20
-23
lines changed

1 file changed

+20
-23
lines changed

Others/Palindrome.java

Lines changed: 20 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,23 @@
11
class Palindrome {
2-
3-
private String reverseString(String x){ //*helper method
4-
String output = "";
5-
for(int i=x.length()-1; i>=0; i--){
6-
output += x.charAt(i); //addition of chars create String
7-
}
8-
return output;
9-
}
10-
11-
12-
public Boolean FirstWay(String x){ //*palindrome method, returns true if palindrome
13-
return (x.equalsIgnoreCase(reverseString(x)));
14-
}
15-
16-
public boolean SecondWay(String x)
17-
{
18-
if (x.length() == 0 || x.length() == 1)
19-
return true;
202

21-
if (x.charAt(0) != x.charAt(x.length() - 1))
22-
return false;
3+
private String reverseString(String x) { // *helper method
4+
StringBuilder output = new StringBuilder(x);
5+
return output.reverse().toString();
6+
}
237

24-
return SecondWay(x.substring(1 , x.length() - 1));
25-
}
26-
}
8+
public boolean FirstWay(String x) { // *palindrome method, returns true if palindrome
9+
if (x == null || x.length() <= 1)
10+
return true;
11+
return x.equalsIgnoreCase(reverseString(x));
12+
}
13+
14+
public boolean SecondWay(String x) {
15+
if (x.length() == 0 || x.length() == 1)
16+
return true;
17+
18+
if (x.charAt(0) != x.charAt(x.length() - 1))
19+
return false;
20+
21+
return SecondWay(x.substring(1, x.length() - 1));
22+
}
23+
}

0 commit comments

Comments
 (0)