How to compress and decompress repeated string characters by storing the repeated length (RLE) in VB.NET

1 Answer

0 votes
Imports System
Imports System.Text

' ---------------------------------------------------------
'   Compress: turn "aaabbcccc" into "a3b2c4"
'   This is run-length encoding (RLE)
' ---------------------------------------------------------
Module RLE

    Public Function Compress(s As String) As String
        If s.Length = 0 Then Return ""   ' Edge case: empty string

        Dim outStr As New StringBuilder()   ' Result string
        Dim count As Integer = 1            ' Count of repeated characters

        ' Start from index 1 and compare with previous character
        For i As Integer = 1 To s.Length

            ' If still repeating the same character, increase count
            If i < s.Length AndAlso s(i) = s(i - 1) Then
                count += 1
            Else
                ' Character changed OR reached end of string
                outStr.Append(s(i - 1))      ' Add the character
                outStr.Append(count)         ' Add how many times it repeated
                count = 1                    ' Reset counter
            End If
        Next

        Return outStr.ToString()
    End Function

    ' ---------------------------------------------------------
    '   Decompress: turn "a3b2c4" into "aaabbcccc"
    '   Reads a letter, then reads digits, expands them.
    ' ---------------------------------------------------------
    Public Function Decompress(s As String) As String
        Dim outStr As New StringBuilder()   ' Result string
        Dim currentChar As Char = Convert.ToChar(0)   ' The character being processed
        Dim number As New StringBuilder()   ' Digits representing the count

        For Each c As Char In s

            If Char.IsLetter(c) Then        ' Found a letter
                ' If we already have a previous letter + number, expand it
                If currentChar <> Convert.ToChar(0) AndAlso number.Length > 0 Then
                    Dim count As Integer = Integer.Parse(number.ToString())
                    outStr.Append(New String(currentChar, count))
                End If

                currentChar = c             ' Start new character
                number.Clear()              ' Reset number buffer

            ElseIf Char.IsDigit(c) Then     ' Found a digit
                number.Append(c)            ' Build multi-digit number
            End If
        Next

        ' Handle the last character + number pair
        If currentChar <> Convert.ToChar(0) AndAlso number.Length > 0 Then
            Dim count As Integer = Integer.Parse(number.ToString())
            outStr.Append(New String(currentChar, count))
        End If

        Return outStr.ToString()
    End Function

    Sub Main()
        Dim s As String = "wwwwwwwwwwwwbwwwwwwwwwwbbbwwwwwwccccwwwwwwwwwwww"

        Dim c As String = Compress(s)
        Dim d As String = Decompress(c)

        Console.WriteLine("Original:   " & s)
        Console.WriteLine("Compressed: " & c)
        Console.WriteLine("Decompressed: " & d)
    End Sub

End Module



' run:
'
' Original:   wwwwwwwwwwwwbwwwwwwwwwwbbbwwwwwwccccwwwwwwwwwwww
' Compressed: w12b1w10b3w6c4w12
' Decompressed: wwwwwwwwwwwwbwwwwwwwwwwbbbwwwwwwccccwwwwwwwwwwww
'

 



answered 22 hours ago by avibootz

Related questions

...