Hot Topics
Interview Questions
1. TCS Technical Questions
2. CTS Technical Questions
3. Infosys Technical Questions
4. Capgemini Technical Questions
5. IBM Technical Questions
6. Wipro Technical Questions
7. Accenture Technical Questions
8. Deloitte Technical Questions
9. MindTree Technical Questions
10. NTT Data Technical Questions
11. Persistent Technical Questions
12. Hexaware Technical Questions
13. ZOHO Technical Questions
14. HCL Technical Questions
15. Genpact Technical Questions
16. udostres Technical Questions
17. samsung research Technical Questions
18. Media.net Technical Questions
19. Tejas Network Technical Questions
20. Kantar Technical Questions
21. Itron Technical Questions
22. Fractal Analyst Technical Questions
23. Lexmark International Technical Questions
24. Infeneon Technologies Technical Questions
25. Razorpay Technical Questions
26. payu Technical Questions
27. Navi Technical Questions
28. Tata Cliq Technical Questions
29. Grab Technical Questions
30. Schlumberger Technical Questions
31. Latenview Analytics Technical Questions
32. GreayB Technical Questions
33. Tally solution Technical Questions
34. Amagi Technical Questions
35. HFCL Technical Questions
36. Slice Technical Questions
37. Larsen & Turbo Technical Questions
38. Zenser Technical Questions
39. Siemens Technical Questions
40. Exon Movie Technical Questions
41. Gupshup Technical Questions
42. Smart cube Technical Questions
43. Celebal Tech Technical Questions
44. Teg Analytics Technical Questions
45. Commvault Systems Technical Questions
46. Incuture Technical Questions
47. Tiger Technical Questions
48. Bridgei2i Technical Questions
49. code nation Technical Questions
50. DE Show Technical Questions
51. Amazon Technical Questions
52. Flipkart Technical Questions
53. Morgan Stanly Technical Questions
54. duutsche bank Technical Questions
55. textas Technical Questions
56. pwc Technical Questions
57. salesforce Technical Questions
58. optum Technical Questions
59. Nvidia Technical Questions
60. Itc Infotech Technical Questions
61. linkedin Technical Questions
62. Adobe Technical Questions
Nagarro Question Solved | Alex’s Love for letter | Codewindow.in
- Question 1
Alex had written all the 26 letters from a to z on a board initially. Later, he arranged those letters forming a new sequence.
Alex gives you this new sequence of 26 letters, another string S of length N and a number K.
Your task is to find and return t5yhe lexicographically smallest subsequence of length K,formed from string S,in accordance with the new sequence of letters provided to you.
Note:
A subsequence of a string is formed by deleting a few (possibly 0) characters from string.
Given two strings,a string will be lexicographically smaller than other string,if it comes first in the dictionary.
Input Specification:
Input1: A string denoting the new sequence of letters.
Input2: An integer value N denoting the length of string S.
Input3: An integer value K denoting the length of the subsequence which you would be required to return.
Input4: The string S.
Output Specification:
Return the lexicographically smallest subsequence as per the sequence of letters in input1.
Example 1:
Input1: abcdefghijklmnopqrstuvwxyz
Input2: 4
Input3: 2
Input4: rfov
Output: fo
Explanation:
Here we need to form subsequences of length 2 from the given string, rfov.
The possible subsequences are rf, ro,rv,fo and so on.
Considering the sequence of letters in input1, fo is lexicographically the smallest. Therefore, fo is returned as the output.
Example 2:
Input1: zyxwvutsrqponmlkjhgfedcba
Input2: 4
Input3: 2
Input4: ehkx
Output: kx
Explanation:
Here we need to form subsequences of length 2 from the given string, ehkx.
The possible subsequences are ek, ex,hx and so on.
Considering the sequence of letters in input1, kx is lexicographically the smallest. Therefore, kx is returned as the output.
Input:
'zyxwvutsrqponmlkjihgfedcba'
4
2
'ehkx'
Output:
kx
- Answer
Python
TEST CASE 1:
# Python3 Code implementation for above problem
def AlexLoveLetter(input1, input2, input3, input4):
test_str = input4
K = input3
res = [test_str[i: j] for i in range(len(test_str)) for j in range(i + 1, len(test_str) + 1) if len(test_str[i:j]) == K]
res.sort()
ans=[]
for i in res:
t=[]
for k in i:
t.append(input1.find(k))
ans.append([i, t])
ans.sort(key = lambda x: x[1])
return ans[0][0]
input1 = 'zyxwvutsrqponmlkjihgfedcba'
input2 = 4
input3 = 2
input4 = 'ehkx'
print(AlexLoveLetter(input1, input2, input3, input4))
input1 = 'abcdefghijklmnopqrstuvwxyz'
input2 = 4
input3 = 2
input4 = 'rfov'
print(AlexLoveLetter(input1, input2, input3, input4))
Popular Category
Hot Topics
Interview Questions
1. TCS Technical Questions
2. CTS Technical Questions
3. Infosys Technical Questions
4. Capgemini Technical Questions
5. IBM Technical Questions
6. Wipro Technical Questions
7. Accenture Technical Questions
8. Deloitte Technical Questions
9. MindTree Technical Questions
10. NTT Data Technical Questions
11. Persistent Technical Questions
12. Hexaware Technical Questions
13. ZOHO Technical Questions
14. HCL Technical Questions
15. Genpact Technical Questions
16. udostres Technical Questions
17. samsung research Technical Questions
18. Media.net Technical Questions
19. Tejas Network Technical Questions
20. Kantar Technical Questions
21. Itron Technical Questions
22. Fractal Analyst Technical Questions
23. Lexmark International Technical Questions
24. Infeneon Technologies Technical Questions
25. Razorpay Technical Questions
26. payu Technical Questions
27. Navi Technical Questions
28. Tata Cliq Technical Questions
29. Grab Technical Questions
30. Schlumberger Technical Questions
31. Latenview Analytics Technical Questions
32. GreayB Technical Questions
33. Tally solution Technical Questions
34. Amagi Technical Questions
35. HFCL Technical Questions
36. Slice Technical Questions
37. Larsen & Turbo Technical Questions
38. Zenser Technical Questions
39. Siemens Technical Questions
40. Exon Movie Technical Questions
41. Gupshup Technical Questions
42. Smart cube Technical Questions
43. Celebal Tech Technical Questions
44. Teg Analytics Technical Questions
45. Commvault Systems Technical Questions
46. Incuture Technical Questions
47. Tiger Technical Questions
48. Bridgei2i Technical Questions
49. code nation Technical Questions
50. DE Show Technical Questions
51. Amazon Technical Questions
52. Flipkart Technical Questions
53. Morgan Stanly Technical Questions
54. duutsche bank Technical Questions
55. textas Technical Questions
56. pwc Technical Questions
57. salesforce Technical Questions
58. optum Technical Questions
59. Nvidia Technical Questions
60. Itc Infotech Technical Questions
61. linkedin Technical Questions
62. Adobe Technical Questions