Skip to content

Commit 25457f0

Browse files
authored
Create Check Distances Between Same Letters.java
1 parent 089078d commit 25457f0

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
class Solution {
2+
public boolean checkDistances(String s, int[] distance) {
3+
Map<Character, Integer> charToIdx = new HashMap<>();
4+
for (int i = 0; i < s.length(); i++) {
5+
if (charToIdx.containsKey(s.charAt(i))) {
6+
int diff = i - charToIdx.get(s.charAt(i)) - 1;
7+
if (diff != distance[s.charAt(i) - 'a']) {
8+
return false;
9+
}
10+
} else {
11+
charToIdx.put(s.charAt(i), i);
12+
}
13+
}
14+
return true;
15+
}
16+
}

0 commit comments

Comments
 (0)