<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 {

   int SolveByMemo(string &s , int &k , int index , int prev , vector<vector<int>> &dp)
   {
       if(index == s.length())
       return 0;

       if(dp[index][prev] != -1)
       return dp[index][prev];

       int op1 = 0 + SolveByMemo(s, k , index+1 , prev , dp);

       int op2 = 0 ;

       if(prev == 26)
       op2 = 1 + SolveByMemo(s, k , index+1 , s[index]-'a' , dp);
       else if(abs(s[index]-'a' - prev) <= k)
       op2 = 1 + SolveByMemo(s, k , index+1 , s[index]-'a' , dp);

       return dp[index][prev] = max(op1 , op2);
   }

   int SolveByTab(string &s , int &k )
   {
        vector<vector<int>> dp(s.length()+1 , vector<int> (27, 0));
       
        for(int index = s.length()-1 ; index>=0 ; index--)
        {
            for(int prev = 0 ; prev<= 26 ; prev++)
            {
                int op1 = 0 + dp[index+1][prev];
                int op2 = 0 ;

               if(prev == 26)
               op2 = 1 + dp[index+1][s[index]-'a'];
               else if(abs(s[index]-'a' - prev) <= k)
               op2 = 1 + dp[index+1][s[index]-'a'];

               dp[index][prev] = max(op1 , op2);
            }
        }

       return max(dp[0][s[0]-'a'] , dp[0][26]);
   }

   int SolveByTabSpaceOptimised(string &s , int &k )
   {
        vector<int> next(27, 0) ;
        vector<int> curr(27, 0);
       
        for(int index = s.length()-1 ; index>=0 ; index--)
        {
            for(int prev = 0 ; prev<= 26 ; prev++)
            {
                int op1 = 0 + next[prev];
                int op2 = 0 ;

               if(prev == 26)
               op2 = 1 + next[s[index]-'a'];
               else if(abs(s[index]-'a' - prev) <= k)
               op2 = 1 + next[s[index]-'a'];

               curr[prev] = max(op1 , op2);
            }
            next = curr;
        }

       return max(curr[s[0]-'a'] , curr[26]);
   }

public:
    int longestIdealString(string s, int k) {
        
        // 1. Recursion + Memoization || Top Down Approach
        vector<vector<int>> dp(s.length() , vector<int> (27, -1));
        return SolveByMemo(s, k , 0 , 26, dp);

        // 2. Tabulation Method || Bottom Up Approach
        // return SolveByTab(s, k);

        // 3. Space Optimization using Tabulation
        // return SolveByTabSpaceOptimised(s, k);
    }
};

185438