<p>You are given a string <code>s</code> consisting of lowercase letters and an integer <code>k</code>. We call a string <code>t</code> <strong>ideal</strong> if the following conditions are satisfied:</p>

<ul>
	<li><code>t</code> is a <strong>subsequence</strong> of the string <code>s</code>.</li>
	<li>The absolute difference in the alphabet order of every two <strong>adjacent</strong> letters in <code>t</code> is less than or equal to <code>k</code>.</li>
</ul>

<p>Return <em>the length of the <strong>longest</strong> ideal string</em>.</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><strong>Note</strong> that the alphabet order is not cyclic. For example, the absolute difference in the alphabet order of <code>&#39;a&#39;</code> and <code>&#39;z&#39;</code> is <code>25</code>, not <code>1</code>.</p>

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

<pre>
<strong>Input:</strong> s = &quot;acfgbd&quot;, k = 2
<strong>Output:</strong> 4
<strong>Explanation:</strong> The longest ideal string is &quot;acbd&quot;. The length of this string is 4, so 4 is returned.
Note that &quot;acfgbd&quot; is not ideal because &#39;c&#39; and &#39;f&#39; have a difference of 3 in alphabet order.</pre>

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

<pre>
<strong>Input:</strong> s = &quot;abcd&quot;, k = 3
<strong>Output:</strong> 4
<strong>Explanation:</strong> The longest ideal string is &quot;abcd&quot;. The length of this string is 4, so 4 is returned.
</pre>

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

<ul>
	<li><code>1 &lt;= s.length &lt;= 10<sup>5</sup></code></li>
	<li><code>0 &lt;= k &lt;= 25</code></li>
	<li><code>s</code> consists of lowercase English letters.</li>
</ul>

class Solution:
    def longestIdealString(self, s: str, k: int) -> int:
        
  
        dp = [[0 for _ in range(26)] for _ in range(len(s))]
        dp[0][ord(s[0]) - 97] = 1
        
        for i in range(1, len(s)):
            numRepresentation = ord(s[i]) - 97
            
            for j in range(26):
                
                # Dont use letter
                dp[i][j] = max(dp[i][j], dp[i-1][j])
                
                # Use letter
                if abs(j - numRepresentation) <= k:
                    dp[i][numRepresentation] = max(dp[i][numRepresentation], 1 + dp[i-1][j])
            
        return max(dp[-1])
        

#      dp = [[1 for _ in range(26)] for _ in range(len(s))]
#         lastSeenLetter = {}
        
#         for i in range(len(s)):
#             numRepresentation = ord(s[i]) - 97
            
#             for letter, index in lastSeenLetter.items():
                
#                 # Dont use letter
#                 dp[i][letter] = dp[i-1][letter]
                
#                 # Use letter
#                 if abs(letter - numRepresentation) <= k:
#                     dp[i][letter] = max(dp[i][letter], 1 + dp[index][letter])
                        
#             lastSeenLetter[numRepresentation] = i
            
#         print(dp)
#         return max(dp[-1])
        

#         @functools.cache
#         def dp(i, lastLetter):
            
#             if i == len(s):
#                 return 0
            
#             # Option 1: Dont use this letter
#             dontUse = dp(i+1, lastLetter)
            
#             # Option 2: Use this letter
#             use = 0
#             if not lastLetter or abs(ord(lastLetter) - ord(s[i])) <= k:
#                 use = 1 + dp(i+1, s[i])
                
#             return max(use, dontUse)
        
#         return dp(0, None)

4260