Sunday, November 17, 2024
Google search engine
HomeLanguagesUsing Set() in Python Pangram Checking

Using Set() in Python Pangram Checking

Given a string check if it is Pangram or not. A pangram is a sentence containing every letter in the English Alphabet. Lowercase and Uppercase are considered the same. Examples:

Input : str = 'The quick brown fox jumps over 
               the lazy dog'
Output : Yes
// Contains all the characters from ā€˜aā€™ to ā€˜zā€™

Input : str='The quick brown fox jumps over the dog'
Output : No
// Doesnā€™t contains all the characters from ā€˜aā€™
// to ā€˜zā€™, as ā€˜lā€™, ā€˜zā€™, ā€˜yā€™ are missing

This problem has existing solution please refer Pangram Checking link. We will solve this in Python using Set() data structure and List() comprehension. Approach is very simple,

  1. Convert complete sentence in lower case using lower() method of string data type in python.
  2. Now pass this sentence into Set(str) so that we could have list of all unique characters present in given string.
  3. Now separate out list of all alphabets (a-z), if length of list is 26 that means all characters are present and sentence is Pangram otherwise not.

Implementation:

Python3




# function to check pangram
Ā 
def pangram(input):
Ā Ā Ā Ā Ā 
Ā Ā Ā Ā # convert input string into lower case
Ā Ā Ā Ā input = input.lower()
Ā Ā Ā Ā Ā 
Ā Ā Ā Ā # convert input string into Set() so that we will
Ā Ā Ā Ā # list of all unique characters present in sentence
Ā Ā Ā Ā input = set(input)
Ā 
Ā Ā Ā Ā # separate out all alphabets
Ā Ā Ā Ā # ord(ch) returns ascii value of character
Ā Ā Ā Ā alpha = [ ch for ch in input if ord(ch) in range(ord('a'), ord('z')+1)]
Ā 
Ā Ā Ā Ā if len(alpha) == 26:
Ā Ā Ā Ā Ā Ā Ā Ā return 'true'
Ā Ā Ā Ā else:
Ā Ā Ā Ā Ā Ā Ā Ā return 'false'
Ā 
# Driver program
if __name__ == "__main__":
Ā Ā Ā Ā input = 'The quick brown fox jumps over the lazy dog'
Ā Ā Ā Ā print (pangram(input))


Java




import java.util.*;
Ā 
public class PangramChecker {
Ā Ā Ā Ā public static void main(String[] args) {
Ā Ā Ā Ā Ā Ā Ā Ā // Define the input string
Ā Ā Ā Ā Ā Ā Ā Ā String input = "The quick brown fox jumps over the lazy dog";
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā // Call the pangram function and print the result
Ā Ā Ā Ā Ā Ā Ā Ā System.out.println(pangram(input));
Ā Ā Ā Ā }
Ā 
Ā Ā Ā Ā public static String pangram(String input) {
Ā Ā Ā Ā Ā Ā Ā Ā // Convert the input string to lowercase
Ā Ā Ā Ā Ā Ā Ā Ā input = input.toLowerCase();
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā // Create a set to store unique characters in the input string
Ā Ā Ā Ā Ā Ā Ā Ā Set<Character> inputSet = new HashSet<>();
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā // Loop through each character in the input string
Ā Ā Ā Ā Ā Ā Ā Ā for (int i = 0; i < input.length(); i++) {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā char ch = input.charAt(i);
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // Check if the character is an alphabet
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā if (ch >= 'a' && ch <= 'z') {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā // Add the character to the input set
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā inputSet.add(ch);
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā }
Ā Ā Ā Ā Ā Ā Ā Ā }
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā // Convert the input set to a list and sort it
Ā Ā Ā Ā Ā Ā Ā Ā List<Character> alpha = new ArrayList<>(inputSet);
Ā Ā Ā Ā Ā Ā Ā Ā Collections.sort(alpha);
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā // Create a StringBuilder to concatenate the sorted characters into a string
Ā Ā Ā Ā Ā Ā Ā Ā StringBuilder sb = new StringBuilder();
Ā Ā Ā Ā Ā Ā Ā Ā for (char ch : alpha) {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā sb.append(ch);
Ā Ā Ā Ā Ā Ā Ā Ā }
Ā Ā Ā Ā Ā Ā Ā Ā String uniqueChars = sb.toString();
Ā 
Ā Ā Ā Ā Ā Ā Ā Ā // Check if the length of the resulting string is 26 (i.e., contains all alphabets)
Ā Ā Ā Ā Ā Ā Ā Ā if (uniqueChars.length() == 26) {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā return "true";
Ā Ā Ā Ā Ā Ā Ā Ā } else {
Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā Ā return "false";
Ā Ā Ā Ā Ā Ā Ā Ā }
Ā Ā Ā Ā }
}


Output

true

Time complexity:Ā 

  • Lowercasing the input string takes O(n) time, where n is the length of the string.
  • Converting the input string to a set takes O(n) time.
  • The list comprehension that separates out all alphabets takes O(n) time.Ā 
  • The len() function to count the number of alphabets takes O(1) time.Ā 
  • The overall time complexity of the function is therefore O(n).

Auxiliary space:

  • Creating the set to store unique characters in the input string requires O(n) space, where n is the length of the string.Ā 
  • Creating the list of alphabets requires O(k) space, where k is the number of alphabets in the string (which is at most 26).Ā 
  • The overall auxiliary space complexity of the function is therefore O(n).

RELATED ARTICLES

Most Popular

Recent Comments

ź°•ģ„œźµ¬ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
źøˆģ²œźµ¬ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
ź“‘ėŖ…ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ź“‘ėŖ…ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ė¶€ģ²œģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
źµ¬ģ›”ė™ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ź°•ģ„œźµ¬ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ģ˜¤ģ‚°ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ź“‘ėŖ…ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
ģ•ˆģ–‘ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
ė¶€ģ²œģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ė™ķƒ„ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ģ„œģšøģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ė¶„ė‹¹ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ė¶€ģ²œģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ķ™”ź³”ė™ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
ź°•ģ„œźµ¬ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
ź³ ģ–‘ģ¶œģž„ģ•ˆė§ˆ on How to store XML data into a MySQL database using Python?
ķ™”ģ„±ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?
ģ²œķ˜øė™ģ¶œģž„ė§ˆģ‚¬ģ§€ on How to store XML data into a MySQL database using Python?