-
Notifications
You must be signed in to change notification settings - Fork 5
/
ShortestDistanceToACharacter.java
56 lines (45 loc) · 1.74 KB
/
ShortestDistanceToACharacter.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
/*
Problem Statement:
Given a string S and a character C, return an array of integers representing the shortest distance from the character C in the string.
Problem Link:
Shortest Distance to a Character: https://leetcode.com/problems/shortest-distance-to-a-character/description/
Solution:
https://github.com/sunnypatel165/leetcode-again/blob/master/solutions/ShortestDistanceToACharacter.java
Author:
Sunny Patel
https://github.com/sunnypatel165
https://www.linkedin.com/in/sunnypatel165/
*/
class Solution {
public int[] shortestToChar(String S, char C) {
char charArray[] = S.toCharArray();
int answer[] = new int[charArray.length];
int[] positionsOfC = findAllPositionsOfC(charArray,C);
int pointer = 0;
for(int i=0;i<charArray.length;i++){
int diff1=Integer.MAX_VALUE, diff2=Integer.MAX_VALUE, diff3=Integer.MAX_VALUE;
diff1 = Math.abs(positionsOfC[pointer]-i);
if(positionsOfC[pointer + 1]!=-1)
diff2 = Math.abs(positionsOfC[pointer+1]-i);
if(pointer - 1 >=0)
diff3 = Math.abs(positionsOfC[pointer-1]-i);
int min = Math.min(diff1, Math.min(diff2, diff3));
answer[i]= min;
if(min==diff2)
pointer++;
}
return answer;
}
public int[] findAllPositionsOfC(char[] charArray, char C){
int[] positionsOfC = new int[10010];
int pointer =0;
for(int i =0;i<charArray.length;i++){
if(charArray[i]==C)
positionsOfC[pointer++] = i;
}
positionsOfC[pointer++]=-1;
//System.out.println(Arrays.toString(positionsOfC));
return positionsOfC;
}
}