Imports System
Public Class Test
Public Shared Sub Main()
Function GetTopLevelAreas(areas As String) As String
Dim topLevelAreas = New System.Collections.Generic.List(Of String)()
Dim areasList = New System.Collections.Generic.List(Of String)()
Dim areasCollection = areas.Split(","C)
For Each area As String In areasCollection
areasList.Add(area)
Next
While areasList.Count > 0
For Each topLevelArea As String In GetCurrentTopLevelAreas(areasList)
topLevelAreas.Add(topLevelArea)
For i As Integer = areasList.Count - 1 To 0 Step -1
If areasList(i).StartsWith(topLevelArea) Then
areasList.Remove(areasList(i))
End If
Next
Next
End While
Dim str = String.Empty
For Each topLevelArea As String In topLevelAreas
str += topLevelArea + ","
Next
Return str.Remove(str.LastIndexOf(","))
End Function
Function GetCurrentTopLevelAreas(areas As System.Collections.Generic.List(Of String)) As System.Collections.Generic.List(Of String)
Dim currentTopLevelAreas = New System.Collections.Generic.List(Of String)()
Dim min = 0
For Each area As String In areas
Dim count = area.Split("\"C).Length - 1
If min = 0 Then
min = count
ElseIf count < min Then
min = count
End If
Next
For Each area As String In areas
If area.Split("\"C).Length - 1 = min Then
currentTopLevelAreas.Add(area)
End If
Next
Return currentTopLevelAreas
End Function
End Sub
End Class