How will you check if a given sentence is a pangram

Carvia Tech | May 18, 2019 at 07:03 PM | 23 views | Java Coding Challenges


Pangram

A pangram is a sentence containing every letter in the English Alphabet (a-z).

Pangram example: The quick brown fox jumps over the lazy dog

Pack my box with five dozen liquor jugs.

— Pangram

Approach:

  1. Create an boolean array that can hold 26 boolean corresponding to each alphabet in English.

  2. Traverse each character of the sentence and mark corresponding index (a=0, b=1,.. z=26) in the boolean array. Take care of upper and lower case.

  3. Traverse through the entire boolean array and see if all characters are covered. If all positions are set to true then input sentence is a pangram.

Pangram Source
public class Pangram {

    public boolean checkPangram(String str) {
        boolean[] mark = new boolean[26];

        int index = 0;
        for (int i = 0; i < str.length(); i++) {
            // If uppercase character, subtract 'A' to find index.
            if ('A' <= str.charAt(i) && str.charAt(i) <= 'Z')
                index = str.charAt(i) - 'A';
            else if ('a' <= str.charAt(i) && str.charAt(i) <= 'z')
                index = str.charAt(i) - 'a';
            mark[index] = true;
        }
        for (int i = 0; i <= 25; i++)
            if (!mark[i])
                return (false);
        return (true);
    }
}

A slightly optimized version of the same program can use an integer (has 32 bits) to hold the state of 26 alphabets by use of Bitwise OR and left shift operator.

Optimized Version
private void pangramEfficient() {
    String s = "The quick brown fox jumps over the lazy dog";
    int i = 0;
    for (char c : s.toCharArray()) {
        int x = Character.toUpperCase(c);
        if (x >= 'A' && x <= 'Z') {
            i |= 1 << (x - 'A');    (1)
        }
    }
    if (i == (1 << (1 + 'Z' - 'A')) - 1) {  (2)
        System.out.println("input is a pangram");
    } else {
        System.out.println("input is not a pangram");
    }
}
1 We are setting nth bit in the integer, where n = 0 for a and n=26 for z. Bitwise ORing can be used to set nth bit of a integer.
2 We are checking if first 26 bits (corresponding to each alphabet) are set or not

No program is complete without a accompanying testcase. Lets write a simple Junit test for Pangram Checker.

Junit Test
import org.junit.Test;

import static org.junit.Assert.assertTrue;

public class PangramTest {
    private Pangram pangramChecker = new Pangram();

    @Test
    public void checkPangram() {
        String str = "The quick brown fox jumps over the lazy dog";
        assertTrue(pangramChecker.checkPangram(str));
    }
}

Thats all.


Java Coding Challenges:
  1. How to check if the given number is palindrome in Java
  2. Check if the given number is Armstrong Number in Java
  3. Calculate factorial of a number in Java using recursion
  4. Write a program to reverse a string using recursion in Java
  5. Create anagram buckets from a given input array of words
  6. Java program to check if two strings are anagrams
  7. Given a collection of 1 million integers, All ranging between 1 to 9, how would you sort them in Big O(n) time
See all articles in Java Coding Challenges
Top articles in this category:
  1. Cracking core java interviews - question bank
  2. BlackRock Top Java Interview Questions: Investment Banking Domain
  3. Morgan Stanley Java Interview Questions
  4. Top 50 Multi-threading Java Interview Questions for Investment Banking Domain
  5. Citibank Java developer interview questions
  6. RBS Java Interview Questions
  7. UBS Top 10 Java Interview Questions


Find more on this topic:
Java Interviews image
Java Interviews

Interview - Product Companies, eCommerce Companies, Investment Banking, Healthcare Industry, Service Companies and Startups.

Last updated 1 month ago


Recommended books for interview preparation:
You may also be interested in..
Generic placeholder image
ebook PDF - Cracking Spring Microservices Interviews for Java Developers
You may also be interested in..
Generic placeholder image
ebook PDF - Cracking Java Interviews v3.5 by Munish Chandel

This website uses cookies to ensure you get the best experience on our website. more info