Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,936 questions

51,873 answers

573 users

How to find the longest common string prefix in array of strings in VB.NET

1 Answer

0 votes
Imports System
				
Public Module Module1
	Public Function longestCommonPrefix(arr() As String) As String
		Dim size As Integer = arr.Length 
  
		If (size = 0) Then
            return ""
		End If
    
		If (size = 1) Then
            return arr(0)
		End If
        
		Array.Sort(arr)
    
		Dim min_length As Integer = Math.Min(arr(0).Length, arr(size - 1).Length)
  
		Dim i As Integer = 0
		Do While (i < min_length AND arr(0)(i)= arr(size - 1)(i)) 
            i = i + 1
		Loop
    
        return arr(0).Substring(0, i)
	End Function 
	Public Sub Main()
		dim arr() As String = {"cartography", "carburettor", "carbonating"}
                                     
        Console.WriteLine("Longest common prefix: " + longestCommonPrefix(arr))
	End Sub
End Module




' run:
'
' Longest common prefix: car
'

 



answered Mar 4, 2021 by avibootz
...