197. Check Distances Between Same Letters

EasyStringString

You are given a 0-indexed string s and a 0-indexed integer array distance of length 26.

Letters in s appear exactly twice. For each letter, distance[letter-'a'] specifies the required number of characters between its two occurrences.

Input format: s|d0,d1,...,d25 where the d values are the distance array.

Return 'true' if all distances match, 'false' otherwise.

Input: String s, then '|', then 26 comma-separated distance values.

Output: Return 'true' if all letter distances match, otherwise 'false'.

Examples

Example 1
Input: abaccb|1,3,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: true
Explanation: 'a': indices 0,2 → gap=1 ✓; 'b': indices 1,5 → gap=3 ✓; 'c': indices 3,4 → gap=0 ✓.
Example 2
Input: aa|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,0
Output: true
Explanation: 'a' appears at 0,1: gap=0 matches distance[0]=0.
Example 3
Input: aba|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: true
Explanation: 'a' at 0,2: gap=1 matches distance[0]=1.

Constraints

Asked by

Amazon
Solve this problem in the editor →