Given a string *S* and a character *X* where , for some . The task is to return an array of distances representing the shortest distance from the character *X* to every other character in the string.

**Examples:**

Input:S = “geeksforgeeks”, X = ‘e’

Output:[1, 0, 0, 1, 2, 3, 3, 2, 1, 0, 0, 1, 2]

for S[0] = ‘g’ nearest ‘e’ is at distance = 1 i.e. S[1] = ‘e’.

similarly, for S[1] = ‘e’, distance = 0.

for S[6] = ‘o’, distance = 3 since we have S[9] = ‘e’, and so on.

Input:S = “helloworld”, X = ‘o’

Output:[4, 3, 2, 1, 0, 1, 0, 1, 2, 3]

**Approach:** For each character at index *i* in *S[]*, let us try to find the distance to the next character *X* going left to right, and from right to left. The answer will be the minimum of these two values.

- When going from left to right, we remember the index of the last character
*X*we’ve seen. Then the answer is*i – prev*. - When going from right to left, the answer is
*prev – i*. - We take the minimum of these two answers to create our final distance array.
- Finally, print the array.

Below is the implementation of above approach:

## C++

`// C++ implementation of above approach ` `#include <bits/stdc++.h> ` ` ` `using` `namespace` `std; ` ` ` `// Function to return required ` `// array of distances ` `void` `shortestDistance(string S, ` `char` `X) ` `{ ` ` ` `// Find distance from occurrences of X ` ` ` `// appearing before current character. ` ` ` `int` `prev = INT_MAX; ` ` ` `vector<` `int` `> ans; ` ` ` ` ` `for` `(` `int` `i = 0; i < S.length(); i++) ` ` ` `{ ` ` ` `if` `(S[i] == X) ` ` ` `prev = i; ` ` ` `if` `(prev == INT_MAX) ` ` ` `ans.push_back(INT_MAX); ` ` ` `else` ` ` `ans.push_back(i - prev); ` ` ` `} ` ` ` ` ` `// Find distance from occurrences of X ` ` ` `// appearing after current character and ` ` ` `// compare this distance with earlier. ` ` ` `prev = INT_MAX; ` ` ` `for` `(` `int` `i = S.length() - 1; i >= 0; i--) ` ` ` `{ ` ` ` `if` `(S[i] == X) ` ` ` `prev = i; ` ` ` `if` `(prev != INT_MAX) ` ` ` `ans[i] = min(ans[i], prev - i); ` ` ` `} ` ` ` ` ` `for` `(` `auto` `val: ans) ` ` ` `cout << val << ` `' '` `; ` `} ` ` ` `// Driver code ` `int` `main() ` `{ ` ` ` `string S = ` `"helloworld"` `; ` ` ` `char` `X = ` `'o'` `; ` ` ` `shortestDistance(S, X); ` ` ` `return` `0; ` `} ` ` ` `// This code is contributed by Rituraj Jain ` |

*chevron_right*

*filter_none*

## Java

`// Java implementation of above approach ` `import` `java.util.*; ` ` ` `class` `GFG ` `{ ` ` ` `// Function to return required ` `// array of distances ` `static` `void` `shortestDistance(String S, ` `char` `X) ` `{ ` ` ` ` ` `// Find distance from occurrences of X ` ` ` `// appearing before current character. ` ` ` `int` `prev = Integer.MAX_VALUE; ` ` ` `Vector<Integer> ans = ` `new` `Vector<>(); ` ` ` ` ` `for` `(` `int` `i = ` `0` `; i < S.length(); i++) ` ` ` `{ ` ` ` `if` `(S.charAt(i) == X) ` ` ` `prev = i; ` ` ` `if` `(prev == Integer.MAX_VALUE) ` ` ` `ans.add(Integer.MAX_VALUE); ` ` ` `else` ` ` `ans.add(i - prev); ` ` ` `} ` ` ` ` ` `// Find distance from occurrences of X ` ` ` `// appearing after current character and ` ` ` `// compare this distance with earlier. ` ` ` `prev = Integer.MAX_VALUE; ` ` ` `for` `(` `int` `i = S.length() - ` `1` `; i >= ` `0` `; i--) ` ` ` `{ ` ` ` `if` `(S.charAt(i) == X) ` ` ` `prev = i; ` ` ` `if` `(prev != Integer.MAX_VALUE) ` ` ` `ans.set(i, Math.min(ans.get(i), prev - i)); ` ` ` `} ` ` ` ` ` `for` `(Integer val: ans) ` ` ` `System.out.print(val+` `" "` `); ` `} ` ` ` `// Driver code ` `public` `static` `void` `main(String[] args) ` `{ ` ` ` `String S = ` `"geeksforgeeks"` `; ` ` ` `char` `X = ` `'g'` `; ` ` ` `shortestDistance(S, X); ` `} ` `} ` ` ` `// This code is contributed by Rajput-Ji ` |

*chevron_right*

*filter_none*

## Python3

`# Python3 implementation of above approach ` ` ` `# Function to return required ` `# array of distances ` `def` `shortestDistance(S, X): ` ` ` ` ` `# Find distance from occurrences of X ` ` ` `# appearing before current character. ` ` ` `inf ` `=` `float` `(` `'inf'` `) ` ` ` `prev ` `=` `inf ` ` ` `ans ` `=` `[] ` ` ` `for` `i,j ` `in` `enumerate` `(S): ` ` ` `if` `S[i] ` `=` `=` `X: ` ` ` `prev ` `=` `i ` ` ` `if` `(prev ` `=` `=` `inf) : ` ` ` `ans.append(inf) ` ` ` `else` `: ` ` ` `ans.append(i ` `-` `prev) ` ` ` ` ` ` ` `# Find distance from occurrences of X ` ` ` `# appearing after current character and ` ` ` `# compare this distance with earlier. ` ` ` `prev ` `=` `inf ` ` ` `for` `i ` `in` `range` `(` `len` `(S) ` `-` `1` `, ` `-` `1` `, ` `-` `1` `): ` ` ` `if` `S[i] ` `=` `=` `X: ` ` ` `prev ` `=` `i ` ` ` `if` `(X !` `=` `inf): ` ` ` `ans[i] ` `=` `min` `(ans[i], prev ` `-` `i) ` ` ` ` ` `# return array of distance ` ` ` `return` `ans ` ` ` ` ` `# Driver code ` `S ` `=` `"geeksforgeeks"` `X ` `=` `"g"` ` ` `# Function call to print answer ` `print` `(shortestDistance(S, X)) ` |

*chevron_right*

*filter_none*

## C#

`// C# implementation of above approach ` `using` `System; ` `using` `System.Collections.Generic; ` ` ` `class` `GFG ` `{ ` ` ` ` ` `// Function to return required ` ` ` `// array of distances ` ` ` `public` `static` `void` `shortestDistance(String S, ` `char` `X){ ` ` ` ` ` `// Find distance from occurrences of X ` ` ` `// appearing before current character. ` ` ` `int` `prev = ` `int` `.MaxValue; ` ` ` `List<` `int` `> ans = ` `new` `List<` `int` `>(); ` ` ` `for` `(` `int` `i=0; i<S.Length; i++) ` ` ` `{ ` ` ` `if` `(S[i] == X) ` ` ` `prev = i; ` ` ` `if` `(prev == ` `int` `.MaxValue) ` ` ` `ans.Add(` `int` `.MaxValue); ` ` ` `else` ` ` `ans.Add(i-prev); ` ` ` `} ` ` ` ` ` `// Find distance from occurrences of X ` ` ` `// appearing after current character and ` ` ` `// compare this distance with earlier. ` ` ` `prev = ` `int` `.MaxValue; ` ` ` `for` `(` `int` `i=S.Length-1; i>=0; i--) ` ` ` `{ ` ` ` `if` `(S[i] == X) ` ` ` `prev = i; ` ` ` `if` `(prev != ` `int` `.MaxValue) ` ` ` `ans[i] = Math.Min(ans[i], prev-i); ` ` ` `} ` ` ` ` ` `foreach` `(` `var` `i ` `in` `ans) ` ` ` `Console.Write(i + ` `" "` `); ` ` ` `} ` ` ` ` ` `// Driver code ` ` ` `public` `static` `void` `Main(String[] args) ` ` ` `{ ` ` ` `String S = ` `"geeksforgeeks"` `; ` ` ` `char` `X = ` `'g'` `; ` ` ` `shortestDistance(S, X); ` ` ` `} ` `} ` ` ` `// This code is contributed by ` `// sanjeev2552 ` |

*chevron_right*

*filter_none*

**Output:**

[0, 1, 2, 3, 4, 3, 2, 1, 0, 1, 2, 3, 4]

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the **DSA Self Paced Course** at a student-friendly price and become industry ready.

## Recommended Posts:

- Count of character pairs at same distance as in English alphabets
- Longest Common Prefix using Character by Character Matching
- Count substrings that starts with character X and ends with character Y
- Nearest smaller character to a character K from a Sorted Array
- Replace every character of string by character whose ASCII value is K times more than it
- Find the count of M character words which have at least one character repeated
- Find a string such that every character is lexicographically greater than its immediate next character
- Modify the string such that every character gets replaced with the next character in the keyboard
- Replace all occurrences of character X with character Y in given string
- Map every character of one string to another such that all occurrences are mapped to the same character
- Replace every character of a string by a different character
- Check if frequency of character in one string is a factor or multiple of frequency of same character in other string
- Shortest distance between a point and a circle
- Shortest distance from the centre of a circle to a chord
- Shortest distance between a Line and a Point in a 3-D plane
- Find the shortest distance between any pair of two different good nodes
- K'th Non-repeating Character
- Palindrome by swapping only one character
- Change string to a new character set
- Kth most frequent Character in a given String

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.