-
Notifications
You must be signed in to change notification settings - Fork 33
/
Copy path19_StringReduction.cpp
99 lines (90 loc) · 2.74 KB
/
19_StringReduction.cpp
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
// For this challenge you will manipulate a string of characters using a simple reduction method.
// 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.
#include <iostream>
#include <string>
using namespace std;
// Function to reduce the string
string reduce(string word, int index, char letter)
{
word[index] = letter;
word.erase(word.begin()+(index + 1));
return word;
}
int StringReduction(string str) {
bool swap;
int index = 0;
do
{
while (index < str.length() - 1)
{
swap = false;
// Checking if the characters are adjacent
if (str[index] == str[index + 1])
{
index++;
}
else
{
// If Adjacent replace it with the third character
if (str[index] == 'a')
{
if (str[index + 1] == 'b')
{
str = reduce(str, index, 'c');
index = index + 1;
}
else if (str[index + 1] == 'c')
{
str = reduce(str, index, 'b');
index = index + 1;
}
swap = true;
}
else if (str[index] == 'b')
{
if (str[index + 1] == 'a')
{
str = reduce(str, index, 'c');
index = index + 1;
}
else if (str[index + 1] == 'c')
{
str = reduce(str, index, 'a');
index = index + 1;
}
swap = true;
}
else if (str[index] == 'c')
{
if (str[index + 1] == 'a')
{
str = reduce(str, index, 'b');
index = index + 1;
}
else if (str[index + 1] == 'b')
{
str = reduce(str, index, 'a');
index = index + 1;
}
swap = true;
}
}
}
// Safeguard that if the string is reduced to 1 character end the loop
if (str.length() == 1)
{
swap = false;
}
// Reset the index back, to traverse the new edited string
index = 0;
} while (swap);
return str.length();
}
int main() {
// keep this function call here
cout << StringReduction("abcabc") << endl; // 2
cout << StringReduction("cccc") << endl; // 4
cout << StringReduction("cab") << endl; // 2
cout << StringReduction("bcab") << endl; // 1
return 0;
}