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
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");
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. :)
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");
}
}
}
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
}
}
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.
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);
}
}