-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathk-pangrams.py
28 lines (22 loc) · 1.02 KB
/
k-pangrams.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
'''
Given a string str and an integer k, return true if the string can be changed into a pangram after at most k operations, else return false.
A single operation consists of swapping an existing alphabetic character with any other alphabetic character.
Note - A pangram is a sentence containing every letter in the english alphabet.
Input: str = "a b c d e f g h i j k l m", k = 20
Output: false
Explaanation: Since there are only 25 charaacters only in this case, so no amount of swapping we can have complete alphabets here.
Approach:
The code checks if a given string can be converted into a pangram (a string containing every letter of the English alphabet at least once) by adding at most k characters.
Time complexity: O(n)
Space complexity: O(1)
'''
class Solution:
def kPangram(self,string, k):
# code here
string=string.replace(' ','')
z=len(string)
x=len(set(string))
if 26-x<=z-x and 26-x<=k:
return True
else:
return False