<p>There is a strange printer with the following two special properties:</p>

<ul>
	<li>The printer can only print a sequence of <strong>the same character</strong> each time.</li>
	<li>At each turn, the printer can print new characters starting from and ending at any place and will cover the original existing characters.</li>
</ul>

<p>Given a string <code>s</code>, return <em>the minimum number of turns the printer needed to print it</em>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>

<pre>
<strong>Input:</strong> s = &quot;aaabbb&quot;
<strong>Output:</strong> 2
<strong>Explanation:</strong> Print &quot;aaa&quot; first and then print &quot;bbb&quot;.
</pre>

<p><strong class="example">Example 2:</strong></p>

<pre>
<strong>Input:</strong> s = &quot;aba&quot;
<strong>Output:</strong> 2
<strong>Explanation:</strong> Print &quot;aaa&quot; first and then print &quot;b&quot; from the second place of the string, which will cover the existing character &#39;a&#39;.
</pre>

<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= s.length &lt;= 100</code></li>
	<li><code>s</code> consists of lowercase English letters.</li>
</ul>

class Solution {
public:

    unordered_map<string, int> memo;

    string createKey(int start, int end) {
        return to_string(start) + "." + to_string(end);
    }

    int turns(int start, int end, string& s) {
        const auto key = createKey(start, end);
        if (memo.find(key) != memo.end()) {
            return memo[key];
        }

        // Base case
        // If start is more than end that s is empty
        // We do not do any other turns as we sum up
        // things when we call turns()
        if (start > end) {
            return 0;
        }

        // We initialise the worst case which is current character
        // And then we add the rest of the string
        int curTurns = 1 + turns(start+1, end, s);

        for (int i = start+1; i<=end; i++) {
            if (s[i] == s[start]) {
                int turnsLeft = turns(start, i-1, s);
                int turnsRight = turns(i+1, end, s);
                curTurns = min(curTurns, turnsLeft+turnsRight);
            }
        }
        return memo[key] = curTurns;
    }

    string removeDuplicates(std::string str) {
        if (str.empty()) {
            return str; 
        }
        std::string outString = "";

        for (char c : str) {
            if (outString.empty() || outString.back() != c) {
                outString += c;
            }
        }
        return outString; 
    }

    int strangePrinter(string s) {
        std::cout << removeDuplicates(s) << endl;
        // aba

        int n = s.length();

        int output = turns(0, n-1, s);



        return output;
    }
};



// s = "cabad"
// Two possible ways to split this string are:
// 1. "c" + "aba" + "d"
// 2. "cab" + "ad"
// In option (1), the "c" and "d" each take 1 turn to print, while "aba" takes 2 turns (as previously explained), resulting in a total of 4 turns.
// In option (2), "cab" takes 3 turns, and "ad" takes 2 turns, totaling 5 turns.
// Thus, the recursive function will prefer the first option.
38277