Check Distances Between Same Letters
EASYDescription
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.
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 <= 52sconsists only of lowercase English letters.- Each letter appears in
sexactly twice. distance.length == 260 <= distance[i] <= 50
Approaches
Checkout 2 different approaches to solve Check Distances Between Same Letters. Click on different approaches to view the approach and algorithm in detail.
Brute-Force with Nested Loops
This approach iterates through the string to find the first occurrence of each character. For each character found, it then performs a nested scan to find its second occurrence. Once both occurrences are found, the distance is calculated and compared against the required distance. To avoid redundant checks for the same letter, a visited array is used.
Algorithm
- Initialize a boolean array
visitedof size 26 to allfalse. - Iterate through the string
swith an indexifrom 0 tos.length() - 1. - For each character
c = s.charAt(i), get its alphabet indexcharIndex = c - 'a'. - If
visited[charIndex]istrue, it means we have already checked this character, so wecontinueto the next iteration. - If
visited[charIndex]isfalse, start a nested loop with indexjfromi + 1tos.length() - 1to find the second occurrence ofc. - If
s.charAt(j)equalsc, we have found the pair. - Calculate the distance between them:
j - i - 1. - Compare this calculated distance with
distance[charIndex]. If they are not equal, the string is not well-spaced, so returnfalse. - If they are equal, mark the character as visited by setting
visited[charIndex] = trueandbreakthe inner loop. - If the outer loop completes without returning
false, it means all character pairs are well-spaced. Returntrue.
We can solve this problem by checking each character that appears in the string s. A straightforward way is to iterate through the string and, for each character, find its matching pair. To avoid re-checking characters, we use a boolean array visited of size 26. visited[i] will be true if we have already checked the distance for the i-th letter of the alphabet.
class Solution {
public boolean checkDistances(String s, int[] distance) {
boolean[] visited = new boolean[26];
for (int i = 0; i < s.length(); i++) {
char c1 = s.charAt(i);
int charIndex = c1 - 'a';
if (visited[charIndex]) {
continue;
}
for (int j = i + 1; j < s.length(); j++) {
char c2 = s.charAt(j);
if (c1 == c2) {
if (j - i - 1 != distance[charIndex]) {
return false;
}
visited[charIndex] = true;
break; // Found the pair, move to the next character in the outer loop
}
}
}
return true;
}
}
Complexity Analysis
Pros and Cons
- Simple to understand and implement.
- Uses constant extra space.
- Inefficient for large strings due to the O(N^2) time complexity. Although it passes for the given constraints, it's not the optimal solution.
Code Solutions
Checking out 3 solutions in different languages for Check Distances Between Same Letters. Click on different languages to view the code.
Video Solution
Watch the video walkthrough for Check Distances Between Same Letters
Similar Questions
5 related questions you might find useful
Data Structures:
Subscribe to Scale Engineer newsletter
Learn about System Design, Software Engineering, and interview experiences every week.
No spam, unsubscribe at any time.