Python practice
Given a non-negative integer represented as a string num
and an integer k
, remove k
digits from the number so that the new number is the smallest possible. The remaining digits should maintain their original order in the string. Return the result as a string.
Input Format:
The first line contains the string
num, representing the non-negative integer.
The second line contains the integer
k.
Output Format:
Print the smallest possible integer as a string after removing
kdigits.
Stack based solution:
Coach Manish
Manish is the coach of a high school football team. He has come up with a training regime of difficulty ‘d’ but he is afraid that it might result in his team getting exhausted quickly and prone to fatigue.
So he has decided to break the training in ’n’ number of days where every day has a certain amount of difficulty and the difficulty of each day adds up to ‘d’ the original difficulty.
But he also wants to make sure that his training results in a greater output so he cannot spread the difficulty of training without planning. The result can be determined by calculating the GCD of the difficulty of all the days. Return the greatest result that can be achieved from the training.
Input Format:
You are given two space-separated integers d and n, denoting the total difficulty and number of days it is divided into.
Output Format:
You have to return a single integer denoting the maximum result that can be achieved from the training.
Sample Input:
9 2
Sample Output:
3
Explanation:
We can break the difficulty in 3 and 6 whose GCD results in 3 which is the greatest output possible.
GCD based solution:
Duplicate Letters
Dholu is very creative and wants to play with strings and he has a very good idea. You have given a string s, remove duplicate letters so that every letter appears once and only once. You must make sure your result is the smallest in lexicographical order
Input Format:
- first line consists of a string
Output Format:
- print the output string
Sample Input:
cbacdcbc
Sample Output:
abcd
Explanation: Since if we only collect unique letters, they come out to be ‘c’, ‘b’, ‘a’ and ‘d’ and when they are arranged lexicographically the output is abcd.
Constraints:
1<= s.length<= 10000
solution: