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) {
StringBuilder r=new StringBuilder();
return r.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) {
StringBuilder r=new StringBuilder();
return r.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) {
StringBuilder r=new StringBuilder();
int abCount=0;
for(int i=0; i< S.length(); i++){
}
return r.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) {
StringBuilder r=new StringBuilder();
int abCount=0;
for(int i=0; i< S.length(); i++){
char c=S.charAt(i);
}
return r.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) {
StringBuilder r=new StringBuilder();
int abCount=0;
for(int i=0; i< S.length(); i++){
char c=S.charAt(i);
}
return r.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) {
StringBuilder r=new StringBuilder();
int abCount=0;
for(int i=0; i< S.length(); i++){
char c=S.charAt(i);
if(c=='a'){
}else{
}
}
return r.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) {
StringBuilder r=new StringBuilder();
int abCount=0;
boolean hasA=false;
for(int i=0; i< S.length(); i++){
char c=S.charAt(i);
if(c=='a'){
}else{
}
}
return r.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) {
StringBuilder r=new StringBuilder();
int abCount=0;
boolean hasA=false;
for(int i=0; i< S.length(); i++){
char c=S.charAt(i);
if(c=='a'){
if(hasA){
//flush abCount,
}
}else{
}
}
return r.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) {
StringBuilder r=new StringBuilder();
int abCount=0;
boolean hasA=false;
for(int i=0; i< S.length(); i++){
char c=S.charAt(i);
if(c=='a'){
if(hasA){
//flush abCount, print a
}else{
}
}else{
}
}
return r.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) {
StringBuilder r=new StringBuilder();
int abCount=0;
boolean hasA=false;
for(int i=0; i< S.length(); i++){
char c=S.charAt(i);
if(c=='a'){
if(hasA){
//flush abCount, print a
}else{
hasA=true;
}
}else{
}
}
return r.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) {
StringBuilder r=new StringBuilder();
int abCount=0;
boolean hasA=false;
for(int i=0; i< S.length(); i++){
char c=S.charAt(i);
if(c=='a'){
if(hasA){
//flush abCount, print a
}else{
hasA=true;
}
}else{
if(hasA){
}else
}
}
return r.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) {
StringBuilder r=new StringBuilder();
int abCount=0;
boolean hasA=false;
for(int i=0; i< S.length(); i++){
char c=S.charAt(i);
if(c=='a'){
if(hasA){
//flush abCount, print a
}else{
hasA=true;
}
}else{
if(hasA){
hasA=
}else{
if(abCount>0){
//transform
}else{
r.append('b');
}
}
}
}
return r.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) {
StringBuilder r=new StringBuilder();
int abCount=0;
boolean hasA=false;
for(int i=0; i< S.length(); i++){
char c=S.charAt(i);
if(c=='a'){
if(hasA){
//flush abCount, print a
}else{
hasA=true;
}
}else{
if(hasA){
hasA=false;
abCount++;
}else{
if(abCount>0){
//transform
}else{
r.append('b');
}
}
}
}
return r.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) {
StringBuilder r=new StringBuilder();
int abCount=0;
boolean hasA=false;
for(int i=0; i< S.length(); i++){
char c=S.charAt(i);
if(c=='a'){
if(hasA){
//flush abCount, print a
abCount=0;
}else{
hasA=true;
}
}else{
if(hasA){
hasA=false;
abCount++;
}else{
if(abCount>0){
//transform
}else{
r.append('b');
}
}
}
}
return r.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) {
StringBuilder r=new StringBuilder();
int abCount=0;
boolean hasA=false;
for(int i=0; i< S.length(); i++){
char c=S.charAt(i);
if(c=='a'){
if(hasA){
//flush abCount, print a
for(int j=0;j<abCount;j ++) r.append("ab");
r.append("a");
abCount=0;
}else{
hasA=true;
}
}else{
if(hasA){
hasA=false;
abCount++;
}else{
if(abCount>0){
//transform
}else{
r.append('b');
}
}
}
}
return r.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) {
StringBuilder r=new StringBuilder();
int abCount=0;
boolean hasA=false;
for(int i=0; i< S.length(); i++){
char c=S.charAt(i);
if(c=='a'){
if(hasA){
for(int j=0;j<abCount;j ++) r.append("ab");
r.append("a");
abCount=0;
}else{
hasA=true;
}
}else{
if(hasA){
hasA=false;
abCount++;
}else{
if(abCount>0){
//transform
}else{
r.append('b');
}
}
}
}
return r.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) {
StringBuilder r=new StringBuilder();
int abCount=0;
boolean hasA=false;
for(int i=0; i< S.length(); i++){
char c=S.charAt(i);
if(c=='a'){
if(hasA){
for(int j=0;j<abCount;j ++) r.append("ab");
r.append("a");
abCount=0;
}else{
hasA=true;
}
}else{
if(hasA){
hasA=false;
abCount++;
}else{
if(abCount>0){
//transform
}else{
r.append('b');
}
}
}
}
return r.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) {
StringBuilder r=new StringBuilder();
int abCount=0;
boolean hasA=false;
for(int i=0; i< S.length(); i++){
char c=S.charAt(i);
if(c=='a'){
if(hasA){
for(int j=0;j<abCount;j ++) r.append("ab");
r.append("a");
abCount=0;
}else{
hasA=true;
}
}else{
if(hasA){
hasA=false;
abCount++;
}else{
if(abCount>0){
//transform
abCount=0;
hasA=true;
}else{
r.append('b');
}
}
}
}
return r.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) {
StringBuilder r=new StringBuilder();
int abCount=0;
boolean hasA=false;
for(int i=0; i< S.length(); i++){
char c=S.charAt(i);
if(c=='a'){
if(hasA){
for(int j=0;j<abCount;j ++) r.append("ab");
r.append("a");
abCount=0;
}else{
hasA=true;
}
}else{
if(hasA){
hasA=false;
abCount++;
}else{
if(abCount>0){
//transform
abCount=0;
hasA=true;
}else{
r.append('b');
}
}
}
}
return r.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) {
StringBuilder r=new StringBuilder();
int abCount=0;
boolean hasA=false;
for(int i=0; i< S.length(); i++){
char c=S.charAt(i);
if(c=='a'){
if(hasA){
for(int j=0;j<abCount;j ++) r.append("ab");
r.append("a");
abCount=0;
}else{
hasA=true;
}
}else{
if(hasA){
hasA=false;
abCount++;
}else{
if(abCount>0){
//transform
r.append("ba");
for(int j=0; j<abCount-1; j++)r
abCount=0;
hasA=true;
}else{
r.append('b');
}
}
}
}
return r.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) {
StringBuilder r=new StringBuilder();
int abCount=0;
boolean hasA=false;
for(int i=0; i< S.length(); i++){
char c=S.charAt(i);
if(c=='a'){
if(hasA){
for(int j=0;j<abCount;j ++) r.append("ab");
r.append("a");
abCount=0;
}else{
hasA=true;
}
}else{
if(hasA){
hasA=false;
abCount++;
}else{
if(abCount>0){
//transform
r.append("ba");
for(int j=0; j<abCount-1; j++)r.append("aa");
abCount=0;
hasA=true;
}else{
r.append('b');
}
}
}
}
return r.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) {
StringBuilder r=new StringBuilder();
int abCount=0;
boolean hasA=false;
for(int i=0; i< S.length(); i++){
char c=S.charAt(i);
if(c=='a'){
if(hasA){
for(int j=0;j<abCount;j ++) r.append("ab");
r.append("a");
abCount=0;
}else{
hasA=true;
}
}else{
if(hasA){
hasA=false;
abCount++;
}else{
if(abCount>0){
//transform
r.append("ba");
for(int j=0; j<abCount-1; j++)r.append("aa");
abCount=0;
hasA=true;
}else{
r.append('b');
}
}
}
}
if()
if(hasA) r.append('a');
return r.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) {
StringBuilder r=new StringBuilder();
int abCount=0;
boolean hasA=false;
for(int i=0; i< S.length(); i++){
char c=S.charAt(i);
if(c=='a'){
if(hasA){
for(int j=0;j<abCount;j ++) r.append("ab");
r.append("a");
abCount=0;
}else{
hasA=true;
}
}else{
if(hasA){
hasA=false;
abCount++;
}else{
if(abCount>0){
//transform
r.append("ba");
for(int j=0; j<abCount-1; j++)r.append("aa");
abCount=0;
hasA=true;
}else{
r.append('b');
}
}
}
}
for(int j=0; j< abCount; j++) r.append("ab");
if(hasA) r.append('a');
return r.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) {
StringBuilder r=new StringBuilder();
int abCount=0;
boolean hasA=false;
for(int i=0; i< S.length(); i++){
char c=S.charAt(i);
if(c=='a'){
if(hasA){
for(int j=0;j<abCount;j ++) r.append("ab");
r.append("a");
abCount=0;
}else{
hasA=true;
}
}else{
if(hasA){
hasA=false;
abCount++;
}else{
if(abCount>0){
//transform
r.append("ba");
for(int j=0; j<abCount-1; j++)r.append("aa");
abCount=0;
hasA=true;
}else{
r.append('b');
}
}
}
}
for(int j=0; j< abCount; j++) r.append("ab");
if(hasA) r.append('a');
return r.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) {
StringBuilder r=new StringBuilder();
int abCount=0;
boolean hasA=false;
for(int i=0; i< S.length(); i++){
char c=S.charAt(i);
if(c=='a'){
if(hasA){
for(int j=0;j<abCount;j ++) r.append("ab");
r.append("a");
abCount=0;
}else hasA=true;
}else{
if(hasA){
hasA=false;
abCount++;
}else{
if(abCount>0){
r.append("ba");
for(int j=0; j<abCount-1; j++)r.append("aa");
abCount=0;
hasA=true;
}else r.append('b');
}
}
}
for(int j=0; j< abCount; j++) r.append("ab");
if(hasA) r.append('a');
return r.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) {
StringBuilder r=new StringBuilder();
int abCount=0;
boolean hasA=false;
for(int i=0; i< S.length(); i++){
char c=S.charAt(i);
if(c=='a'){
if(hasA){
for(int j=0;j<abCount;j ++) r.append("ab");
r.append("a");
abCount=0;
}else hasA=true;
}else{
if(hasA){
hasA=false;
abCount++;
}else{
if(abCount>0){
r.append("ba");
for(int j=0; j<abCount-1; j++)r.append("aa");
abCount=0;
hasA=true;
}else r.append('b');
}
}
}
for(int j=0; j< abCount; j++) r.append("ab");
if(hasA) r.append('a');
return r.toString();
}
}
import java.util.*;
class Solution {
public String solution(String S) {
StringBuilder r=new StringBuilder();
int abCount=0;
boolean hasA=false;
for(int i=0; i< S.length(); i++){
char c=S.charAt(i);
if(c=='a'){
if(hasA){
for(int j=0;j<abCount;j ++) r.append("ab");
r.append("a");
abCount=0;
}else hasA=true;
}else{
if(hasA){
hasA=false;
abCount++;
}else{
if(abCount>0){
r.append("ba");
for(int j=0; j<abCount-1; j++)r.append("aa");
abCount=0;
hasA=true;
}else r.append('b');
}
}
}
for(int j=0; j< abCount; j++) r.append("ab");
if(hasA) r.append('a');
return r.toString();
}
}
//import java.util.*;
class Solution {
public String solution(String S) {
StringBuilder r=new StringBuilder();
int abCount=0;
boolean hasA=false;
for(int i=0; i< S.length(); i++){
char c=S.charAt(i);
if(c=='a'){
if(hasA){
for(int j=0;j<abCount;j ++) r.append("ab");
r.append("a");
abCount=0;
}else hasA=true;
}else{
if(hasA){
hasA=false;
abCount++;
}else{
if(abCount>0){
r.append("ba");
for(int j=0; j<abCount-1; j++)r.append("aa");
abCount=0;
hasA=true;
}else r.append('b');
}
}
}
for(int j=0; j< abCount; j++) r.append("ab");
if(hasA) r.append('a');
return r.toString();
}
}
class Solution {
public String solution(String S) {
StringBuilder r=new StringBuilder();
int abCount=0;
boolean hasA=false;
for(int i=0; i< S.length(); i++){
char c=S.charAt(i);
if(c=='a'){
if(hasA){
for(int j=0;j<abCount;j ++) r.append("ab");
r.append("a");
abCount=0;
}else hasA=true;
}else{
if(hasA){
hasA=false;
abCount++;
}else{
if(abCount>0){
r.append("ba");
for(int j=0; j<abCount-1; j++)r.append("aa");
abCount=0;
hasA=true;
}else r.append('b');
}
}
}
for(int j=0; j< abCount; j++) r.append("ab");
if(hasA) r.append('a');
return r.toString();
}
}
class Solution {
public String solution(String S) {
StringBuilder r=new StringBuilder();
int abCount=0;
boolean hasA=false;
for(int i=0; i< S.length(); i++){
char c=S.charAt(i);
if(c=='a'){
if(hasA){
for(int j=0;j<abCount;j ++) r.append("ab");
r.append("a");
abCount=0;
}else hasA=true;
}else{
if(hasA){
hasA=false;
abCount++;
}else{
if(abCount>0){
r.append("ba");
for(int j=0; j<abCount-1; j++)r.append("aa");
abCount=0;
hasA=true;
}else r.append('b');
}
}
}
for(int j=0; j< abCount; j++) r.append("ab");
if(hasA) r.append('a');
return r.toString();
}
}
class Solution {
public String solution(String S) {
StringBuilder r=new StringBuilder();
int abCount=0;
boolean hasA=false;
for(int i=0; i< S.length(); i++){
char c=S.charAt(i);
if(c=='a'){
if(hasA){
for(int j=0;j<abCount;j ++) r.append("ab");
r.append("a");
abCount=0;
}else hasA=true;
}else{
if(hasA){
hasA=false;
abCount++;
}else{
if(abCount>0){
r.append("ba");
for(int j=0; j<abCount-1; j++)r.append("aa");
abCount=0;
hasA=true;
}else r.append('b');
}
}
}
for(int j=0; j< abCount; j++) r.append("ab");
if(hasA) r.append('a');
return r.toString();
}
}
The solution obtained perfect score.