File tree Expand file tree Collapse file tree 1 file changed +8
-3
lines changed
src/main/java/com/thealgorithms/strings Expand file tree Collapse file tree 1 file changed +8
-3
lines changed Original file line number Diff line number Diff line change @@ -21,12 +21,17 @@ private Alphabetical() {
21
21
* @return {@code true} if all characters are in alphabetical order (case-insensitive), otherwise {@code false}
22
22
*/
23
23
public static boolean isAlphabetical (String s ) {
24
+ if (s == null || s .isEmpty ()) {
25
+ return false ;
26
+ }
24
27
s = s .toLowerCase ();
25
- for (int i = 0 ; i < s .length () - 1 ; ++i ) {
26
- if (!Character .isLetter (s .charAt (i )) || s .charAt (i ) > s .charAt (i + 1 )) {
28
+ for (int i = 0 ; i < s .length () - 1 ; i ++) {
29
+ char current = s .charAt (i );
30
+ char next = s .charAt (i + 1 );
31
+ if (!Character .isLetter (current ) || current > next ) {
27
32
return false ;
28
33
}
29
34
}
30
- return ! s . isEmpty () && Character .isLetter (s .charAt (s .length () - 1 ));
35
+ return Character .isLetter (s .charAt (s .length () - 1 ));
31
36
}
32
37
}
You can’t perform that action at this time.
0 commit comments