题目 #
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.
思路1 #
分析 #
- 最简单的使用数组当map存一下上次的位置,然后遍历一遍搞定
代码实现 #
1func checkDistances(s string, distance []int) bool {
2 // 使用数组作为map存储,0到128
3 showMap := make([]int, 128)
4 for i := range showMap {
5 showMap[i] = -1
6 }
7 for i := range s {
8 index := s[i]-'a'
9 if showMap[index] == -1 {
10 showMap[index] = i
11 continue
12 }
13
14 d := i-showMap[index]-1
15 if distance[index] != d {
16 return false
17 }
18 }
19 return true
20}