-
Notifications
You must be signed in to change notification settings - Fork 1
/
WORD_K_SELECTION_1.PY
53 lines (45 loc) · 1.35 KB
/
WORD_K_SELECTION_1.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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# Words - K Selection - 1
# https://nados.io/question/words-k-selection-1
# 1. You are given a word (may have one character repeat more than once).
# 2. You are given an integer k.
# 2. You are required to generate and print all ways you can select k distinct characters out of the
# word.
# Note -> Use the code snippet and follow the algorithm discussed in question video. The judge can't
# force you but the intention is to teach a concept. Play in spirit of the question.
# Sample Input
# aabbbccdde
# 3
# Sample Output
# abc
# abd
# abe
# acd
# ace
# ade
# bcd
# bce
# bde
# cde
def get_solution(start,uniqstr,k,ans,count):
# base case when index:start reaches to len(uniqstr)
if start>=len(uniqstr):
# we only want the k items so print : k
if k==count:
print(ans)
return
# take character by index
char = uniqstr[start]
# if we include that character : call
get_solution(start+1,uniqstr,k,ans+char,count+1)
# if we do not include that character : call
get_solution(start+1,uniqstr,k,ans,count)
if __name__ == '__main__':
string = "aabbbccdde"
hashmap = set()
uniq = ""
for i in string:
if i not in hashmap:
uniq+=i
hashmap.add(i)
k = 3
get_solution(0,uniq,k,"",0)