How to remove all adjacent duplicate characters from a string until no more can be removed in VB.NET

1 Answer

0 votes
Imports System
				
Module Module1

    Function RemoveAdjacentDuplicates(s As String) As String
        Dim stack As New System.Text.StringBuilder()

        For Each ch As Char In s
            Dim n As Integer = stack.Length

            If n > 0 AndAlso stack(n - 1) = ch Then
                stack.Remove(n - 1, 1)   ' pop
            Else
                stack.Append(ch)         ' push
            End If
        Next

        Return stack.ToString()
    End Function

    Sub Main()
        Dim s As String = "abbacccada"
	
        Console.WriteLine(RemoveAdjacentDuplicates(s))  
    End Sub

End Module



' run
'
' cada
'

 



answered Mar 7 by avibootz

Related questions

...