Isaac Emmanuel
15 Jun 2024Elixir

Removing Stars From A String

Elixir solution for Removing Stars From A String.

The Removing Stars From A String solution is below.

Solution files

Elixir•removing-stars-from-a-string/removing-stars-from-a-string.ex
defmodule Solution do
  @spec remove_stars(s :: String.t) :: String.t
  def remove_stars(s) do
    s
    |> String.split("", trim: true)
    |> Enum.reverse()
    |> compute(0, [])
    |> Enum.join("")
  end

  defp compute([], _, ret), do: ret
  defp compute(["*" | s], stars, ret), do: compute(s, stars+1, ret)
  defp compute([c | s], 0, ret), do: compute(s, 0, [c | ret])
  defp compute([c | s], stars, ret), do: compute(s, stars-1, ret)
end