We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
I'm trying to replicate the Full Counting Sort question with a really small change, which some of you may have seen.
In my adaptation, I expect everything to work exactly like the Full Counting Sort problem, with the only difference being that instead of a "-"being shown, I would expect a "lol" instead.
So let me run you through my thought process here.
We are given a List of Lists List<List<String>> like this [['1','a'],['3','c'],['2','b'],['4','d']] and we are to convert it into a string (How it is done is explained via the link above).
So my thought process was to
Create a hashmap from the inputs provided, where x[0] would be the keys (In my example List, x[0] would be'1','2','3','4' would be the keys and the string associates with them would be the values
Firstly, (before sorting), change the keys of the first half elements of the Hashmap to "lol" similar to how the instructions on the site require us to change it to "-".
Change the data type of the keys from string to ints, to facilitate with sorting. This is done by copying the data from the original hashmap to a new hashmap, changing the data type during the conversion.
Sort the array using the keys, which are now ints, using a treemap.
Create a stringbuilder and then iterate over the tree map and append the treemap values to the stringbuilder.
Now that I've explained my thought process, here is my code.
publicclasshackkerankstuff{publicstaticvoidcountSort(List<List<String>>arr){//taking the input List<List<String>> and storing it as a HashmapHashMap<String,String>p=newHashMap<>();for(List<String>mapping:arr){p.put(mapping.get(0),mapping.get(1));}//converting the values of the first half of the keys to a "lol"intmaxiterations=p.keySet().size()/2;Iterator<String>iterator=p.keySet().iterator();for(inti=0;i<maxiterations;i++){if(iterator.hasNext()){Stringkey=iterator.next();p.replace(key,"lol");}}//Converting data type of key to intMap<Integer,String>map2=newHashMap<>();for(Map.Entry<String,String>entry:p.entrySet())map2.put(Integer.parseInt(entry.getKey()),entry.getValue());//sort the map based on the keys, since they are now intsTreeMap<Integer,String>sorted=newTreeMap<>();sorted.putAll(map2);//create a stringbuilder to add the Treemap values to the stringbuilderStringBuilders=newStringBuilder();for(Map.Entry<Integer,String>entry:sorted.entrySet()){s.append(entry.getValue()).append(" ");}//after running the program, s should return the string as expected.
Is there any way I could change this to improve this code or are there any edits necessary to be made based on what you see?
Thank you.
Is there any way I could change this to improve this code or are there any edits necessary to be made based on what you see?
Thank you.
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
The Full Counting Sort
You are viewing a single comment's thread. Return to all comments →
I'm trying to replicate the Full Counting Sort question with a really small change, which some of you may have seen.
In my adaptation, I expect everything to work exactly like the Full Counting Sort problem, with the only difference being that instead of a
"-"
being shown, I would expect a"lol"
instead.So let me run you through my thought process here.
We are given a List of Lists
List<List<String>>
like this[['1','a'],['3','c'],['2','b'],['4','d']]
and we are to convert it into a string (How it is done is explained via the link above).So my thought process was to
Create a hashmap from the inputs provided, where x[0] would be the keys (In my example List,
x[0]
would be'1','2','3','4'
would be the keys and the string associates with them would be the valuesFirstly, (before sorting), change the keys of the first half elements of the Hashmap to "lol" similar to how the instructions on the site require us to change it to "-".
Change the data type of the keys from string to ints, to facilitate with sorting. This is done by copying the data from the original hashmap to a new hashmap, changing the data type during the conversion.
Sort the array using the keys, which are now ints, using a treemap.
Create a stringbuilder and then iterate over the tree map and append the treemap values to the stringbuilder.
Now that I've explained my thought process, here is my code.
Is there any way I could change this to improve this code or are there any edits necessary to be made based on what you see?
Thank you.
Is there any way I could change this to improve this code or are there any edits necessary to be made based on what you see?
Thank you.