File tree 1 file changed +20
-23
lines changed
1 file changed +20
-23
lines changed Original file line number Diff line number Diff line change 1
1
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 ;
20
2
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
+ }
23
7
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
+ }
You can’t perform that action at this time.
0 commit comments