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'.
// you can also use imports, for example:
// import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public String solution(String s) {
// write your code in Java SE 8
Deque<Character> stack = new ArrayDeque<>();
}
}
// you can also use imports, for example:
import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public String solution(String s) {
// write your code in Java SE 8
Deque<Character> stack = new ArrayDeque<>();
}
}
// you can also use imports, for example:
import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public String solution(String s) {
// write your code in Java SE 8
Deque<Character> stack = new ArrayDeque<>();
StringBuilder sb = new StringBuilder(s);
while()
}
}
// you can also use imports, for example:
import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public String solution(String s) {
// write your code in Java SE 8
Deque<Character> stack = new ArrayDeque<>();
StringBuilder sb = new StringBuilder(s);
while(sb.length() > 0)
{
}
}
}
// you can also use imports, for example:
import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public String solution(String s) {
// write your code in Java SE 8
Deque<Character> stack = new ArrayDeque<>();
StringBuilder sb = new StringBuilder(s);
while(sb.length() > 0)
{
}
}
}
// you can also use imports, for example:
import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public String solution(String s) {
// write your code in Java SE 8
Deque<Character> stack = new ArrayDeque<>();
StringBuilder sb = new StringBuilder(s);
char second = 'z', first = 'z';
while(sb.length() > 0)
{
if (stack.size() < 3)
{
stack.push()
}
}
}
}
// you can also use imports, for example:
import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public String solution(String s) {
// write your code in Java SE 8
Deque<Character> stack = new ArrayDeque<>();
StringBuilder sb = new StringBuilder(s);
char second = 'z', first = 'z';
while(sb.length() > 0)
{
if (stack.size() < 3)
{
stack.push(sb.charAt(0));
}
}
}
}
// you can also use imports, for example:
import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public String solution(String s) {
// write your code in Java SE 8
Deque<Character> stack = new ArrayDeque<>();
StringBuilder sb = new StringBuilder(s);
char second = 'z', first = 'z';
while(sb.length() > 0)
{
if (stack.size() < 3)
{
stack.push(sb.charAt(0));
s
}
}
}
}
// you can also use imports, for example:
import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public String solution(String s) {
// write your code in Java SE 8
Deque<Character> stack = new ArrayDeque<>();
StringBuilder sb = new StringBuilder(s);
char second = 'z', first = 'z';
while(sb.length() > 0)
{
if (stack.size() < 3)
{
stack.push(sb.charAt(0));
sb = sb.deleteCharAt(0);
}
}
}
}
// you can also use imports, for example:
import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public String solution(String s) {
// write your code in Java SE 8
Deque<Character> stack = new ArrayDeque<>();
StringBuilder sb = new StringBuilder(s);
char second = 'z', first = 'z';
while(sb.length() > 0)
{
if (stack.size() < 3)
{
stack.push(sb.charAt(0));
sb = sb.deleteCharAt(0);
}
else
}
}
}
// you can also use imports, for example:
import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public String solution(String s) {
// write your code in Java SE 8
Deque<Character> stack = new ArrayDeque<>();
StringBuilder sb = new StringBuilder(s);
char second = 'z', first = 'z';
while(sb.length() > 0)
{
if (stack.size() < 3)
{
stack.push(sb.charAt(0));
sb = sb.deleteCharAt(0);
}
else
{
char second = stack.pop();
char first = stack.pop();
}
}
}
}
// you can also use imports, for example:
import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public String solution(String s) {
// write your code in Java SE 8
Deque<Character> stack = new ArrayDeque<>();
StringBuilder sb = new StringBuilder(s);
char second = 'z', first = 'z';
while(sb.length() > 0)
{
if (stack.size() < 2)
{
stack.push(sb.charAt(0));
sb = sb.deleteCharAt(0);
}
else
{
char second = stack.pop();
char first = stack.pop();
}
}
}
}
// you can also use imports, for example:
import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public String solution(String s) {
// write your code in Java SE 8
Deque<Character> stack = new ArrayDeque<>();
StringBuilder sb = new StringBuilder(s);
char second = 'z', first = 'z';
while(sb.length() > 0)
{
if (stack.size() < 2)
{
stack.push(sb.charAt(0));
sb = sb.deleteCharAt(0);
}
else
{
char second = stack.pop();
char first = stack.pop();
char third = sb
}
}
}
}
// you can also use imports, for example:
import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public String solution(String s) {
// write your code in Java SE 8
Deque<Character> stack = new ArrayDeque<>();
StringBuilder sb = new StringBuilder(s);
char second = 'z', first = 'z';
while(sb.length() > 0)
{
if (stack.size() < 2)
{
stack.push(sb.charAt(0));
sb = sb.deleteCharAt(0);
}
else
{
char second = stack.pop();
char first = stack.pop();
char third = sb.charAt(0);
if (first == 'a' && second == 'b' && third == 'b')
{
}
}
}
}
}
// you can also use imports, for example:
import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public String solution(String s) {
// write your code in Java SE 8
Deque<Character> stack = new ArrayDeque<>();
StringBuilder sb = new StringBuilder(s);
char second = 'z', first = 'z';
while(sb.length() > 0)
{
if (stack.size() < 2)
{
stack.push(sb.charAt(0));
sb = sb.deleteCharAt(0);
}
else
{
char second = stack.pop();
char first = stack.pop();
char third = sb.charAt(0);
if (first == 'a' && second == 'b' && third == 'b')
{
}
else
{
}
}
}
}
}
// you can also use imports, for example:
import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public String solution(String s) {
// write your code in Java SE 8
Deque<Character> stack = new ArrayDeque<>();
StringBuilder sb = new StringBuilder(s);
char second = 'z', first = 'z';
while(sb.length() > 0)
{
if (stack.size() < 2)
{
stack.push(sb.charAt(0));
sb = sb.deleteCharAt(0);
}
else
{
char second = stack.pop();
char first = stack.pop();
char third = sb.charAt(0);
if (first == 'a' && second == 'b' && third == 'b')
{
}
else
{
stack.push(sb.charAt(0));
sb = sb.deleteCharAt(0);
}
}
}
}
}
// you can also use imports, for example:
import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public String solution(String s) {
// write your code in Java SE 8
Deque<Character> stack = new ArrayDeque<>();
StringBuilder sb = new StringBuilder(s);
char second = 'z', first = 'z';
while(sb.length() > 0)
{
if (stack.size() < 2)
{
stack.push(sb.charAt(0));
sb = sb.deleteCharAt(0);
}
else
{
char second = stack.pop();
char first = stack.pop();
char third = sb.charAt(0);
if (first == 'a' && second == 'b' && third == 'b')
{
sb.append('b');
sb.append('a');
}
else
{
stack.push(sb.charAt(0));
sb = sb.deleteCharAt(0);
}
}
}
}
}
// you can also use imports, for example:
import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public String solution(String s) {
// write your code in Java SE 8
Deque<Character> stack = new ArrayDeque<>();
StringBuilder sb = new StringBuilder(s);
char second = 'z', first = 'z';
while(sb.length() > 0)
{
if (stack.size() < 2)
{
stack.push(sb.charAt(0));
sb = sb.deleteCharAt(0);
}
else
{
char second = stack.pop();
char first = stack.pop();
char third = sb.charAt(0);
if (first == 'a' && second == 'b' && third == 'b')
{
sb.append();
sb.append('a');
}
else
{
stack.push(sb.charAt(0));
sb = sb.deleteCharAt(0);
}
}
}
}
}
// you can also use imports, for example:
import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public String solution(String s) {
// write your code in Java SE 8
Deque<Character> stack = new ArrayDeque<>();
StringBuilder sb = new StringBuilder(s);
char second = 'z', first = 'z';
while(sb.length() > 0)
{
if (stack.size() < 2)
{
stack.push(sb.charAt(0));
sb = sb.deleteCharAt(0);
}
else
{
char second = stack.pop();
char first = stack.pop();
char third = sb.charAt(0);
if (first == 'a' && second == 'b' && third == 'b')
{
sb.insert(0, 'b');
sb.append('a');
}
else
{
stack.push(sb.charAt(0));
sb = sb.deleteCharAt(0);
}
}
}
}
}
// you can also use imports, for example:
import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public String solution(String s) {
// write your code in Java SE 8
Deque<Character> stack = new ArrayDeque<>();
StringBuilder sb = new StringBuilder(s);
char second = 'z', first = 'z';
while(sb.length() > 0)
{
if (stack.size() < 2)
{
stack.push(sb.charAt(0));
sb = sb.deleteCharAt(0);
}
else
{
char second = stack.pop();
char first = stack.pop();
char third = sb.charAt(0);
if (first == 'a' && second == 'b' && third == 'b')
{
}
else
{
stack.push(sb.charAt(0));
sb = sb.deleteCharAt(0);
}
}
}
}
}
// you can also use imports, for example:
import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public String solution(String s) {
// write your code in Java SE 8
Deque<Character> stack = new ArrayDeque<>();
StringBuilder sb = new StringBuilder(s);
char second = 'z', first = 'z';
while(sb.length() > 0)
{
if (stack.size() < 2)
{
stack.push(sb.charAt(0));
sb = sb.deleteCharAt(0);
}
else
{
char second = stack.pop();
char first = stack.pop();
char third = sb.charAt(0);
if (first == 'a' && second == 'b' && third == 'b')
{
sb.deleteCharAt(0);
String newString = "baa" + sb.toString();
sb = ne
}
else
{
stack.push(sb.charAt(0));
sb = sb.deleteCharAt(0);
}
}
}
}
}
// you can also use imports, for example:
import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public String solution(String s) {
// write your code in Java SE 8
Deque<Character> stack = new ArrayDeque<>();
StringBuilder sb = new StringBuilder(s);
char second = 'z', first = 'z';
while(sb.length() > 0)
{
if (stack.size() < 2)
{
stack.push(sb.charAt(0));
sb = sb.deleteCharAt(0);
}
else
{
char second = stack.pop();
char first = stack.pop();
char third = sb.charAt(0);
if (first == 'a' && second == 'b' && third == 'b')
{
sb.deleteCharAt(0);
String newString = "baa" + sb.toString();
sb = new StringBuilder(newString);
}
else
{
stack.push(sb.charAt(0));
sb = sb.deleteCharAt(0);
}
}
}
}
}
// you can also use imports, for example:
import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public String solution(String s) {
// write your code in Java SE 8
Deque<Character> stack = new ArrayDeque<>();
StringBuilder sb = new StringBuilder(s);
char second = 'z', first = 'z';
while(sb.length() > 0)
{
if (stack.size() < 2)
{
stack.push(sb.charAt(0));
sb = sb.deleteCharAt(0);
}
else
{
second = stack.pop();
first = stack.pop();
char third = sb.charAt(0);
if (first == 'a' && second == 'b' && third == 'b')
{
sb.deleteCharAt(0);
String newString = "baa" + sb.toString();
sb = new StringBuilder(newString);
}
else
{
stack.push(sb.charAt(0));
sb = sb.deleteCharAt(0);
}
}
}
}
}
// you can also use imports, for example:
import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public String solution(String s) {
// write your code in Java SE 8
Deque<Character> stack = new ArrayDeque<>();
String s = "abababb";
StringBuilder sb = new StringBuilder(s);
char second = 'z', first = 'z';
while(sb.length() > 0)
{
if (stack.size() < 2)
{
stack.push(sb.charAt(0));
sb = sb.deleteCharAt(0);
}
else
{
second = stack.pop();
first = stack.pop();
char third = sb.charAt(0);
if (first == 'a' && second == 'b' && third == 'b')
{
sb.deleteCharAt(0);
String newString = "baa" + sb.toString();
sb = new StringBuilder(newString);
}
else
{
stack.push(first);
stack.push(second);
stack.push(sb.charAt(0));
sb = sb.deleteCharAt(0);
}
}
}
for(char c: stack)
{
sb.append(c);
}
System.out.println(sb.reverse().toString());
}
}
// you can also use imports, for example:
import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public String solution(String s) {
// write your code in Java SE 8
Deque<Character> stack = new ArrayDeque<>();
StringBuilder sb = new StringBuilder(s);
char second = 'z', first = 'z';
while(sb.length() > 0)
{
if (stack.size() < 2)
{
stack.push(sb.charAt(0));
sb = sb.deleteCharAt(0);
}
else
{
second = stack.pop();
first = stack.pop();
char third = sb.charAt(0);
if (first == 'a' && second == 'b' && third == 'b')
{
sb.deleteCharAt(0);
String newString = "baa" + sb.toString();
sb = new StringBuilder(newString);
}
else
{
stack.push(first);
stack.push(second);
stack.push(sb.charAt(0));
sb = sb.deleteCharAt(0);
}
}
}
for(char c: stack)
{
sb.append(c);
}
System.out.println(sb.reverse().toString());
}
}
// you can also use imports, for example:
import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public String solution(String s) {
// write your code in Java SE 8
Deque<Character> stack = new ArrayDeque<>();
StringBuilder sb = new StringBuilder(s);
char second = 'z', first = 'z';
while(sb.length() > 0)
{
if (stack.size() < 2)
{
stack.push(sb.charAt(0));
sb = sb.deleteCharAt(0);
}
else
{
second = stack.pop();
first = stack.pop();
char third = sb.charAt(0);
if (first == 'a' && second == 'b' && third == 'b')
{
sb.deleteCharAt(0);
String newString = "baa" + sb.toString();
sb = new StringBuilder(newString);
}
else
{
stack.push(first);
stack.push(second);
stack.push(sb.charAt(0));
sb = sb.deleteCharAt(0);
}
}
}
for(char c: stack)
{
sb.append(c);
}
return sb.reverse().toString();
}
}
// you can also use imports, for example:
import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public String solution(String s) {
// write your code in Java SE 8
Deque<Character> stack = new ArrayDeque<>();
StringBuilder sb = new StringBuilder(s);
char second = 'z', first = 'z';
while(sb.length() > 0)
{
if (stack.size() < 2)
{
stack.push(sb.charAt(0));
sb = sb.deleteCharAt(0);
}
else
{
second = stack.pop();
first = stack.pop();
char third = sb.charAt(0);
if (first == 'a' && second == 'b' && third == 'b')
{
sb.deleteCharAt(0);
String newString = "baa" + sb.toString();
sb = new StringBuilder(newString);
}
else
{
stack.push(first);
stack.push(second);
stack.push(sb.charAt(0));
sb = sb.deleteCharAt(0);
}
}
}
for(char c: stack)
{
sb.append(c);
}
return sb.reverse().toString();
}
}
// you can also use imports, for example:
import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public String solution(String s) {
// write your code in Java SE 8
Deque<Character> stack = new ArrayDeque<>();
StringBuilder sb = new StringBuilder(s);
char second = 'z', first = 'z';
while(sb.length() > 0)
{
if (stack.size() < 2)
{
stack.push(sb.charAt(0));
sb = sb.deleteCharAt(0);
}
else
{
second = stack.pop();
first = stack.pop();
char third = sb.charAt(0);
if (first == 'a' && second == 'b' && third == 'b')
{
sb.deleteCharAt(0);
String newString = "baa" + sb.toString();
sb = new StringBuilder(newString);
}
else
{
stack.push(first);
stack.push(second);
stack.push(sb.charAt(0));
sb = sb.deleteCharAt(0);
}
}
}
for(char c: stack)
{
sb.append(c);
}
return sb.reverse().toString();
}
}
// you can also use imports, for example:
import java.util.*;
// you can write to stdout for debugging purposes, e.g.
// System.out.println("this is a debug message");
class Solution {
public String solution(String s) {
// write your code in Java SE 8
Deque<Character> stack = new ArrayDeque<>();
StringBuilder sb = new StringBuilder(s);
char second = 'z', first = 'z';
while(sb.length() > 0)
{
if (stack.size() < 2)
{
stack.push(sb.charAt(0));
sb = sb.deleteCharAt(0);
}
else
{
second = stack.pop();
first = stack.pop();
char third = sb.charAt(0);
if (first == 'a' && second == 'b' && third == 'b')
{
sb.deleteCharAt(0);
String newString = "baa" + sb.toString();
sb = new StringBuilder(newString);
}
else
{
stack.push(first);
stack.push(second);
stack.push(sb.charAt(0));
sb = sb.deleteCharAt(0);
}
}
}
for(char c: stack)
{
sb.append(c);
}
return sb.reverse().toString();
}
}
The following issues have been detected: timeout errors.
Simple big test. N = 100,000. Score x 3.
running time: 0.884 sec., time limit: 0.256 sec.
Random big string. N = 100,000.
running time: 3.400 sec., time limit: 0.304 sec.