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:
function solution(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'.
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(S) {
// write your code in JavaScript (Node.js 8.9.4)
return S.reduce()
}
let ans;
function solution(S) {
// write your code in JavaScript (Node.js 8.9.4)
let a = S.replace(/abb/g, 'baa');
if (S == a) {
ans = a;
}else{
solution(a)
}
return ans;
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(S) {
// write your code in JavaScript (Node.js 8.9.4)
return
}
let ans;
function solution(S) {
// write your code in JavaScript (Node.js 8.9.4)
let a = S.replace(/abb/g, 'baa');
if (S == a) {
ans = a;
}else{
solution(a)
}
return ans;
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(S) {
// write your code in JavaScript (Node.js 8.9.4)
return
}
let ans;
function solution(S) {
// write your code in JavaScript (Node.js 8.9.4)
let a = S.replace(/abb/g, 'baa');
if (S == a) {
ans = a;
}else{
solution(a)
}
return ans;
while ()
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(S) {
// write your code in JavaScript (Node.js 8.9.4)
return
}
let ans;
function solution(S) {
// write your code in JavaScript (Node.js 8.9.4)
let a = S.replace(/abb/g, 'baa');
if (S == a) {
ans = a;
}else{
solution(a)
}
return ans;
while (ans == S){
}
}
// you can write to stdout for debugging purposes, e.g.
// console.log('this is a debug message');
function solution(S) {
// write your code in JavaScript (Node.js 8.9.4)
return
}
let ans;
function solution(S) {
// write your code in JavaScript (Node.js 8.9.4)
let a =
if (S == a) {
ans = a;
}else{
solution(a)
}
return ans;
while (ans == S){
S.replace(/abb/g, 'baa');
}
}
TypeError: Cannot read property 'replace' of undefined at solution (solution.js:8:23) at solutionWrapper (/tmp/exec.js:394:28) at Promise.resolve.then (/tmp/exec.js:420:24) at <anonymous> at process._tickCallback (internal/process/next_tick.js:188:7) at Function.Module.runMain (module.js:686:11) at startup (bootstrap_node.js:187:16) at bootstrap_node.js:608:3
TypeError: Cannot read property 'replace' of undefined at solution (solution.js:8:23) at solutionWrapper (/tmp/exec.js:394:28) at Promise.resolve.then (/tmp/exec.js:420:24) at <anonymous> at process._tickCallback (internal/process/next_tick.js:188:7) at Function.Module.runMain (module.js:686:11) at startup (bootstrap_node.js:187:16) at bootstrap_node.js:608:3
TypeError: Cannot read property 'replace' of undefined at solution (solution.js:8:23) at solutionWrapper (/tmp/exec.js:394:28) at Promise.resolve.then (/tmp/exec.js:420:24) at <anonymous> at process._tickCallback (internal/process/next_tick.js:188:7) at Function.Module.runMain (module.js:686:11) at startup (bootstrap_node.js:187:16) at bootstrap_node.js:608:3
The following issues have been detected: timeout errors.
Simple big test. N = 100,000. Score x 3.
Killed. Hard limit reached: 6.000 sec.