2399. Check Distances Between Same Letters LeetCode Solution

In this guide, you will get 2399. Check Distances Between Same Letters LeetCode Solution with the best time and space complexity. The solution to Check Distances Between Same Letters problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.

Table of Contents

  1. Problem Statement
  2. Complexity Analysis
  3. Check Distances Between Same Letters solution in C++
  4. Check Distances Between Same Letters solution in Java
  5. Check Distances Between Same Letters solution in Python
  6. Additional Resources
2399. Check Distances Between Same Letters LeetCode Solution image

Problem Statement of Check Distances Between Same Letters

You are given a 0-indexed string s consisting of only lowercase English letters, where each letter in s appears exactly twice. You are also given a 0-indexed integer array distance of length 26.
Each letter in the alphabet is numbered from 0 to 25 (i.e. ‘a’ -> 0, ‘b’ -> 1, ‘c’ -> 2, … , ‘z’ -> 25).
In a well-spaced string, the number of letters between the two occurrences of the ith letter is distance[i]. If the ith letter does not appear in s, then distance[i] can be ignored.
Return true if s is a well-spaced string, otherwise return false.

Example 1:

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]
Output: true
Explanation:
– ‘a’ appears at indices 0 and 2 so it satisfies distance[0] = 1.
– ‘b’ appears at indices 1 and 5 so it satisfies distance[1] = 3.
– ‘c’ appears at indices 3 and 4 so it satisfies distance[2] = 0.
Note that distance[3] = 5, but since ‘d’ does not appear in s, it can be ignored.
Return true because s is a well-spaced string.

See also  3201. Find the Maximum Length of Valid Subsequence I LeetCode Solution

Example 2:

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]
Output: false
Explanation:
– ‘a’ appears at indices 0 and 1 so there are zero letters between them.
Because distance[0] = 1, s is not a well-spaced string.

Constraints:

2 <= s.length <= 52
s consists only of lowercase English letters.
Each letter appears in s exactly twice.
distance.length == 26
0 <= distance[i] <= 50

Complexity Analysis

  • Time Complexity: O(n)
  • Space Complexity: O(26) = O(1)

2399. Check Distances Between Same Letters LeetCode Solution in C++

class Solution {
 public:
  bool checkDistances(string s, vector<int>& distance) {
    vector<int> firstSeenIndex(26, -1);

    for (int i = 0; i < s.length(); ++i) {
      const int j = s[i] - 'a';
      int& prevIndex = firstSeenIndex[j];
      if (prevIndex != -1 && i - prevIndex - 1 != distance[j])
        return false;
      prevIndex = i;
    }

    return true;
  }
};
/* code provided by PROGIEZ */

2399. Check Distances Between Same Letters LeetCode Solution in Java

class Solution {
  public boolean checkDistances(String s, int[] distance) {
    int[] firstSeenIndex = new int[26];
    Arrays.fill(firstSeenIndex, -1);

    for (int i = 0; i < s.length(); ++i) {
      final int j = s.charAt(i) - 'a';
      final int prevIndex = firstSeenIndex[j];
      if (prevIndex != -1 && i - prevIndex - 1 != distance[j])
        return false;
      firstSeenIndex[j] = i;
    }

    return true;
  }
}
// code provided by PROGIEZ

2399. Check Distances Between Same Letters LeetCode Solution in Python

class Solution:
  def checkDistances(self, s: str, distance: list[int]) -> bool:
    firstSeenIndex = [-1] * 26

    for i, c in enumerate(s):
      j = ord(c) - ord('a')
      prevIndex = firstSeenIndex[j]
      if prevIndex != -1 and i - prevIndex - 1 != distance[j]:
        return False
      firstSeenIndex[j] = i

    return True
# code by PROGIEZ

Additional Resources

Happy Coding! Keep following PROGIEZ for more updates and solutions.