forked from keshavnandan/Topcoder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathEllysSortingTrimmer.txt
102 lines (61 loc) · 2.83 KB
/
EllysSortingTrimmer.txt
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
PROBLEM STATEMENT
Elly has a string S of uppercase letters and a magic device that can modify the string. The strength of the device is an int L.
The device is used in the following way. The user enters a 0-based index i such that 0 <= i <= length(S)-L. The device then performs the following changes:
It leaves the first i characters (i.e., characters with indices 0 through i-1) untouched.
It rearranges the next L characters (i.e., characters with indices i through i+L-1) into alphabetical order.
It erases all the remaining characters (i.e., characters with indices i+L and more). Note that for i=length(S)-L no characters are erased.
The girl can use this "sorting trimmer" as many times as she likes. After each use she is left with the new version of the string.
In the examples below we use brackets to highlight the region that shall be sorted. For example, "ABRA[CADAB]RA" means that L=5 and Elly chose i=4. The device keeps the letters in front of the brackets, sorts the letters in the brackets, and throws away the rest.
Here is one way how Elly could have used a device with L = 5, starting with the string S = "ABRACADABRA":
"ABRAC[ADABR]A" -> "ABRACAABDR"
"ABR[ACAAB]DR" -> "ABRAAABC"
"A[BRAAA]BC" -> "AAAABR"
You are given the string S and the int L. Return the lexicographically smallest string Elly can obtain.
DEFINITION
Class:EllysSortingTrimmer
Method:getMin
Parameters:string, int
Returns:string
Method signature:string getMin(string S, int L)
NOTES
-A string A is lexicographically smaller than string B if A contains a smaller character in the first position where they differ. In case one of the strings ends before they have a different character, the shorter one is considered smaller.
CONSTRAINTS
-S will contain between 2 and 50 characters, inclusive.
-L will be between 2 and |S|, inclusive, where |S| denotes the number of characters in S.
-S will consist of uppercase characters of the English alphabet, only ('A'-'Z').
EXAMPLES
0)
"ABRACADABRA"
5
Returns: "AAAAA"
Please note that the example in the problem statement does not obtain the lexicographically smallest string. In fact, it is optimal to start by using the device on the last five characters of the string, transforming it from ABRACA[DABRA] to ABRACAAABDR.
1)
"ESPRIT"
3
Returns: "EIP"
We can obtain the answer in the following steps:
ES[PRI]T -> ESIPR
E[SIP]R -> EIPS
[EIP]S -> EIP
2)
"BAZINGA"
7
Returns: "AABGINZ"
We can use the sorting trimmer on the entire word.
3)
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
13
Returns: "ABCDEFGHIJKLM"
Even though the string is already sorted, shorter strings are considered lexicographically smaller, so we can use the device once to make the string as short as possible.
4)
"GOODLUCKANDHAVEFUN"
10
Returns: "AACDDEFGHK"
5)
"AAAWDIUAOIWDESBEAIWODJAWDBPOAWDUISAWDOOPAWD"
21
Returns: "AAAAAAAAABBDDDDDDDEEI"
6)
"TOPCODER"
3
Returns: "CDT"