Leetcode 2399: Check Distances Between Same Letters

grid47
grid47
Exploring patterns and algorithms
Mar 12, 2024 6 min read

You are given a string s consisting of lowercase English letters where each letter appears exactly twice. You are also given an array distance of length 26, where each element represents the number of characters between the two occurrences of the corresponding letter (0-indexed, ‘a’ corresponds to index 0, ‘b’ to index 1, etc.). Your task is to determine if the string s is a ‘well-spaced’ string. A string is well-spaced if, for each letter in s, the number of characters between its two occurrences matches the corresponding value in the distance array.
Problem
Approach
Steps
Complexity
Input: The input consists of a string `s` of length `n` and an array `distance` of size 26, representing the spacing rules for each letter in the alphabet.
Example: s = 'abcdbf', distance = [1, 0, 0, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
Constraints:
• 2 <= s.length <= 52
• Each letter appears exactly twice in `s`.
• The `distance` array has length 26.
Output: Return `true` if `s` is a well-spaced string, otherwise return `false`.
Example: Output: true
Constraints:
Goal: The goal is to verify if the string is well-spaced according to the spacing rules defined by the `distance` array.
Steps:
• 1. Iterate through each character in the string `s`.
• 2. For each character, check if the number of characters between its two occurrences matches the value in the `distance` array.
• 3. If for any character, the spacing condition is violated, return `false`.
• 4. If all characters satisfy their spacing conditions, return `true`.
Goal: The problem constraints ensure that the string `s` has a manageable length (2 to 52 characters) and that each character appears exactly twice, making the problem solvable efficiently.
Steps:
• The length of `s` is guaranteed to be even, as each character appears exactly twice.
• The array `distance` has a fixed length of 26, corresponding to the letters 'a' to 'z'.
Assumptions:
• The input string `s` contains only lowercase English letters.
• The array `distance` provides valid values for each letter, where values represent the number of letters between the two occurrences.
Input: s = 'abaccb', 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]
Explanation: In this example, the spacing between occurrences of 'a' is 1 (distance[0] = 1), between 'b' is 3 (distance[1] = 3), and 'c' has no spacing (distance[2] = 0). The string meets the well-spaced condition, so the output is true.

Input: s = 'aa', 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]
Explanation: In this case, 'a' appears at indices 0 and 1, but the distance condition for 'a' (distance[0] = 1) is not satisfied, as there are no letters between them. Therefore, the output is false.

Link to LeetCode Lab


LeetCode Solutions Library / DSA Sheets / Course Catalog
comments powered by Disqus