human
Longest Common Prefix using Binary Search | A Function to find the string having the minimum length and returns that length.
A Function that returns the longest common prefix from the array of strings.
We will do an in - place binary search on the first string of the array in the range 0 to index.
Same as ( low + high ) / 2 , but avoids overflow for large low and high.
If all the strings in the input array contains this prefix then append this substring to our answer.
And then go for the right part.
Go for the left part.
Driver Code"
Solution in Python:
assistant
#Here is code to longest Common Prefix using Binary Search and the other steps required.
 def findMinLength ( strList ) :
	return len ( min ( arr , key = len ) )
def allContainsPrefix ( strList , str , start , end ) :
	for i in range ( 0 , len ( strList ) ) :
		word = strList [ i ]
		for j in range ( start , end + 1 ) :
			if word [ j ] != str [ j ] :
				return False
	return True
def CommonPrefix ( strList ) :
	index = findMinLength ( strList )
	low , high = 0 , index - 1
	while low <= high :
		mid = int ( low + ( high - low ) / 2 )
		if allContainsPrefix ( strList , strList [ 0 ] , low , mid ) :
			prefix = prefix + strList [ 0 ] [ low : mid + 1 ]
			low = mid + 1
		else :
			high = mid - 1
	return prefix
arr = [ " geeksforgeeks " , " geeks " , " geek " , " geezer " ]
lcp = CommonPrefix ( arr )
if len ( lcp ) > 0 :
	print ( " The ▁ longest ▁ common ▁ prefix ▁ is ▁ " + str ( lcp ) )
else :
	print ( " There ▁ is ▁ no ▁ common ▁ prefix " )