Imports System
Imports System.Linq
Imports System.Text.RegularExpressions
Module Module1
Function LongestCommonPrefix(input As String) As String
If String.IsNullOrWhiteSpace(input) Then
Return ""
End If
' Split by non‑word characters (same as Java's split("\\W+"))
Dim words = Regex.Split(input.ToLower(), "\W+").
Where(Function(w) w.Length > 0).
ToArray()
If words.Length = 0 Then
Return ""
End If
' Start with the first word as the prefix
Dim prefix As String = words(0)
For i = 1 To words.Length - 1
While Not words(i).StartsWith(prefix)
prefix = prefix.Substring(0, prefix.Length - 1)
If prefix = "" Then
Return ""
End If
End While
Next
Return prefix
End Function
Sub Main()
Dim s1 = "The lowly inhabitants of the lowland were surprised to see the lower branches."
Console.WriteLine("LCP: '" & LongestCommonPrefix(s1) & "'")
Dim s2 = "unclear, uncertain, unexpected"
Console.WriteLine("LCP: '" & LongestCommonPrefix(s2) & "'")
End Sub
End Module
' run:
'
' LCP: ''
' LCP: 'un'
'