Skip to content

Commit 2d383bf

Browse files
authored
Update Palindrome.java
1 parent a5bf69f commit 2d383bf

File tree

1 file changed

+20
-25
lines changed

1 file changed

+20
-25
lines changed

Others/Palindrome.java

Lines changed: 20 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +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-
if(x == null || x.length() <= 1)
14-
return true;
15-
return (x.equalsIgnoreCase(reverseString(x)));
16-
}
17-
18-
public boolean SecondWay(String x)
19-
{
20-
if (x.length() == 0 || x.length() == 1)
21-
return true;
222

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

26-
return SecondWay(x.substring(1 , x.length() - 1));
27-
}
28-
}
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)