1+ /*
2+
3+ Using the JavaScript language, have the function StringReduction(str) take the str parameter
4+ being passed and return the smallest number you can get through the following reduction method.
5+ The method is: Only the letters a, b, and c will be given in str and you must take two different
6+ adjacent characters and replace it with the third. For example "ac" can be replaced with "b" but
7+ "aa" cannot be replaced with anything. This method is done repeatedly until the string cannot be
8+ further reduced, and the length of the resulting string is to be outputted. For example: if str
9+ is "cab", "ca" can be reduced to "b" and you get "bb" (you can also reduce it to "cc"). The
10+ reduction is done so the output should be 2. If str is "bcab", "bc" reduces to "a", so you have
11+ "aab", then "ab" reduces to "c", and the final string "ac" is reduced to "b" so the output should be 1.
12+ */
13+
14+ function StringReduction ( str ) {
15+ var remainingString = str ;
16+ var newString = "" ;
17+ var shortestStr = str . length ;
18+ var nextString = "" ;
19+ var same = true ;
20+ var newerString = "" ;
21+ var replaceLetters = function ( a , b ) {
22+ debugger ;
23+ //console.log("replacing: " + a + " and " + b);
24+ if ( ( a == "a" && b == "b" ) || ( a == "b" && b == "a" ) ) {
25+ return "c" ;
26+ } else if ( ( a == "b" && b == "c" ) || ( a == "c" && b == "b" ) ) {
27+ return "a" ;
28+ } else if ( ( a == "a" && b == "c" ) || ( a == "c" && b == "a" ) ) {
29+ return "b" ;
30+ } else {
31+ return a + b ;
32+ }
33+ }
34+ var iterateStr = function ( newStr , remainingStr ) {
35+ debugger ;
36+ var wasDiff = false ;
37+ if ( remainingStr . length < 2 ) {
38+ return 1 ;
39+ }
40+ for ( i = 0 ; i < remainingStr . length ; i ++ ) {
41+ //console.log("remainingStr: " + remainingStr);
42+ if ( remainingStr . length < 2 ) {
43+ newStr += remainingStr ;
44+ var nextString = "" ;
45+ nextString += newStr ;
46+ //console.log("nextString: " + nextString);
47+ // return iterateStr(newStr,nextString);
48+ } else {
49+ if ( remainingStr . charAt ( i ) != remainingStr . charAt ( i + 1 ) ) {
50+ wasDiff = true ;
51+ debugger ;
52+ newStr += replaceLetters ( remainingStr . charAt ( i ) , remainingStr . charAt ( i + 1 ) ) ;
53+ i ++ ;
54+ } else if ( remainingStr . charAt ( i ) == remainingStr . charAt ( i + 1 ) ) {
55+ debugger ;
56+ newStr += remainingStr . charAt ( i ) ;
57+ }
58+ }
59+ if ( remainingStr . charAt ( i + 1 ) == undefined ) {
60+ //console.log("calling this!");
61+ newStr += remainingStr . charAt ( i ) ;
62+ if ( ! wasDiff ) {
63+ return newStr . length ;
64+ }
65+ }
66+ //console.log("newStr: " + newStr);
67+ }
68+ var nextString = "" ;
69+ nextString += newStr ;
70+ //console.log("nextString: " + nextString);
71+ if ( nextString == remainingStr ) {
72+ return nextString . length ;
73+ } else if ( nextString . length < 2 ) {
74+ return 1 ;
75+ }
76+ var emptyStr = "" ;
77+ return iterateStr ( emptyStr , nextString ) ;
78+ }
79+ return iterateStr ( newString , remainingString ) ;
80+ }
81+
82+ //StringReduction("abcabc"); // output = 2;
83+ StringReduction ( "cccc" ) ; // output = 4;
84+ //StringReduction("ababccbbcabab"); //
0 commit comments