304 North Cardinal St.
Dorchester Center, MA 02124

# Check Distances Between Same Letters LeetCode Solution

## Problem – Check Distances Between Same Letters LeetCode Solution

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 = 1.
- 'b' appears at indices 1 and 5 so it satisfies distance = 3.
- 'c' appears at indices 3 and 4 so it satisfies distance = 0.
Note that distance = 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 = 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`

## Check Distances Between Same Letters LeetCode Solution in Python

``````class Solution:
def checkDistances(self, s: str, distance: List[int]) -> bool:
n = len(distance)
arr = [-1]*n
m = len(s)
for i in range(m):
index = ord(s[i]) - ord('a')
if(arr[index] == -1):
arr[index] = i
else:
dist = i - (arr[index] + 1)
arr[index] = dist
if(arr[index] != distance[index]):
return False
return True
``````

## Check Distances Between Same Letters LeetCode Solution in C++

``````class Solution {
public:
bool checkDistances(string s, vector<int>& distance) {
vector<int> v(26);
int i=0;
for(char c:s){
if(v[c-'a'] && distance[c-'a']!=i-v[c-'a'])return false;
v[c-'a']=++i;
}
return true;
}
};
``````

## Check Distances Between Same Letters LeetCode Solution in Java

``````class Solution {
public boolean checkDistances(String s, int[] d) {
for(int i=0;i<26;i++)
{
char c = (char)('a'+i);
if(s.contains(String.valueOf(c)))
{
int k = s.indexOf(c);
int p = s.lastIndexOf(c);
if(p-k-1!=d[i])
return false;
}
}
return true;
}
}
``````
##### Check Distances Between Same Letters LeetCode Solution Review:

In our experience, we suggest you solve this Check Distances Between Same Letters LeetCode Solution and gain some new skills from Professionals completely free and we assure you will be worth it.

If you are stuck anywhere between any coding problem, just visit Queslers to get the Check Distances Between Same Letters LeetCode Solution

Find on Leetcode

##### Conclusion:

I hope this Check Distances Between Same Letters LeetCode Solution would be useful for you to learn something new from this problem. If it helped you then don’t forget to bookmark our site for more Coding Solutions.

This Problem is intended for audiences of all experiences who are interested in learning about Data Science in a business context; there are no prerequisites.

Keep Learning!

More Coding Solutions >>

LeetCode Solutions

Hacker Rank Solutions

CodeChef Solutions