A string S containing only the letters "A", "B" and "C" is given. The string can be transformed by removing one occurrence of "AA", "BB" or "CC".
Transformation of the string is the process of removing letters from it, based on the rules described above. As long as at least one rule can be applied, the process should be repeated. If more than one rule can be used, any one of them could be chosen.
Write a function:
class Solution { public String solution(String S); }
that, given a string S consisting of N characters, returns any string that can result from a sequence of transformations as described above.
For example, given string S = "ACCAABBC" the function may return "AC", because one of the possible sequences of transformations is as follows:
Also, given string S = "ABCBBCBA" the function may return "", because one possible sequence of transformations is:
Finally, for string S = "BABABA" the function must return "BABABA", because no rules can be applied to string S.
Write an efficient algorithm for the following assumptions:
the length of string S is within the range [0..50,000];
string S consists only of the following characters: "A", "B" and/or "C".
string getReduced(string prefix, string suffix)
{
bool fix = true;
while(fix)
{
if (string.IsNullOrWhitespace(suffix))
{fix = false; break;}
int I = 0;
while (i+1 < suffix.length && suffix[i] == suffix[i+1])
{
suffix = suffix.substring(I+2);
Fix = true;
}
If (fix) continue;
While( prefix.length > 0 && prefix.last() == suffix.first())
{
prefix = prefix.length-1;
suffix = suffix.substring(1, suffix.length-1);
. Fix = true;
}
If(fix) continue;
Prefix = prefix + suffix.first();
Suffix = suffix.substring(1, suffix.length-1);
Fix = true;
}
}
return prefix+suffix;
}
No comments:
Post a Comment