JP Morgan Interview Question
Country: United States
Interview Type: Written Test
package arraystring;
import java.util.LinkedHashSet;
import java.util.Set;
import java.util.SortedSet;
import java.util.TreeSet;
public class PanagramString {
public static void main(String[] args) {
// TODO Auto-generated method stub
String str = "Jived fox nymph grabs quick waltz";
char [] a = str.toLowerCase().toCharArray();
Set<Character> s = new LinkedHashSet<Character>();
SortedSet<Character> t = new TreeSet<Character>();
//Add all the alphabets from a to z in a sorted Set
for(int i=97; i<123; i++){
t.add(new Character((char)i));
}
for(int i=0; i<a.length; i++){
if(a[i] == ' ')
continue;
s.add(new Character(a[i]));//add the alphabet from the sorted set
t.remove(new Character(a[i]));//remove the alphabet from the sorted set
if(s.size() == 26){//break from loop when all 26 alphabets are added
break;
}
}
if(s.size() == 26){
System.out.println("Is Panagram !!!");
}else{
System.out.println("NOT Panagram !!!" + t);
}
}
}
package arraystring;
import java.util.LinkedHashSet;
import java.util.Set;
import java.util.SortedSet;
import java.util.TreeSet;
public class PanagramString {
public static void main(String[] args) {
// TODO Auto-generated method stub
String str = "Jived fox nymph grabs quick waltz";
char [] a = str.toLowerCase().toCharArray();
Set<Character> s = new LinkedHashSet<Character>();
SortedSet<Character> t = new TreeSet<Character>();
//Add all the alphabets from a to z in a sorted Set
for(int i=97; i<123; i++){
t.add(new Character((char)i));
}
for(int i=0; i<a.length; i++){
if(a[i] == ' ')
continue;
s.add(new Character(a[i]));//add the alphabet from the sorted set
t.remove(new Character(a[i]));//remove the alphabet from the sorted set
if(s.size() == 26){//break from loop when all 26 alphabets are added
break;
}
}
if(s.size() == 26){
System.out.println("Is Panagram !!!");
}else{
System.out.println("NOT Panagram !!!" + t);
}
}
}
A simple solution is to just keep a track of the counts of letters (a-z). If any of the counts are 0, then it is not a panagram, else it is.
Simple to understand Python solution below:
from string import ascii_lowercase, punctuation
from collections import defaultdict
def isPanagram(sentence):
sentence = sentence.lower()
letterCount = defaultdict(int)
for letter in ascii_lowercase:
letterCount[letter] = 0
for letter in sentence:
if letter in punctuation:
continue # Skip special characters / punctuation marks
letterCount[letter] += 1
missingLetters = [letter for letter, count in letterCount.items() if count == 0]
if len(missingLetters) == 0:
print('The sentence is a panagram!')
else:
print('The sentence is not a panagram and is missing the letters: ', missingLetters)
isPanagram('Fox nymphs grab quick-jived waltz.')
isPanagram('The quick fox.')
isPanagram('The five boxing wizards jump quickly.')
isPanagram('The five boxing wizards jump.')
#include <iostream>
#include <string>
using namespace std;
int main() {
string s;
cin >> s;
string missing="";
int add = 'a';
int alphabet[26] = {0};
for(char c: s)
alphabet[c-'a']++;
for(int i=0; i<26; i++)
if(!alphabet[i])
missing+=((char)(i + add));
cout << (missing.size() ? missing : "Panagram");
return 0;
}
- Karthik January 17, 2018