Search code examples
javaalgorithmlogic

Check if string has all the letters of the alphabet


What would be the best logic to check all the letters in a given string.

If all the 26 letters are available in the provided string, I want to check that and perform so ops. eg. Pack my box with five dozen liquor jugs.

  1. Would using a Hash be useful?
  2. Or using a bit map? or any other way?

BTW my code would be in Java.


Solution

  • Not yet fully optimized:

    public static void main(String... a) {
        String s = "Pack my box with five dozen liquor jugs.";
        int i=0;
        for(char c : s.toCharArray()) {
            int x = Character.toUpperCase(c);
            if (x >= 'A' && x <= 'Z') {
                i |= 1 << (x - 'A');
            }
        }
        if (i == (i | ((1 << (1 + 'Z' - 'A')) - 1))) {
            System.out.println("ok");
        }
    }