String Reduction Coderbyte Solution

0

Have the function StringReduction(str) take the str parameter being passed and return the smallest number you can get through the following reduction method. The method is: Only the letters a, b, and c will be given in str and you must take two different adjacent characters and replace it with the third. 


For example:  
"ac" can be replaced with "b" but "aa" cannot be replaced with anything. This method is done repeatedly until the string cannot be further reduced, and the length of the resulting string is to be outputted. For example: if str is "cab", "ca" can be reduced to "b" and you get "bb" (you can also reduce it to "cc"). The reduction is done so the output should be 2. If str is "bcab", "bc" reduces to "a", so you have "aab", then "ab" reduces to "c", and the final string "ac" is reduced to "b" so the output should be 1.

String reduction coderbyte java


import java.util.Scanner;


public class StringReduction {
int StringReduction(String str) {
if (str.length() <= 1) return str.length();

String result = "";
for (int i = 0; i < str.length(); i++) {
if (i == str.length() - 1 && str.charAt(i) == str.charAt(i - 1)) {
result += String.valueOf(str.charAt(i));
continue;
}

if (str.charAt(i) != str.charAt(i + 1)) {
result += String.valueOf(reduct(str.charAt(i), str.charAt(i + 1)));
if (i < str.length() - 1) {
i += 1;
result += str.substring(i + 1);
}

return StringReduction(result);
} else {
result += String.valueOf(str.charAt(i));
}
}
return result.length();
}

char reduct(char x, char y) {
if (x == 'b' && y == 'c') {
return 'a';
} else if (x == 'c' && y == 'b') {
return 'a';
} else if (x == 'a' && y == 'c') {
return 'b';
} else if (x == 'c' && y == 'a') {
return 'b';
}
return 'c';
}

public static void main (String[] args) {
// keep this function call here
Scanner s = new Scanner(System.in);
StringReduction c = new StringReduction();
System.out.print(c.StringReduction(s.nextLine()));
}
}

String reduction coderbyte Python


def StringReduction(str):
str = list(str)
cSet = set(['a','b','c'])
repeat = True
while repeat:
i = 0
repeat = False
while i < len(str)-1:
if str[i] != str[i+1]:
str[i:i+2] = list(cSet-set([str[i],str[i+1]]))
repeat = True
else:
i += 1
return len(str)
# keep this function call here
# to see how to enter arguments in Python scroll down
print StringReduction(raw_input())

String reduction coderbyte JavaScript


function StringReduction(str) {
// Set initial counter variable to 0
var i = 0;
// Split the string into an array of letters
var letters = str.split();
// Start do loop.
do {
// If the first and second letter in the array match any of the pairs
if ((letters[i] == "a" && letters[i + 1] == "b") || (letters[i] == "b" && letters[i + 1] == "a")) {
// Remove the second letter
letters.splice(i + 1, 1);
// Transform the first letter
letters[i] = "c";
// Set the counter back to 0 so we can start at the beginning again
i = 0;
} else if ((letters[i] == "b" && letters[i + 1] == "c") || (letters[i] == "c" && letters[i + 1] == "b")) {
letters.splice(i + 1, 1);
letters[i] = "a";
i = 0;
} else if ((letters[i] == "c" && letters[i + 1] == "a") || (letters[i] == "a" && letters[i + 1] == "c")) {
letters.splice(i + 1, 1);
letters[i] = "b";
i = 0;
// If no conditions are met, incremenet the counter
} else {
i++;
}
} while (i < letters.length);
// Return the length of the transformed string
return letters.length;
}

console.log(StringReduction("baccabcbc"));

Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.
Post a Comment (0)
Our website uses cookies to enhance your experience. Learn More
Accept !