Skip to content

refactor: refactor Alphabetical and AlphabeticalTest #6355

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 12 additions & 16 deletions src/main/java/com/thealgorithms/strings/Alphabetical.java
Original file line number Diff line number Diff line change
@@ -1,36 +1,32 @@
package com.thealgorithms.strings;

/**
* Utility class for checking if a string's characters are in alphabetical order.
* <p>
* Alphabetical order is a system whereby character strings are placed in order
* based on the position of the characters in the conventional ordering of an
* alphabet. Wikipedia: https://en.wikipedia.org/wiki/Alphabetical_order
* alphabet.
* <p>
* Reference: <a href="https://en.wikipedia.org/wiki/Alphabetical_order">Wikipedia: Alphabetical Order</a>
*/
final class Alphabetical {
public final class Alphabetical {
private Alphabetical() {
}

public static void main(String[] args) {
assert !isAlphabetical("123abc");
assert isAlphabetical("aBC");
assert isAlphabetical("abc");
assert !isAlphabetical("xyzabc");
assert isAlphabetical("abcxyz");
}

/**
* Check if a string is alphabetical order or not
* Checks whether the characters in the given string are in alphabetical order.
* Non-letter characters will cause the check to fail.
*
* @param s a string
* @return {@code true} if given string is alphabetical order, otherwise
* {@code false}
* @param s the input string
* @return {@code true} if all characters are in alphabetical order (case-insensitive), otherwise {@code false}
*/
public static boolean isAlphabetical(String s) {
s = s.toLowerCase();
for (int i = 0; i < s.length() - 1; ++i) {
if (!Character.isLetter(s.charAt(i)) || !(s.charAt(i) <= s.charAt(i + 1))) {
if (!Character.isLetter(s.charAt(i)) || s.charAt(i) > s.charAt(i + 1)) {
return false;
}
}
return true;
return !s.isEmpty() && Character.isLetter(s.charAt(s.length() - 1));
}
}
29 changes: 7 additions & 22 deletions src/test/java/com/thealgorithms/strings/AlphabeticalTest.java
Original file line number Diff line number Diff line change
@@ -1,30 +1,15 @@
package com.thealgorithms.strings;

import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.assertEquals;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;

public class AlphabeticalTest {

@Test
public void isAlphabetical() {
// expected to be true
String input1 = "abcdefghijklmno";
String input2 = "abcdxxxyzzzz";
String input3 = "fpw";

// expected to be false
String input4 = "123a";
String input5 = "abcABC";
String input6 = "abcdefghikjlmno";

assertTrue(Alphabetical.isAlphabetical(input1));
assertTrue(Alphabetical.isAlphabetical(input2));
assertTrue(Alphabetical.isAlphabetical(input3));

assertFalse(Alphabetical.isAlphabetical(input4));
assertFalse(Alphabetical.isAlphabetical(input5));
assertFalse(Alphabetical.isAlphabetical(input6));
@ParameterizedTest(name = "\"{0}\" → Expected: {1}")
@CsvSource({"'abcdefghijklmno', true", "'abcdxxxyzzzz', true", "'123a', false", "'abcABC', false", "'abcdefghikjlmno', false", "'aBC', true", "'abc', true", "'xyzabc', false", "'abcxyz', true", "'', false", "'1', false"})
void testIsAlphabetical(String input, boolean expected) {
assertEquals(expected, Alphabetical.isAlphabetical(input));
}
}
Loading