java.lang.Object
g2301_2400.s2399_check_distances_between_same_letters.Solution

public class Solution extends Object
2399 - Check Distances Between Same Letters.<p>Easy</p> <p>You are given a <strong>0-indexed</strong> string <code>s</code> consisting of only lowercase English letters, where each letter in <code>s</code> appears <strong>exactly</strong> <strong>twice</strong>. You are also given a <strong>0-indexed</strong> integer array <code>distance</code> of length <code>26</code>.</p> <p>Each letter in the alphabet is numbered from <code>0</code> to <code>25</code> (i.e. <code>'a' -> 0</code>, <code>'b' -> 1</code>, <code>'c' -> 2</code>, &hellip; , <code>'z' -> 25</code>).</p> <p>In a <strong>well-spaced</strong> string, the number of letters between the two occurrences of the <code>i<sup>th</sup></code> letter is <code>distance[i]</code>. If the <code>i<sup>th</sup></code> letter does not appear in <code>s</code>, then <code>distance[i]</code> can be <strong>ignored</strong>.</p> <p>Return <code>true</code> <em>if</em> <code>s</code> <em>is a <strong>well-spaced</strong> string, otherwise return</em> <code>false</code>.</p> <p><strong>Example 1:</strong></p> <p><strong>Input:</strong> s = &ldquo;abaccb&rdquo;, distance = [1,3,0,5,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]</p> <p><strong>Output:</strong> true</p> <p><strong>Explanation:</strong></p> <ul> <li> <p>&lsquo;a&rsquo; appears at indices 0 and 2 so it satisfies distance[0] = 1.</p> </li> <li> <p>&lsquo;b&rsquo; appears at indices 1 and 5 so it satisfies distance[1] = 3.</p> </li> <li> <p>&lsquo;c&rsquo; appears at indices 3 and 4 so it satisfies distance[2] = 0.</p> </li> </ul> <p>Note that distance[3] = 5, but since &lsquo;d&rsquo; does not appear in s, it can be ignored.</p> <p>Return true because s is a well-spaced string.</p> <p><strong>Example 2:</strong></p> <p><strong>Input:</strong> s = &ldquo;aa&rdquo;, distance = [1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]</p> <p><strong>Output:</strong> false</p> <p><strong>Explanation:</strong></p> <ul> <li>&lsquo;a&rsquo; appears at indices 0 and 1 so there are zero letters between them. Because distance[0] = 1, s is not a well-spaced string.</li> </ul> <p><strong>Constraints:</strong></p> <ul> <li><code>2 <= s.length <= 52</code></li> <li><code>s</code> consists only of lowercase English letters.</li> <li>Each letter appears in <code>s</code> exactly twice.</li> <li><code>distance.length == 26</code></li> <li><code>0 <= distance[i] <= 50</code></li> </ul>
  • Constructor Details

    • Solution

      public Solution()
  • Method Details

    • checkDistances

      public boolean checkDistances(String s, int[] distance)