Sort by

recency

|

58 Discussions

|

  • + 0 comments

    swift solution

    func compress(input: String) -> String {
        var input = input
        var count = 1
        var output = ""
        for char in input {
            if let last = output.last, last == char {
                count += 1
            }else {
                if count > 1 {
                    output = output + String(count)
                    count = 1
                }
                output = output + String(char)
            }
        }
        if count > 1 {
            output = output + String(count)
        }
        return output
    }
    
  • + 0 comments

    Scala Solution using Regex

        val s: String = io.StdIn.readLine().trim
    
        val chars: Seq[String] = Seq(
          "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l",
          "m", "n", "o", "p", "q", "r", "s", "t", "u", "v", "x", "w", "y", "z"
        )
    
        val groupPattern: Regex = chars.map(c => s"($c{2,})").reduce(_ + "|" + _).r
    
        val compressed: String = groupPattern.findAllIn(s).toVector
          .sortBy(_.length * -1)
          .foldLeft(s){ (acc, group) =>
            acc.replace(group, s"${group.head}${group.length}")
          }
    
        println(compressed)
    
  • + 0 comments

    What is string compression in Java? www cricketbet9 com mahadev book

  • + 0 comments

    f# solution

    let rec compress n list =
        match list with
        | [] -> []
        | [ c ] when n = 1 -> [ c ]
        | [ c ] -> [ c; string n ]
        | x :: xs when x = List.head xs -> compress (n + 1) xs
        | x :: xs when n = 1 -> x :: compress 1 xs
        | x :: xs -> x :: string n :: compress 1 xs
    
    open System
    
    [<EntryPoint>]
    let main _ =
        Console.ReadLine()
        |> Seq.toList
        |> List.map string
        |> compress 1
        |> String.concat ""
        |> Console.WriteLine
    
        0
    
  • + 0 comments

    Haskell

    import Data.List
    
    solve :: String -> String
    solve = foldr (\x acc -> if (length x) == 1 then (head x):acc else [head x] ++ (show $ length x) ++ acc) "" . group
    
    main = interact $ solve