How to check if a string is pangram in Java

1 Answer

0 votes
// A string is a pangram if it contains all the characters of the alphabet ignoring case
 
import java.util.stream.Collectors;
import java.util.stream.Stream;
 
public class MyClass {
    public static String removeRepeatedCharactersFromString(String str) {
        StringBuilder sb = new StringBuilder();
         
        str.chars().distinct().forEach(ch -> sb.append((char) ch));
         
        return sb.toString();
    }
     
    public static boolean isStringPangram(String str) {
        String abc = "abcdefghijklmnopqrstuvwxyz";
         
        str = str.toLowerCase();
         
        str = str.replaceAll("\\s+",""); // remove spaces from string
         
        str = removeRepeatedCharactersFromString(str);
         
        str = Stream.of(str.split("")) // sort string
                    .sorted()
                    .collect(Collectors.joining());
 
        return abc.equals(str);
    }
     
    public static void main(String args[]) {
         
        String str = "The quick brown fox jumps over the lazy dog";
         
        System.out.println(isStringPangram(str));
    }
}
 
 
 
 
/*
run:
 
true
 
*/

 



answered Sep 13, 2023 by avibootz
edited Sep 14, 2023 by avibootz

Related questions

1 answer 125 views
125 views asked Sep 14, 2023 by avibootz
1 answer 122 views
122 views asked Sep 14, 2023 by avibootz
1 answer 137 views
137 views asked Sep 14, 2023 by avibootz
1 answer 117 views
1 answer 103 views
103 views asked Sep 14, 2023 by avibootz
1 answer 132 views
...