Construct String
This was a problem of the Starters 83 Division Contest of CodeChef and was I think the 4th or the 5th problem from the starting and was kinda medium level problem. I had implemented this problem in Python3 at first but this current implementation is in C++17. The overall test in this problem was according to me how we detect the pattern that is being made in the Process, and the usage of String traversal alongwith while loop, bcz this would have been done with a for loop too, but I doubt there would be a TLE for that case, basically they wanted to understand if we get this little catch or not. Cause we should always try to go for the most optimal option when it comes to Traversal in a string, this type of problem often comes in inputs of 10^9 order.
I feel that for such kind of problems going with C++ is a better option bcz as it is we won't be able to get any benefit of the built in features of Python like the Counter function and also the while loop in Python is slow as hell, so it would always be a better choice to go with C++.
Problem Statement:-
Consider a string consisting of lowercase Latin alphabets.
In one operation, you can pick an index from string and replace the character at that index with occurrences of the same character.
For example, if , in one operation, we can choose the index and replace with occurrences of . Thus, the string becomes .
Chefina has a string of length .
Help Chef design a string of minimum length such that Chef can convert it to using any (possibly zero) number of operations.
It is guaranteed that exactly one such exists.
Input Format
- The first line of input contains a single integer , the number of test cases.
- Each test case consists of multiple lines of input.
- The first line of each test case consists of an integer — the length of the string .
- The next line consists of string .
Output Format
For each test case, output on a new line, the string of minimum length, such that Chef can convert it to using any (possibly zero) number of operations.
Constraints
- consists of lowercase Latin alphabets only.
- The sum of over all test cases won't exceed .
Sample 1:
3 6 aaabbb 7 abbbbbc 4 abcd
ab abc abcd
Explanation:
Test case : With as , we can first replace with , and then with .
We cannot construct from a string having length less than .
Test case : With as , we can first replace with , and then with .
Test case : Here , and no operations need to be applied.
We cannot construct from a string having length less than .
Code Implementation:-
C++17
#include <bits/stdc++.h>
Python3:-
Explanation:-
Simply, the only logic in this problem is that we need to get the minimum possible occurrence of every element and according to the circumstance that an elem can be replaced by 3 of its occurrence, means 1 elem can be made to -->3,5,7,9,11,13... and so on, but what about the even freq elems, so that can be replaced by 2 of their occurrences, bcz 2 elems can be increased in the order--> 4,6,8,10,12,14... So, we just need to use this idea and replace even freq elems with 2 elems and odd freq elems with 1 elem. But there's a catch here, we cannot directly get the frequency of all elems, means in such a string --> "aaaabbbaaa", we will have to find the freq of elem a differently for both of its occurrences, we cannot directly consider it to be 7 as a whole, this was a mistake that I did for making use of the Counter in Python and was getting the WA for many Test Cases, apart from that the problem was kinda medium level.
Comments
Post a Comment