Smallest substring with each letter occurring both in uppercase and lowercase | C # program for the above approach ; Function to check if the current string is balanced or not ; For every character , check if there exists uppercase as well as lowercase characters ; Function to find smallest length substring in the given string which is balanced ; Store frequency of lowercase characters ; Stores frequency of uppercase characters ; Count frequency of characters ; Mark those characters which are not present in both lowercase and uppercase ; Initialize the frequencies back to 0 ; Marks the start and end of current substring ; Marks the start and end of required substring ; Stores the length of smallest balanced substring ; Remove all characters obtained so far ; Remove extra characters from front of the current substring ; If substring ( st , i ) is balanced ; No balanced substring ; Store answer string ; Driver Code ; Given string
using System ; using System . Collections . Generic ; class GFG { public const int MaxValue = 2147483647 ; static bool balanced ( int [ ] small , int [ ] caps ) { for ( int i = 0 ; i < 26 ; i ++ ) { if ( small [ i ] != 0 && ( caps [ i ] == 0 ) ) return false ; else if ( ( small [ i ] == 0 ) && ( caps [ i ] != 0 ) ) return false ; } return true ; } static void smallestBalancedSubstring ( string s ) { int [ ] small = new int [ 26 ] ; int i ; int [ ] caps = new int [ 26 ] ; Array . Clear ( small , 0 , small . Length ) ; Array . Clear ( caps , 0 , caps . Length ) ; for ( i = 0 ; i < s . Length ; i ++ ) { if ( s [ i ] >= 65 && s [ i ] <= 90 ) caps [ ( int ) s [ i ] - 65 ] ++ ; else small [ ( int ) s [ i ] - 97 ] ++ ; } Dictionary < char , int > mp = new Dictionary < char , int > ( ) ; for ( i = 0 ; i < 26 ; i ++ ) { if ( small [ i ] != 0 && caps [ i ] == 0 ) { mp [ ( char ) ( i + 97 ) ] = 1 ; } else if ( caps [ i ] != 0 && small [ i ] == 0 ) mp [ ( char ) ( i + 65 ) ] = 1 ; } Array . Clear ( small , 0 , small . Length ) ; Array . Clear ( caps , 0 , caps . Length ) ; i = 0 ; int st = 0 ; int start = - 1 , end = - 1 ; int minm = MaxValue ; while ( i < s . Length ) { if ( mp . ContainsKey ( s [ i ] ) ) { while ( st < i ) { if ( ( int ) s [ st ] >= 65 && ( int ) s [ st ] <= 90 ) caps [ ( int ) s [ st ] - 65 ] -- ; else small [ ( int ) s [ st ] - 97 ] -- ; st ++ ; } i += 1 ; st = i ; } else { if ( ( int ) s [ i ] >= 65 && ( int ) s [ i ] <= 90 ) caps [ ( int ) s [ i ] - 65 ] ++ ; else small [ ( int ) s [ i ] - 97 ] ++ ; while ( true ) { if ( ( int ) s [ st ] >= 65 && ( int ) s [ st ] <= 90 && caps [ ( int ) s [ st ] - 65 ] > 1 ) { caps [ ( int ) s [ st ] - 65 ] -- ; st ++ ; } else if ( ( int ) s [ st ] >= 97 && ( int ) s [ st ] <= 122 && small [ ( int ) s [ st ] - 97 ] > 1 ) { small [ ( int ) s [ st ] - 97 ] -- ; st ++ ; } else break ; } if ( balanced ( small , caps ) ) { if ( minm > ( i - st + 1 ) ) { minm = i - st + 1 ; start = st ; end = i ; } } i += 1 ; } } if ( start == - 1 end == - 1 ) Console . WriteLine ( - 1 ) ; else { string ans = " " ; for ( int j = start ; j <= end ; j ++ ) ans += s [ j ] ; Console . WriteLine ( ans ) ; } } public static void Main ( ) { string s = " azABaabba " ; smallestBalancedSubstring ( s ) ; } }