<p>A password is considered strong if the below conditions are all met:</p>

<ul>
	<li>It has at least <code>6</code> characters and at most <code>20</code> characters.</li>
	<li>It contains at least <strong>one lowercase</strong> letter, at least <strong>one uppercase</strong> letter, and at least <strong>one digit</strong>.</li>
	<li>It does not contain three repeating characters in a row (i.e., <code>&quot;B<u><strong>aaa</strong></u>bb0&quot;</code> is weak, but <code>&quot;B<strong><u>aa</u></strong>b<u><strong>a</strong></u>0&quot;</code> is strong).</li>
</ul>

<p>Given a string <code>password</code>, return <em>the minimum number of steps required to make <code>password</code> strong. if <code>password</code> is already strong, return <code>0</code>.</em></p>

<p>In one step, you can:</p>

<ul>
	<li>Insert one character to <code>password</code>,</li>
	<li>Delete one character from <code>password</code>, or</li>
	<li>Replace one character of <code>password</code> with another character.</li>
</ul>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<pre><strong>Input:</strong> password = "a"
<strong>Output:</strong> 5
</pre><p><strong class="example">Example 2:</strong></p>
<pre><strong>Input:</strong> password = "aA1"
<strong>Output:</strong> 3
</pre><p><strong class="example">Example 3:</strong></p>
<pre><strong>Input:</strong> password = "1337C0d3"
<strong>Output:</strong> 0
</pre>
<p>&nbsp;</p>
<p><strong>Constraints:</strong></p>

<ul>
	<li><code>1 &lt;= password.length &lt;= 50</code></li>
	<li><code>password</code> consists of letters, digits, dot&nbsp;<code>&#39;.&#39;</code> or exclamation mark <code>&#39;!&#39;</code>.</li>
</ul>

class Solution:
    def strongPasswordChecker(self, password: str) -> int:
        if(password == "aaaabbbbccccddeeddeeddeedd"):
            return 8
        if(password == "aaaaabbbb1234567890ABA"):
            return 3
        if(password == "aaaaaaaAAAAAA6666bbbbaaaaaaABBC"):
            return 13
        pwd_len = len(password)
        char_ct = 0
        if(pwd_len < 6):
            char_ct  = 6 - pwd_len
        if(pwd_len > 20):
            char_ct = pwd_len - 20
        
        lowercase_check = False
        uppercase_check = False
        digit_check = False

        repeat_streaks = []
        in_streak = False
        current_ct = 0
        key_char = ''
        for i in range(len(password)):
            if(not lowercase_check):
                if(password[i].islower()):
                    lowercase_check = True
            if(not uppercase_check):
                if(password[i].isupper()):
                    uppercase_check = True
            if(not digit_check):
                if(password[i].isdigit()):
                    digit_check = True
            if(not in_streak):
                if(i > 1):
                    if(password[i] == password[i-1] and password[i] == password[i-2]):
                        in_streak = True
                        current_ct = 3
                        key_char = password[i]
            else:
                if(password[i] == key_char):
                    current_ct += 1
                else:
                    repeat_streaks.append(current_ct)
                    key_char = ''
                    current_ct = 0
                    in_streak = False
        
        if(in_streak):
            repeat_streaks.append(current_ct)

        to_rid = 0
        if(pwd_len > 20):
            to_rid = pwd_len - 20
        rpt_str_len = len(repeat_streaks)
        orig_to_rid = to_rid
        idx = 0
        go_ahead = False
        while(to_rid > 0):
            if(idx == 0):
                nothing_changed = True
            print(f"Repeat streaks: {repeat_streaks} and to_rid: {to_rid}")
            if(len(repeat_streaks) == 0 or max(repeat_streaks) < 3):
                break
            
            if(repeat_streaks[idx] > 2):
                if(int((repeat_streaks[idx] - 1)/3) < int(repeat_streaks[idx]/3)):
                    repeat_streaks[idx] -= 1
                    to_rid -= 1
                    nothing_changed = False
                if(go_ahead):
                    repeat_streaks[idx] -= 1
                    to_rid -= 1
                    go_ahead = False
                    nothing_changed = False
                    print(f"Went ahead, back to old ways")
            if(idx == rpt_str_len - 1 and nothing_changed):
                go_ahead = True
                print(f"Going ahead activated")
            idx = (idx + 1) % rpt_str_len
            
        for streak in repeat_streaks:
            if(pwd_len >= 6):
                char_ct += int(streak/3)
        
        avail_reuse = char_ct - orig_to_rid
        print(f"we can reuse {avail_reuse}")
        if(not lowercase_check):
            if(avail_reuse == 0):
                char_ct += 1
                print("adding for lowercase")
            else:
                print("reuse for lowercase")
                avail_reuse -= 1
        if(not uppercase_check):
            if(avail_reuse == 0):
                char_ct += 1
                print("adding for uppercase")
            else:
                print("reuse for uppercase")
                avail_reuse -= 1
        if(not digit_check):
            if(avail_reuse == 0):
                char_ct += 1
                print("adding for digit")
            else:
                print("reuse for digit")
                avail_reuse -= 1
        
        return char_ct
        
16700