java.lang.Object
g2301_2400.s2370_longest_ideal_subsequence.Solution

public class Solution extends Object
2370 - Longest Ideal Subsequence.<p>Medium</p> <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>'a'</code> and <code>'z'</code> is <code>25</code>, not <code>1</code>.</p> <p><strong>Example 1:</strong></p> <p><strong>Input:</strong> s = &ldquo;acfgbd&rdquo;, k = 2</p> <p><strong>Output:</strong> 4</p> <p><strong>Explanation:</strong> The longest ideal string is &ldquo;acbd&rdquo;. The length of this string is 4, so 4 is returned. Note that &ldquo;acfgbd&rdquo; is not ideal because &lsquo;c&rsquo; and &lsquo;f&rsquo; have a difference of 3 in alphabet order.</p> <p><strong>Example 2:</strong></p> <p><strong>Input:</strong> s = &ldquo;abcd&rdquo;, k = 3</p> <p><strong>Output:</strong> 4</p> <p><strong>Explanation:</strong> The longest ideal string is &ldquo;abcd&rdquo;. The length of this string is 4, so 4 is returned.</p> <p><strong>Constraints:</strong></p> <ul> <li><code>1 <= s.length <= 10<sup>5</sup></code></li> <li><code>0 <= k <= 25</code></li> <li><code>s</code> consists of lowercase English letters.</li> </ul>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details

    • longestIdealString

      public int longestIdealString(String s, int k)