-
Notifications
You must be signed in to change notification settings - Fork 1
/
StringReduction.js
84 lines (81 loc) · 2.79 KB
/
StringReduction.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/*
Using the JavaScript language, 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.
*/
function StringReduction(str) {
var remainingString = str;
var newString = "";
var shortestStr = str.length;
var nextString = "";
var same = true;
var newerString = "";
var replaceLetters = function(a,b){
debugger;
//console.log("replacing: " + a + " and " + b);
if ((a == "a" && b == "b") || (a == "b" && b == "a")){
return "c";
} else if ((a == "b" && b == "c")||(a == "c" && b == "b")) {
return "a";
} else if((a == "a" && b == "c")||(a == "c" && b == "a")) {
return "b";
} else {
return a+b;
}
}
var iterateStr = function(newStr, remainingStr){
debugger;
var wasDiff = false;
if (remainingStr.length < 2){
return 1;
}
for (i = 0; i < remainingStr.length; i++){
//console.log("remainingStr: " + remainingStr);
if (remainingStr.length < 2){
newStr += remainingStr;
var nextString = "";
nextString += newStr;
//console.log("nextString: " + nextString);
// return iterateStr(newStr,nextString);
} else {
if (remainingStr.charAt(i) != remainingStr.charAt(i+1)){
wasDiff = true;
debugger;
newStr += replaceLetters(remainingStr.charAt(i), remainingStr.charAt(i+1));
i++;
} else if (remainingStr.charAt(i) == remainingStr.charAt(i+1)){
debugger;
newStr += remainingStr.charAt(i);
}
}
if (remainingStr.charAt(i+1) == undefined){
//console.log("calling this!");
newStr += remainingStr.charAt(i);
if (!wasDiff) {
return newStr.length;
}
}
//console.log("newStr: " + newStr);
}
var nextString = "";
nextString += newStr;
//console.log("nextString: " + nextString);
if (nextString == remainingStr){
return nextString.length;
} else if (nextString.length < 2){
return 1;
}
var emptyStr = "";
return iterateStr(emptyStr,nextString);
}
return iterateStr(newString, remainingString);
}
//StringReduction("abcabc"); // output = 2;
StringReduction("cccc"); // output = 4;
//StringReduction("ababccbbcabab"); //