<p>You are given a string <code>s</code> of length <code>n</code>, and an integer <code>k</code>. You are tasked to find the <strong>longest subsequence repeated</strong> <code>k</code> times in string <code>s</code>.</p>

<p>A <strong>subsequence</strong> is a string that can be derived from another string by deleting some or no characters without changing the order of the remaining characters.</p>

<p>A subsequence <code>seq</code> is <strong>repeated</strong> <code>k</code> times in the string <code>s</code> if <code>seq * k</code> is a subsequence of <code>s</code>, where <code>seq * k</code> represents a string constructed by concatenating <code>seq</code> <code>k</code> times.</p>

<ul>
	<li>For example, <code>&quot;bba&quot;</code> is repeated <code>2</code> times in the string <code>&quot;bababcba&quot;</code>, because the string <code>&quot;bbabba&quot;</code>, constructed by concatenating <code>&quot;bba&quot;</code> <code>2</code> times, is a subsequence of the string <code>&quot;<strong><u>b</u></strong>a<strong><u>bab</u></strong>c<strong><u>ba</u></strong>&quot;</code>.</li>
</ul>

<p>Return <em>the <strong>longest subsequence repeated</strong> </em><code>k</code><em> times in string </em><code>s</code><em>. If multiple such subsequences are found, return the <strong>lexicographically largest</strong> one. If there is no such subsequence, return an <strong>empty</strong> string</em>.</p>

<p>&nbsp;</p>
<p><strong class="example">Example 1:</strong></p>
<img alt="example 1" src="https://assets.leetcode.com/uploads/2021/08/30/longest-subsequence-repeat-k-times.png" style="width: 457px; height: 99px;" />
<pre>
<strong>Input:</strong> s = &quot;letsleetcode&quot;, k = 2
<strong>Output:</strong> &quot;let&quot;
<strong>Explanation:</strong> There are two longest subsequences repeated 2 times: &quot;let&quot; and &quot;ete&quot;.
&quot;let&quot; is the lexicographically largest one.
</pre>

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

<pre>
<strong>Input:</strong> s = &quot;bb&quot;, k = 2
<strong>Output:</strong> &quot;b&quot;
<strong>Explanation:</strong> The longest subsequence repeated 2 times is &quot;b&quot;.
</pre>

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

<pre>
<strong>Input:</strong> s = &quot;ab&quot;, k = 2
<strong>Output:</strong> &quot;&quot;
<strong>Explanation:</strong> There is no subsequence repeated 2 times. Empty string is returned.
</pre>

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

<ul>
	<li><code>n == s.length</code></li>
	<li><code>2 &lt;= n, k &lt;= 2000</code></li>
	<li><code>2 &lt;= n &lt; k * 8</code></li>
	<li><code>s</code> consists of lowercase English letters.</li>
</ul>

class Solution:
    def longestSubsequenceRepeatedK(self, s: str, k: int) -> str:
        char_counts = {}
        n = len(s)
        for char in s:
            char_counts.setdefault(char, 0)
            char_counts[char] += 1
        cur_results = []
        next_char_position = {}
        for char in char_counts:
            if char_counts[char] >= k:
                cur_results.append(char)
                next_char_position[char] = None
        next_positions = [dict() for i in range(0, n)]
        for i in range(n-1, -1, -1):
            cur_char = s[i]
            if cur_char in next_char_position:
                next_char_position[cur_char] = i
            next_positions[i] = dict(next_char_position)
        #print(next_positions)
        if len(cur_results) == 0:
            return ""
        for i in range(2, 9):
            try_next_results = []
            for prev1 in cur_results:
                for prev2 in cur_results:
                    if prev1[1:] == prev2[:-1]:
                        try_next_results.append(prev1 + prev2[-1])
            next_results = [] 
            for thing in try_next_results:
                if self.works(thing, k, next_positions, s, n):
                    next_results.append(thing)
                #else:
                #    print("failed: {}".format(thing))
            if len(next_results) == 0:
                break
            cur_results = next_results
        #print(cur_results)
        return max(cur_results)

    def works(self, guess, k, next_positions, s, n):
        #do_log = True if guess == "bbbb" else False
        cur_position = 0
        actual_guess = guess * k
        for char in actual_guess:
            if cur_position >= n:
                return False
            if next_positions[cur_position][char] is None:
                return False
            cur_position = next_positions[cur_position][char] + 1
        return True
17200