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) {
// A - true, B - false
boolean[] encoded = encode(S);
for (int i = encoded.length-3; i >= 0; i--) {
// System.out.println("len: "+encoded.length+", i: "+i);
if (encoded[i] && !encoded[i+1] && !encoded[i+2]) {
// do flip
encoded[i] = false;
encoded[i+1] = true;
encoded[i+2] = true;
// check if we didn't create new valid pattern behind by the flip
if (i+3 < encoded.length && !encoded[i+3] && !encoded[i+4])
i = i+3;
}
// printEncoded(encoded);
}
return decode(encoded);
}
public void printEncoded(boolean[] encoded) {
for (int i = 0; i < encoded.length; i++)
if (encoded[i])
System.out.print("a");
else
System.out.print("b");
System.out.println("");
}
public boolean[] encode(String s) {
// A - true, B - false
boolean[] encoded = new boolean[s.length()];
for (int i = 0; i < s.length(); i++)
if (s.charAt(i) == 'a')
encoded[i] = true;
return encoded;
}
public String decode(boolean[] s) {
// A - true, B - false
char[] text = new char[s.length];
for (int i = 0; i < s.length; i++)
if (s[i])
text[i] = 'a';
else
text[i] = 'b';
return new String(text);
}
}
class Solution {
public String solution(String S) {
// A - true, B - false
boolean[] encoded = encode(S);
for (int i = encoded.length-3; i >= 0; i--) {
// System.out.println("len: "+encoded.length+", i: "+i);
if (encoded[i] && !encoded[i+1] && !encoded[i+2]) {
// do flip
encoded[i] = false;
encoded[i+1] = true;
encoded[i+2] = true;
// check if we didn't create new valid pattern behind by the flip
if (i+3 < encoded.length && !encoded[i+3] && !encoded[i+4])
i = i+3;
}
// printEncoded(encoded);
}
return decode(encoded);
}
public void printEncoded(boolean[] encoded) {
for (int i = 0; i < encoded.length; i++)
if (encoded[i])
System.out.print("a");
else
System.out.print("b");
System.out.println("");
}
public boolean[] encode(String s) {
// A - true, B - false
boolean[] encoded = new boolean[s.length()];
for (int i = 0; i < s.length(); i++)
if (s.charAt(i) == 'a')
encoded[i] = true;
return encoded;
}
public String decode(boolean[] s) {
// A - true, B - false
char[] text = new char[s.length];
for (int i = 0; i < s.length; i++)
if (s[i])
text[i] = 'a';
else
text[i] = 'b';
return new String(text);
}
}
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 6 at Solution.solution(Solution.java:16) at Exec.run(exec.java:48) at Exec.main(exec.java:34)
function result: 'baa'
['abbbbb']
['abb']
class Solution {
public String solution(String S) {
// A - true, B - false
boolean[] encoded = encode(S);
for (int i = encoded.length-3; i >= 0; i--) {
System.out.println("len: "+encoded.length+", i: "+i);
if (encoded[i] && !encoded[i+1] && !encoded[i+2]) {
// do flip
encoded[i] = false;
encoded[i+1] = true;
encoded[i+2] = true;
// check if we didn't create new valid pattern behind by the flip
if (i+3 < encoded.length && !encoded[i+3] && !encoded[i+4])
i = i+3;
}
printEncoded(encoded);
}
return decode(encoded);
}
public void printEncoded(boolean[] encoded) {
for (int i = 0; i < encoded.length; i++)
if (encoded[i])
System.out.print("a");
else
System.out.print("b");
System.out.println("");
}
public boolean[] encode(String s) {
// A - true, B - false
boolean[] encoded = new boolean[s.length()];
for (int i = 0; i < s.length(); i++)
if (s.charAt(i) == 'a')
encoded[i] = true;
return encoded;
}
public String decode(boolean[] s) {
// A - true, B - false
char[] text = new char[s.length];
for (int i = 0; i < s.length; i++)
if (s[i])
text[i] = 'a';
else
text[i] = 'b';
return new String(text);
}
}
class Solution {
public String solution(String S) {
// A - true, B - false
boolean[] encoded = encode(S);
for (int i = encoded.length-3; i >= 0; i--) {
System.out.println("len: "+encoded.length+", i: "+i);
if (encoded[i] && !encoded[i+1] && !encoded[i+2]) {
// do flip
encoded[i] = false;
encoded[i+1] = true;
encoded[i+2] = true;
// check if we didn't create new valid pattern behind by the flip
if (i+3 < encoded.length && !encoded[i+3] && !encoded[i+4])
i = i+3;
}
printEncoded(encoded);
}
return decode(encoded);
}
public void printEncoded(boolean[] encoded) {
for (int i = 0; i < encoded.length; i++)
if (encoded[i])
System.out.print("a");
else
System.out.print("b");
System.out.println("");
}
public boolean[] encode(String s) {
// A - true, B - false
boolean[] encoded = new boolean[s.length()];
for (int i = 0; i < s.length(); i++)
if (s.charAt(i) == 'a')
encoded[i] = true;
return encoded;
}
public String decode(boolean[] s) {
// A - true, B - false
char[] text = new char[s.length];
for (int i = 0; i < s.length; i++)
if (s[i])
text[i] = 'a';
else
text[i] = 'b';
return new String(text);
}
}
len: 5, i: 2 abbaa len: 5, i: 1 abbaa len: 5, i: 0 baaaa
len: 7, i: 4 abbbbaa len: 7, i: 3 abbbbaa len: 7, i: 2 abbbbaa len: 7, i: 1 abbbbaa len: 7, i: 0 baabbaa len: 7, i: 2 babaaaa len: 7, i: 1 babaaaa len: 7, i: 0 babaaaa
len: 6, i: 3 aaabab len: 6, i: 2 aaabab len: 6, i: 1 aaabab len: 6, i: 0 aaabab
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 6 at Solution.solution(Solution.java:16) at Exec.run(exec.java:48) at Exec.main(exec.java:34)stdout:
len: 6, i: 3 abbbbb len: 6, i: 2 abbbbb len: 6, i: 1 abbbbb len: 6, i: 0 baabbb len: 6, i: 2
function result: 'baa'
len: 3, i: 0 baa
['abbbbb']
['abb']
class Solution {
public String solution(String S) {
// A - true, B - false
boolean[] encoded = encode(S);
for (int i = encoded.length-3; i >= 0; i--) {
System.out.println("len: "+encoded.length+", i: "+i);
if (encoded[i] && !encoded[i+1] && !encoded[i+2]) {
// do flip
encoded[i] = false;
encoded[i+1] = true;
encoded[i+2] = true;
// check if we didn't create new valid pattern behind by the flip
if (i+4 < encoded.length && !encoded[i+3] && !encoded[i+4])
i = i+3;
}
printEncoded(encoded);
}
return decode(encoded);
}
public void printEncoded(boolean[] encoded) {
for (int i = 0; i < encoded.length; i++)
if (encoded[i])
System.out.print("a");
else
System.out.print("b");
System.out.println("");
}
public boolean[] encode(String s) {
// A - true, B - false
boolean[] encoded = new boolean[s.length()];
for (int i = 0; i < s.length(); i++)
if (s.charAt(i) == 'a')
encoded[i] = true;
return encoded;
}
public String decode(boolean[] s) {
// A - true, B - false
char[] text = new char[s.length];
for (int i = 0; i < s.length; i++)
if (s[i])
text[i] = 'a';
else
text[i] = 'b';
return new String(text);
}
}
class Solution {
public String solution(String S) {
// A - true, B - false
boolean[] encoded = encode(S);
for (int i = encoded.length-3; i >= 0; i--) {
System.out.println("len: "+encoded.length+", i: "+i);
if (encoded[i] && !encoded[i+1] && !encoded[i+2]) {
// do flip
encoded[i] = false;
encoded[i+1] = true;
encoded[i+2] = true;
// check if we didn't create new valid pattern behind by the flip
if (i+4 < encoded.length && !encoded[i+3] && !encoded[i+4])
i = i+3;
}
printEncoded(encoded);
}
return decode(encoded);
}
public void printEncoded(boolean[] encoded) {
for (int i = 0; i < encoded.length; i++)
if (encoded[i])
System.out.print("a");
else
System.out.print("b");
System.out.println("");
}
public boolean[] encode(String s) {
// A - true, B - false
boolean[] encoded = new boolean[s.length()];
for (int i = 0; i < s.length(); i++)
if (s.charAt(i) == 'a')
encoded[i] = true;
return encoded;
}
public String decode(boolean[] s) {
// A - true, B - false
char[] text = new char[s.length];
for (int i = 0; i < s.length; i++)
if (s[i])
text[i] = 'a';
else
text[i] = 'b';
return new String(text);
}
}
len: 5, i: 2 abbaa len: 5, i: 1 abbaa len: 5, i: 0 baaaa
len: 7, i: 4 abbbbaa len: 7, i: 3 abbbbaa len: 7, i: 2 abbbbaa len: 7, i: 1 abbbbaa len: 7, i: 0 baabbaa len: 7, i: 2 babaaaa len: 7, i: 1 babaaaa len: 7, i: 0 babaaaa
len: 6, i: 3 aaabab len: 6, i: 2 aaabab len: 6, i: 1 aaabab len: 6, i: 0 aaabab
function result: 'babaab'
len: 6, i: 3 abbbbb len: 6, i: 2 abbbbb len: 6, i: 1 abbbbb len: 6, i: 0 baabbb len: 6, i: 2 babaab len: 6, i: 1 babaab len: 6, i: 0 babaab
function result: 'baa'
len: 3, i: 0 baa
['abbbbb']
['abb']
class Solution {
public String solution(String S) {
// A - true, B - false
boolean[] encoded = encode(S);
for (int i = encoded.length-3; i >= 0; i--) {
// System.out.println("len: "+encoded.length+", i: "+i);
if (encoded[i] && !encoded[i+1] && !encoded[i+2]) {
// do flip
encoded[i] = false;
encoded[i+1] = true;
encoded[i+2] = true;
// check if we didn't create new valid pattern behind by the flip
if (i+4 < encoded.length && !encoded[i+3] && !encoded[i+4])
i = i+3;
}
// printEncoded(encoded);
}
return decode(encoded);
}
public void printEncoded(boolean[] encoded) {
for (int i = 0; i < encoded.length; i++)
if (encoded[i])
System.out.print("a");
else
System.out.print("b");
System.out.println("");
}
public boolean[] encode(String s) {
// A - true, B - false
boolean[] encoded = new boolean[s.length()];
for (int i = 0; i < s.length(); i++)
if (s.charAt(i) == 'a')
encoded[i] = true;
return encoded;
}
public String decode(boolean[] s) {
// A - true, B - false
char[] text = new char[s.length];
for (int i = 0; i < s.length; i++)
if (s[i])
text[i] = 'a';
else
text[i] = 'b';
return new String(text);
}
}
['abbbbb']
['abb']
class Solution {
public String solution(String S) {
// A - true, B - false
boolean[] encoded = encode(S);
for (int i = encoded.length-3; i >= 0; i--) {
// System.out.println("len: "+encoded.length+", i: "+i);
if (encoded[i] && !encoded[i+1] && !encoded[i+2]) {
// do flip
encoded[i] = false;
encoded[i+1] = true;
encoded[i+2] = true;
// check if we didn't create new valid pattern behind by the flip
if (i+4 < encoded.length && !encoded[i+3] && !encoded[i+4])
i = i+3;
}
// printEncoded(encoded);
}
return decode(encoded);
}
public void printEncoded(boolean[] encoded) {
for (int i = 0; i < encoded.length; i++)
if (encoded[i])
System.out.print("a");
else
System.out.print("b");
System.out.println("");
}
public boolean[] encode(String s) {
// A - true, B - false
boolean[] encoded = new boolean[s.length()];
for (int i = 0; i < s.length(); i++)
if (s.charAt(i) == 'a')
encoded[i] = true;
return encoded;
}
public String decode(boolean[] s) {
// A - true, B - false
char[] text = new char[s.length];
for (int i = 0; i < s.length; i++)
if (s[i])
text[i] = 'a';
else
text[i] = 'b';
return new String(text);
}
}
['abbbbb']
['abb']
class Solution {
public String solution(String S) {
// A - true, B - false
boolean[] encoded = encode(S);
for (int i = encoded.length-3; i >= 0; i--) {
// System.out.println("len: "+encoded.length+", i: "+i);
if (encoded[i] && !encoded[i+1] && !encoded[i+2]) {
// do flip
encoded[i] = false;
encoded[i+1] = true;
encoded[i+2] = true;
// check if we didn't create new valid pattern behind by the flip
if (i+4 < encoded.length && !encoded[i+3] && !encoded[i+4])
i = i+3;
}
// printEncoded(encoded);
}
return decode(encoded);
}
public void printEncoded(boolean[] encoded) {
for (int i = 0; i < encoded.length; i++)
if (encoded[i])
System.out.print("a");
else
System.out.print("b");
System.out.println("");
}
public boolean[] encode(String s) {
// A - true, B - false
boolean[] encoded = new boolean[s.length()];
for (int i = 0; i < s.length(); i++)
if (s.charAt(i) == 'a')
encoded[i] = true;
return encoded;
}
public String decode(boolean[] s) {
// A - true, B - false
char[] text = new char[s.length];
for (int i = 0; i < s.length; i++)
if (s[i])
text[i] = 'a';
else
text[i] = 'b';
return new String(text);
}
}
The solution obtained perfect score.