Let us define a transformation that, given a string S of letters 'a' and/or 'b', replaces some consecutive sequence "abb" in S by "baa".
For example, after applying such a transformation to the string "abbabb", both strings "baaabb" and "abbbaa" can be obtained.
Write a function:
class Solution { public String solution(String S); }
that, given a string S of length N, returns the alphabetically largest string that can be obtained by performing the above operation any number of times.
Examples:
1. Given S = "ababb", your function should return "baaaa".
"baaaa" is alphabetically the largest possible outcome and may be obtained from "ababb" by using two transformations:
"ababb" → "abbaa" → "baaaa"
2. Given S = "abbbabb", your function should return "babaaaa".
"babaaaa" may be obtained from "abbbabb" by using three transformations:
"abbbabb" → "abbbbaa" → "baabbaa" → "babaaaa"
3. Given S = "aaabab", your function should return "aaabab".
No operation can be performed on S since it contains no "abb" fragment.
Write an efficient algorithm for the following assumptions:
- N is an integer within the range [1..100,000];
- string S is made only of the characters 'a' and/or 'b'.
class Solution {
public String solution(String S) {
StringBuilder r = new StringBuilder();
char[] A = S.toCharArray();
BigInteger[] F = new BigInteger[A.length];
BigInteger sum = BigInteger.ZERO;
int j = A.length - 1;
for (int i = 0; i < A.length; i++) {
if (i == 0)
F[j] = BigInteger.ONE;
else if (i == 1)
F[j] = BigInteger.TWO;
else
F[j] = F[j + 1].add(F[j + 2]);
if (A[j] == 'b')
sum = sum.add(F[j]);
j--;
}
for (int i = 0; i < A.length; i++) {
if (sum.compareTo(F[i]) >= 0) {
sum = sum.subtract(F[i]);
r.append('b');
} else
r.append('a');
}
return r.toString();
}
}
import java.math.BigInteger;
class Solution {
public String solution(String S) {
StringBuilder r = new StringBuilder();
char[] A = S.toCharArray();
BigInteger[] F = new BigInteger[A.length];
BigInteger sum = BigInteger.ZERO;
int j = A.length - 1;
for (int i = 0; i < A.length; i++) {
if (i == 0)
F[j] = BigInteger.ONE;
else if (i == 1)
F[j] = BigInteger.TWO;
else
F[j] = F[j + 1].add(F[j + 2]);
if (A[j] == 'b')
sum = sum.add(F[j]);
j--;
}
for (int i = 0; i < A.length; i++) {
if (sum.compareTo(F[i]) >= 0) {
sum = sum.subtract(F[i]);
r.append('b');
} else
r.append('a');
}
return r.toString();
}
}
Solution.java:16: error: TWO has private access in BigInteger F[j] = BigInteger.TWO; ^ 1 error
import java.math.BigInteger;
class Solution {
public String solution(String S) {
StringBuilder r = new StringBuilder();
char[] A = S.toCharArray();
BigInteger[] F = new BigInteger[A.length];
BigInteger sum = BigInteger.ZERO;
int j = A.length - 1;
for (int i = 0; i < A.length; i++) {
if (i == 0)
F[j] = BigInteger.ONE;
else if (i == 1)
F[j] = BigInteger.ONE;
else
F[j] = F[j + 1].add(F[j + 2]);
if (A[j] == 'b')
sum = sum.add(F[j]);
j--;
}
for (int i = 0; i < A.length; i++) {
if (sum.compareTo(F[i]) >= 0) {
sum = sum.subtract(F[i]);
r.append('b');
} else
r.append('a');
}
return r.toString();
}
}
import java.math.BigInteger;
class Solution {
public String solution(String S) {
StringBuilder r = new StringBuilder();
char[] A = S.toCharArray();
BigInteger[] F = new BigInteger[A.length];
BigInteger sum = BigInteger.ZERO;
int j = A.length - 1;
for (int i = 0; i < A.length; i++) {
if (i == 0)
F[j] = BigInteger.ONE;
else if (i == 1)
F[j] = new BigInteger(2);
else
F[j] = F[j + 1].add(F[j + 2]);
if (A[j] == 'b')
sum = sum.add(F[j]);
j--;
}
for (int i = 0; i < A.length; i++) {
if (sum.compareTo(F[i]) >= 0) {
sum = sum.subtract(F[i]);
r.append('b');
} else
r.append('a');
}
return r.toString();
}
}
import java.math.BigInteger;
class Solution {
public String solution(String S) {
StringBuilder r = new StringBuilder();
char[] A = S.toCharArray();
BigInteger[] F = new BigInteger[A.length];
BigInteger sum = BigInteger.ZERO;
int j = A.length - 1;
for (int i = 0; i < A.length; i++) {
if (i == 0)
F[j] = BigInteger.ONE;
else if (i == 1)
F[j] = new BigInteger(2);
else
F[j] = F[j + 1].add(F[j + 2]);
if (A[j] == 'b')
sum = sum.add(F[j]);
j--;
}
for (int i = 0; i < A.length; i++) {
if (sum.compareTo(F[i]) >= 0) {
sum = sum.subtract(F[i]);
r.append('b');
} else
r.append('a');
}
return r.toString();
}
}
Solution.java:16: error: BigInteger(long) has private access in BigInteger F[j] = new BigInteger(2); ^ 1 error
import java.math.BigInteger;
class Solution {
public String solution(String S) {
StringBuilder r = new StringBuilder();
char[] A = S.toCharArray();
BigInteger[] F = new BigInteger[A.length];
BigInteger sum = BigInteger.ZERO;
int j = A.length - 1;
for (int i = 0; i < A.length; i++) {
if (i == 0)
F[j] = BigInteger.ONE;
else if (i == 1)
F[j] = new BigInteger("2");
else
F[j] = F[j + 1].add(F[j + 2]);
if (A[j] == 'b')
sum = sum.add(F[j]);
j--;
}
for (int i = 0; i < A.length; i++) {
if (sum.compareTo(F[i]) >= 0) {
sum = sum.subtract(F[i]);
r.append('b');
} else
r.append('a');
}
return r.toString();
}
}
import java.math.BigInteger;
class Solution {
public String solution(String S) {
StringBuilder r = new StringBuilder();
char[] A = S.toCharArray();
BigInteger[] F = new BigInteger[A.length];
BigInteger sum = BigInteger.ZERO;
int j = A.length - 1;
for (int i = 0; i < A.length; i++) {
if (i == 0)
F[j] = BigInteger.ONE;
else if (i == 1)
F[j] = new BigInteger("2");
else
F[j] = F[j + 1].add(F[j + 2]);
if (A[j] == 'b')
sum = sum.add(F[j]);
j--;
}
for (int i = 0; i < A.length; i++) {
if (sum.compareTo(F[i]) >= 0) {
sum = sum.subtract(F[i]);
r.append('b');
} else
r.append('a');
}
return r.toString();
}
}
import java.math.BigInteger;
class Solution {
public String solution(String S) {
StringBuilder r = new StringBuilder();
char[] A = S.toCharArray();
BigInteger[] F = new BigInteger[A.length];
BigInteger sum = BigInteger.ZERO;
int j = A.length - 1;
for (int i = 0; i < A.length; i++) {
if (i == 0)
F[j] = BigInteger.ONE;
else if (i == 1)
F[j] = new BigInteger("2");
else
F[j] = F[j + 1].add(F[j + 2]);
if (A[j] == 'b')
sum = sum.add(F[j]);
j--;
}
for (int i = 0; i < A.length; i++) {
if (sum.compareTo(F[i]) >= 0) {
sum = sum.subtract(F[i]);
r.append('b');
} else
r.append('a');
}
return r.toString();
}
}
The following issues have been detected: timeout errors.
Simple big test. N = 100,000. Score x 3.
running time: 2.544 sec., time limit: 0.272 sec.
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space at java.math.BigInteger.add(BigInteger.java:1324) at java.math.BigInteger.add(BigInteger.java:1230) at Solution.solution(Solution.java:18) at Exec.run(exec.java:48) at Exec.main(exec.java:34)
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space at java.math.BigInteger.add(BigInteger.java:1324) at java.math.BigInteger.add(BigInteger.java:1230) at Solution.solution(Solution.java:18) at Exec.run(exec.java:48) at Exec.main(exec.java:34)
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space at java.math.BigInteger.add(BigInteger.java:1324) at java.math.BigInteger.add(BigInteger.java:1230) at Solution.solution(Solution.java:21) at Exec.run(exec.java:48) at Exec.main(exec.java:34)
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space at java.math.BigInteger.add(BigInteger.java:1324) at java.math.BigInteger.add(BigInteger.java:1230) at Solution.solution(Solution.java:21) at Exec.run(exec.java:48) at Exec.main(exec.java:34)
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space at java.math.BigInteger.add(BigInteger.java:1324) at java.math.BigInteger.add(BigInteger.java:1230) at Solution.solution(Solution.java:21) at Exec.run(exec.java:48) at Exec.main(exec.java:34)
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space at java.math.BigInteger.add(BigInteger.java:1324) at java.math.BigInteger.add(BigInteger.java:1230) at Solution.solution(Solution.java:18) at Exec.run(exec.java:48) at Exec.main(exec.java:34)
Monotone string. N = 100,000.
running time: 3.336 sec., time limit: 0.304 sec.
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space at java.math.BigInteger.add(BigInteger.java:1324) at java.math.BigInteger.add(BigInteger.java:1230) at Solution.solution(Solution.java:21) at Exec.run(exec.java:48) at Exec.main(exec.java:34)
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space at java.math.BigInteger.add(BigInteger.java:1324) at java.math.BigInteger.add(BigInteger.java:1230) at Solution.solution(Solution.java:21) at Exec.run(exec.java:48) at Exec.main(exec.java:34)
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space at java.math.BigInteger.add(BigInteger.java:1324) at java.math.BigInteger.add(BigInteger.java:1230) at Solution.solution(Solution.java:18) at Exec.run(exec.java:48) at Exec.main(exec.java:34)
Random big string. N = 100,000.
running time: 2.516 sec., time limit: 0.288 sec.
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space at java.math.BigInteger.add(BigInteger.java:1324) at java.math.BigInteger.add(BigInteger.java:1230) at Solution.solution(Solution.java:18) at Exec.run(exec.java:48) at Exec.main(exec.java:34)
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space at java.math.BigInteger.add(BigInteger.java:1324) at java.math.BigInteger.add(BigInteger.java:1230) at Solution.solution(Solution.java:21) at Exec.run(exec.java:48) at Exec.main(exec.java:34)
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space at java.math.BigInteger.add(BigInteger.java:1324) at java.math.BigInteger.add(BigInteger.java:1230) at Solution.solution(Solution.java:21) at Exec.run(exec.java:48) at Exec.main(exec.java:34)
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space at java.math.BigInteger.add(BigInteger.java:1324) at java.math.BigInteger.add(BigInteger.java:1230) at Solution.solution(Solution.java:21) at Exec.run(exec.java:48) at Exec.main(exec.java:34)
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space at java.math.BigInteger.add(BigInteger.java:1324) at java.math.BigInteger.add(BigInteger.java:1230) at Solution.solution(Solution.java:21) at Exec.run(exec.java:48) at Exec.main(exec.java:34)