Reputation: 1461
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.
BTW my code would be in Java.
Upvotes: 4
Views: 21951
Reputation: 1505
try this method, is very simple and works fast
public static String checkSentence(String mySentence)
{
String result = "";
char[] alphabet = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'};
String missingLetters = "";
int countMissLetters = 0;
for (int i = 0; i < alphabet.length; i++)
{
if (!mySentence.toLowerCase().contains(String.valueOf(alphabet[i]).toLowerCase()))
{
missingLetters = missingLetters + String.valueOf(alphabet[i]) + " ";
countMissLetters++;
}
}
result = "Letters who are missing: " + missingLetters + "Counted: " + countMissLetters;
return result;
}
with this exemple
System.out.println(checkSentence("Your own sentence to test."));
System.out.println(checkSentence("abcdefghijklmnoprstuvwxyz"));
will return this results
Letters who are missing: a b d f g h i j k l m p q v x z Counted: 16
Letters who are missing: q Counted: 1
Upvotes: 0
Reputation: 2029
Just another solution, using Array instead of Set
public static boolean isPanagram(String input) {
if(input == null || input.trim().equals(""))
return false;
boolean[] chars = new boolean[26];
int count = 0;
for(char c : input.toLowerCase().toCharArray()) {
if(Character.isAlphabetic(c)) {
int val = (int) c - 'a';
if(val >= 0 || val <= 26) {
if(!chars[val]) {
count++;
chars[val] = true;
}
}
// checking if all 26 alphabets are set, for every new alphabet found.
// so can return true immediately instead of processing entire string
if(count==26)
return true;
}
}
return false;
}
Upvotes: 0
Reputation: 17
Not a optimal solution but it works.
String a= new String("the quick brown fox jumps over the lazy dog");
int n=a.length();
int[] b= new int[123];
char x;
boolean flag=true;
for(int i=0;i<n;i++)
{
if(a.charAt(i)==' ')
continue;
x=Character.toLowerCase(a.charAt(i));
b[(int)x]++;
}
for(int i=97;i<123;i++)
{
if(b[i]==0)
{
flag=false;
break;
}
}
if(!flag)
System.out.print("No");
else
System.out.print("yes");
Upvotes: 0
Reputation: 4712
Explanation
First I will convert all string chars to uppercase.
in ASCII Table you can find uppercase chars ranges, i.e. between 65 to 90
A == 65 and Z == 90
So, for
loop should start from 65 and end on 90.
Check if the string contains current ASCII char.
I am converting integer to char and put "" to be string
if !text.contains((char)i + "") == true
This implies that if the string does not have the current char to i
, I will return false
.
You can make it even better by using char i
in loop
static boolean isPangram(String text) {
//change all chars to upper case
text = text.toUpperCase();
//Loop from A to Z
for(int i = 65 ; i < 91 ; i++){
//if text not contains return false
if(!text.contains((char)i + "")){
return false;
}
}
return true;
}
Upvotes: 0
Reputation: 11
Try this one out it's easy and simple to understand
import java.util.*;
public class Pnagram{
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
String Str=sc.nextLine();
Set<Character> set = new HashSet<Character>();
for(char c:Str.toUpperCase().toCharArray()){
if(Character.isLetter(c))
set.add(c);
}
if(set.size()==26)
System.out.println("pnagram");
else
System.out.println("not pnagram");
}
}
Upvotes: 1
Reputation: 1
This is not an optimal solution, yet easy to understand :) !
import java.io.*;
import java.util.*;
public class Solution {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String s = in.nextLine().toUpperCase();
char[] sarray = s.toCharArray();
char[] alphabets = {'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'};
Set<Character> set = new HashSet<Character>();
for(int i=0;i<sarray.length;i++){
for(int j=0;j<alphabets.length;j++){
if(sarray[i] == alphabets[j]){
set.add(sarray[i]);
break;
}
continue;
}
}
if(set.size() == 26){
System.out.println("pangram");
}else{
System.out.println("not pangram");
}
}
}
Upvotes: 0
Reputation: 13
public class Pangram {
public static boolean isPangram(String test){
for (char a = 'A'; a <= 'Z'; a++)
if ((test.indexOf(a) < 0) && (test.indexOf((char)(a + 32)) < 0))
return false;
return true;
}
public static void main(String[] args){
System.out.println(isPangram("the quick brown fox jumps over the lazy dog"));//true
System.out.println(isPangram("the quick brown fox jumped over the lazy dog"));//false, no s
System.out.println(isPangram("ABCDEFGHIJKLMNOPQRSTUVWXYZ"));//true
System.out.println(isPangram("ABCDEFGHIJKLMNOPQSTUVWXYZ"));//false, no r
System.out.println(isPangram("ABCDEFGHIJKL.NOPQRSTUVWXYZ"));//false, no m
System.out.println(isPangram("ABC.D.E.FGHI*J/KL-M+NO*PQ R\nSTUVWXYZ"));//true
System.out.println(isPangram(""));//false
System.out.println(isPangram("Pack my box with five dozen liquor jugs."));//true
}
}
Upvotes: 1
Reputation: 1
public static void main(String[] args) {
String A ="asdfsdafasdf";
String B ="abcdefghijklmnopqrstuvwxyz";
String C ="ASDFGFHWER";
String result = "NO";
String letters[] = {A,B,C};
int length = letters.length;
for(int j=0; j< length; j++){
String letter = letters[j].toLowerCase();
int letterLength = letter.length();
for(char i='a'; i<'z'+1; i++){
if(letter.contains (""+i)){
result ="YES";
} else{
result = "NO";
}
}
System.out.println(result);
}
}
Upvotes: 0
Reputation: 37467
I'd go for a sieve algorithm on the 26 letters. Just my $.02.
Edit: An array of 26 values that represent the 26 letters of the alphabet. Then scan the string, checking each letter as you encounter it. At the end, check if the 26 letters have been checked.
Upvotes: 1
Reputation: 83938
You could iterate over your string for each letter of the alphabet you want to check. When you find the letter you are looking for, continue with the next. If you don’t, abort. Here is some pseudo-code:
found = true;
for (letter in letters) {
if (!string.contains(letter)) {
found = false;
break;
}
}
This way you do not need to store the information for each letter but you have to search the string again and again for each letter. What you end up using will depend on your requirements: should it be fast? Should it use little memory? Your decision. :)
Upvotes: 0
Reputation: 33545
Using a BitMap, I'm assuming you meant case insenstive.
Update: Solution by Thomas is more efficient, than the following. :) Use that one.
//
String test = "abcdefeghjiklmnopqrstuvwxyz";
BitSet alpha = new BitSet(26);
for(char ch : test.toUpperCase().toCharArray())
if(Character.isLetter(ch))
alpha.set(ch - 65);
System.out.println(alpha.cardinality() == 26);
Upvotes: 6
Reputation: 50127
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");
}
}
Upvotes: 5
Reputation: 3873
An array of 26 booleans is enough, each entry representing on of the alphabet letters. You can set the entry to true when the letter is found.
Upvotes: 1
Reputation: 26586
Keep an boolean
array of size 26. Each position of the array says whether a particular character is present or not (a is at 0, b is at 1, etc.). Initially all are set to false. Now do a single scan through the string character by character, setting the value for that character to true. At the end, check if all 26 indexes contain true.
Upvotes: 1
Reputation:
I'd go for a bitmap. If you increment a counter each time you set an entry in the bitmap to 1, you can early return as soon as you've seen all the letters. I hope this is not for enforcing password requirements.
Upvotes: 3