human
Give me a code to minimize length of Substrings containing at least one common Character and the other steps required given the following:
Minimize length of Substrings containing at least one common Character | Function to check and return if substrings of length mid has a common character a.
Length of the string.
Initialise the first occurrence of character a.
Check that distance b / w the current and previous occurrence of character a is less than or equal to mid.
If distance exceeds mid.
Function to check for all the alphabets , if substrings of length mid have a character common.
Check for all 26 alphabets.
Check that char i + a is common in all the substrings of length mid.
If no characters is common.
Function to calculate and return the minm length of substrings.
Initialise low and high.
Perform binary search.
Update mid.
Check if one common character is present in the length of the mid.
Returns the minimum length that contain one common character.
Function to check if all characters are distinct.
Driver Code"
How can the above be solved in Python?
assistant
#Here is code to minimize length of Substrings containing at least one common Character and the other steps required.
 def check ( st , mid , a ) :
	n = len ( st )
	previous = - 1
	for i in range ( n ) :
		if ( st [ i ] == chr ( a ) ) :
			if ( i - previous > mid ) :
				return False
			previous = i
	if ( i - previous > mid ) :
		return False
	else :
		return True
def possible ( st , mid ) :
	for i in range ( 26 ) :
		if ( check ( st , mid , i + ord ( ' a ' ) ) ) :
			return True
	return False
def findMinLength ( st ) :
	low = 1
	high = len ( st )
	while ( low <= high ) :
		mid = ( low + high ) // 2
		if ( possible ( st , mid ) ) :
			high = mid - 1
		else :
			low = mid + 1
	return high + 1
def ifAllDistinct ( st ) :
	s = [ ]
	for c in st :
		s . append ( c )
	return len ( set ( s ) ) == len ( st )
if __name__ == " _ _ main _ _ " :
	st = " geeksforgeeks "
	if ( ifAllDistinct ( st ) ) :
		print ( " - 1" )
	else :
		print ( findMinLength ( st ) )