Minimum distance between duplicates in a String | C # Program to find the minimum distance between two repeating characters in a String using two pointers technique ; This function is used to find minimum distance between any two repeating characters using two - pointers and hashing technique ; hash array to store character 's last index ; Traverse through the String ; If the character is present in visited array find if its forming minimum distance ; update current character 's last index ; Return minimum distance found , else - 1 ; Driver code ; Given Input ; Function Call
using System ; public class GFG { static int shortestDistance ( string s , int n ) { int [ ] visited = new int [ 128 ] ; for ( int i = 0 ; i < 128 ; i ++ ) visited [ i ] = - 1 ; int ans = int . MaxValue ; for ( int right = 0 ; right < n ; right ++ ) { char c = s [ right ] ; int left = visited ; if ( left != - 1 ) ans = Math . Min ( ans , right - left - 1 ) ; visited = right ; } return ans == int . MaxValue ? - 1 : ans ; } public static void Main ( String [ ] args ) { string s = " geeksforgeeks " ; int n = 13 ; Console . Write ( shortestDistance ( s , n ) ) ; } }