I need to check if the string is Panagram or not. Why I get right solution only when I use set. Why it is not working with string?
def checkIfPangram(sentence):
return len(set(sentence)) == 26 # answer is True
print(checkIfPangram('thequickbrownfoxjumpsoverthelazydog'))
but for this solution it does not work:
def checkIfPangram(sentence):
return len(sentence) == 26 #
print(checkIfPangram('thequickbrownfoxjumpsoverthelazydog'))
Answer is not correct. With len(sentence) I get only False, but if I use set(sentence) then solution is correct
>Solution :
In the first solution, you are using the set data structure to convert the sentence into a set of unique characters. The set data structure only allows unique elements, so by converting the sentence into a set, you are removing any duplicate characters. Then, you are checking if the length of the set is equal to 26, which is the number of unique letters in the English alphabet.
In the second solution, you are simply checking if the length of the sentence is equal to 26. This will only return True if the sentence has exactly 26 characters, regardless of whether those characters are all unique or not. This means that the second solution will not work for checking if a sentence is a pangram, because a pangram can have more than 26 characters if it includes duplicate letters.