MCPcopy Create free account
hub / github.com/TheAlgorithms/Java / Alphabetical

Class Alphabetical

src/main/java/com/thealgorithms/strings/Alphabetical.java:17–58  ·  view source on GitHub ↗

Utility class for checking whether a string's characters are in non-decreasing lexicographical order based on Unicode code points (case-insensitive). This does NOT implement language-aware alphabetical ordering (collation rules). It simply compares lowercase Unicode character values. Non-let

Source from the content-addressed store, hash-verified

15 * <a href="https://en.wikipedia.org/wiki/Alphabetical_order">Wikipedia: Alphabetical order</a>
16 */
17public final class Alphabetical {
18
19 private Alphabetical() {
20 }
21
22 /**
23 * Checks whether the characters in the given string are in non-decreasing
24 * lexicographical order (case-insensitive).
25 * <p>
26 * Rules:
27 * <ul>
28 * <li>String must not be null or blank</li>
29 * <li>All characters must be letters</li>
30 * <li>Comparison is based on lowercase Unicode values</li>
31 * <li>Order must be non-decreasing (equal or increasing allowed)</li>
32 * </ul>
33 *
34 * @param s input string
35 * @return {@code true} if characters are in non-decreasing order, otherwise {@code false}
36 */
37 public static boolean isAlphabetical(String s) {
38 if (s == null || s.isBlank()) {
39 return false;
40 }
41
42 String normalized = s.toLowerCase(Locale.ROOT);
43
44 if (!Character.isLetter(normalized.charAt(0))) {
45 return false;
46 }
47
48 for (int i = 1; i < normalized.length(); i++) {
49 char prev = normalized.charAt(i - 1);
50 char curr = normalized.charAt(i);
51
52 if (!Character.isLetter(curr) || prev > curr) {
53 return false;
54 }
55 }
56 return true;
57 }
58}

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected