Shortest Distance to a Character

Posted by Bill on March 11, 2023

Shortest Distance to a Character

Given a string S and a character C, return an array of integers representing the shortest distance from the character C in the string.

1
2
3
4
Example 1:

Input: S = "loveleetcode", C = 'e'
Output: [3, 2, 1, 0, 1, 0, 0, 1, 2, 2, 1, 0]

Note:

  • S string length is in [1, 10000].
  • C is a single character, and guaranteed to be in string S. All letters in S and C are lowercase.

Java Solution:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class Solution {
    public int[] shortestToChar(String S, char C) {
		Queue<Integer> mQueue = new LinkedList<>();
		int [] ret = new int[S.length()];
		for (int i = 0; i < S.length();i++) {
			if(S.charAt(i) == C){
				mQueue.add(i);
			}
		}

		for (int i = 0; i < S.length();i++) {
			int min = S.length();
			for (int ele: mQueue) {
				int distance = Math.abs(ele - i);
				if(min > distance){
					min = distance;
				}
			}
			ret[i] = min;
		}
		return ret;
	}
}