Thursday, June 23, 2016

Pangrams: Check existence of character in string

If it tells you that the problem is case insensitive alphabets, to check a letters existence, you can use an integer to set corresponding bit value to represent existence of a letter since there are totally 26 letters and integer has 32 bits.

Problem:


Given a sentence , tell Roy if it is a pangram or not.

Input Format
Input consists of a string .
Constraints
Length of can be at most and it may contain spaces, lower case and upper case letters. Lower-case and upper-case instances of a letter are considered the same.
Output Format
Output a line containing pangram if is a pangram, otherwise output not pangram.
Sample Input
Input #1
We promptly judged antique ivory buckles for the next prize    
Input #2
We promptly judged antique ivory buckles for the prize    
Sample Output
Output #1
pangram
Output #2
not pangram
Explanation
In the first test case, the answer is pangram because the sentence contains all the letters of the English alphabet.

Solution:



 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import java.io.*;
import java.util.*;
import java.text.*;
import java.math.*;
import java.util.regex.*;

public class Solution {

    public static void main(String[] args) {
        /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
        int bitCheck=0;
        Scanner in = new Scanner(System.in);
        StringBuilder sb = new StringBuilder();
        while(in.hasNext()){
            sb.append(in.next());
        }
        
        for(int i=0;i<sb.length();i++){
            if(Character.isLetter(sb.charAt(i) )){
                int n = Character.toLowerCase(sb.charAt(i))-'a';
                bitCheck|=1<<n;}
        
        }
        if(bitCheck==0b00000011111111111111111111111111)
            System.out.println("pangram");
        else
            System.out.println("not pangram");
    }
}

No comments: