I just announced the new Spring Boot 2 material, coming in REST With Spring:

>> CHECK OUT THE COURSE

1. Overview

In this tutorial,  we’ll see how to check if a String contains all the letters of the alphabet or not.

Here’s a quick example: “Farmer jack realized that big yellow quilts were expensive.” – which does actually contain all the letters of the alphabet.

We’ll discuss three approaches.

First, we’ll model the algorithm using an imperative approach. Then will use regular expressions. And finally, we’ll take advantage of a more declarative approach using Java 8.

Additionally, we’ll discuss the Big-O-complexity of the approaches taken.

2. Imperative Algorithm

Let’s implement an imperative algorithm. For this, first, we’ll create a boolean array visited. Then, we’ll walk through input string character by character and mark the character as visited.

Please note that Uppercase and Lowercase are considered the same. So index 0 represents both A and a, likewise, index 25 represents both Z and z.

Finally, we’ll check if all the characters in the visited array are set to true:

public class EnglishAlphabetLetters {

    public static boolean checkStringForAllTheLetters(String input) {
        int index = 0;
        boolean[] visited = new boolean[26];

        for (int id = 0; id < input.length(); id++) {
            if ('a' <= input.charAt(id) && input.charAt(id) <= 'z') {
                index = input.charAt(id) - 'a';
            } else if ('A' <= input.charAt(id) && input.charAt(id) <= 'Z') {
                index = input.charAt(id) - 'A';
            }
            visited[index] = true;
        }

        for (int id = 0; id < 26; id++) {
            if (!visited[id]) {
                return false;
            }
        }
        return true;
    }
}

Big-O-complexity of this program is O(n) where n is the length of the string.

Note that there are many ways to optimize the algorithm, such as removing letters from a set and breaking as soon as the Set is empty. For the purpose of the exercise though, this algorithm is good enough.

3. Using Regular Expression

Using Regular expression, we can easily get the same results with a few lines of code:

public static boolean checkStringForAllLetterUsingRegex(String input) {
    return input.toLowerCase()
      .replaceAll("[^a-z]", "")
      .replaceAll("(.)(?=.*\\1)", "")
      .length() == 26;
}

Here, we are first eliminating all the characters except alphabet letters from the input. Then we are removing duplicate characters. Finally, we are counting letters and making sure we have all of them, 26.

Although less performant, Big-O-Complexity of this approach also tends to O(n).

4. Java 8 Stream

Using Java 8 features, we can easily achieve the same result in a more compact and declarative way using Stream’s filter and distinct methods:

public static boolean checkStringForAllLetterUsingStream(String input) {
    long c = input.toLowerCase().chars()
      .filter(ch -> ch >= 'a' && ch <= 'z')
      .distinct()
      .count();
    return c == 26;
}

Big-O-complexity of this approach will also be O(n).

4. Testing

Let’s test a happy path for our algorithm:

@Test
public void givenString_whenContainsAllCharacter_thenTrue() {
    String sentence = "Farmer jack realized that big yellow quilts were expensive";
    assertTrue(EnglishAlphabetLetters.checkStringForAllTheLetters(sentence));
}

Here, sentence contains all the letters of the alphabet, hence, we’re expecting true as a result.

5. Conclusion

In this tutorial, we’ve covered how to check if a String contains all the letters of the alphabet.

We saw a couple of ways to implement this first using traditional imperative programming, regular expressions, and Java 8 streams.

The complete source code is available over on GitHub.

I just announced the new Spring Boot 2 material, coming in REST With Spring:

>> CHECK OUT THE LESSONS