HackerRank: Reverse Shuffle Merge
private void solveOne() throws IOException {
String s = nextToken();
s = reverseString(s);
final int alphaSize = 26;
int[] count = new int[alphaSize];
for (int i = 0; i < s.length(); i++)
++count[s.charAt(i) - 'a'];
int needLength = 0;
for (int i = 0; i < alphaSize; i++) {
if (count[i] % 2 != 0)
throw new AssertionError();
count[i] /= 2;
needLength += count[i];
}
StringBuilder result = new StringBuilder();
int[][] counts = new int[s.length()][alphaSize];
for (int i = s.length() - 1; i >= 0; i--) {
for (int j = 0; j < alphaSize; j++)
counts[i][j] = (i + 1 == s.length() ? 0 : counts[i + 1][j]);
counts[i][s.charAt(i) - 'a']++;
}
int leftPointer = 0;
for (int it = 0; it < needLength; it++) {
int resultIndex = -1;
for (int i = leftPointer; i < s.length(); i++) {
// out.println(it + " " + i + " " + resultIndex);
if (count[s.charAt(i) - 'a'] > 0) {
if (resultIndex == -1
|| s.charAt(i) < s.charAt(resultIndex)) {
if (isOk(count, counts[i]))
resultIndex = i;
}
}
}
result.append(s.charAt(resultIndex));
--count[s.charAt(resultIndex) - 'a'];
leftPointer = resultIndex + 1;
// out.println(resultIndex + " " + result);
// out.flush();
}
out.println(result);
}
Given a string S, we define some operations on the string as follows
a. reverse(S) denotes the string obtained by reversing the string S. Eg: reverse("abc") = "cba"
b. shuffle(S) denotes any string that's a permutation of the string S. Eg: shuffle("god") ∈ ['god', 'gdo', 'ogd', 'odg', 'dgo', 'dog']
c. merge(S1,S2) denotes any string that's obtained by interspersing the two strings S1 & S2, maintaining the order of characters in S1 & S2.
Eg: S1 = "abc" & S2 = "def", one possible result of merge(S1,S2) could be "abcdef", another could be "abdecf", another could be "adbecf" and so on..
Eg: S1 = "abc" & S2 = "def", one possible result of merge(S1,S2) could be "abcdef", another could be "abdecf", another could be "adbecf" and so on..
Given a string S, such that S ∈ merge(reverse(A), shuffle(A)), for some string A, can you find the lexicographically smallest such A.
https://codepair.hackerrank.com/paper/qk0iAcrq?b=eyJyb2xlIjoiY2FuZGlkYXRlIiwibmFtZSI6ImplZmZlcnl5dWFuIiwiZW1haWwiOiJ5dWFueXVuLmtlbm55QGdtYWlsLmNvbSJ9private void solveOne() throws IOException {
String s = nextToken();
s = reverseString(s);
final int alphaSize = 26;
int[] count = new int[alphaSize];
for (int i = 0; i < s.length(); i++)
++count[s.charAt(i) - 'a'];
int needLength = 0;
for (int i = 0; i < alphaSize; i++) {
if (count[i] % 2 != 0)
throw new AssertionError();
count[i] /= 2;
needLength += count[i];
}
StringBuilder result = new StringBuilder();
int[][] counts = new int[s.length()][alphaSize];
for (int i = s.length() - 1; i >= 0; i--) {
for (int j = 0; j < alphaSize; j++)
counts[i][j] = (i + 1 == s.length() ? 0 : counts[i + 1][j]);
counts[i][s.charAt(i) - 'a']++;
}
int leftPointer = 0;
for (int it = 0; it < needLength; it++) {
int resultIndex = -1;
for (int i = leftPointer; i < s.length(); i++) {
// out.println(it + " " + i + " " + resultIndex);
if (count[s.charAt(i) - 'a'] > 0) {
if (resultIndex == -1
|| s.charAt(i) < s.charAt(resultIndex)) {
if (isOk(count, counts[i]))
resultIndex = i;
}
}
}
result.append(s.charAt(resultIndex));
--count[s.charAt(resultIndex) - 'a'];
leftPointer = resultIndex + 1;
// out.println(resultIndex + " " + result);
// out.flush();
}
out.println(result);
}