Rosetta Code

Determine if a string is squeezable

Collapse repeated adjacent characters and compare the squeezed result with a target string.

Intro View source
Source rosettacode/popular/determine_if_a_string_is_squeezable.vibe
# title: Determine if a string is squeezable
# source: https://rosettacode.org/wiki/Determine_if_a_string_is_squeezable
# category: Rosetta Code
# difficulty: Intro
# summary: Collapse repeated adjacent characters and compare the squeezed result with a target string.
# tags: popular, strings, parsing, basics
# vibe: 0.2

def squeeze(text)
  chars = text.split("")
  result = ""
  previous = nil
  index = 0

  while index < chars.size
    char = chars[index]
    if char != previous
      result = result + char
      previous = char
    end
    index = index + 1
  end

  result
end

def squeezable?(text, target)
  squeeze(text) == target
end

def run
  {
    balloon: squeezable?("baalllloooon", "balon"),
    mississippi: squeezable?("mississippi", "misisipi"),
    no_change: squeezable?("vibescript", "vibescript"),
    mismatch: squeezable?("bookkeeper", "bokeper")
  }
end
Output
Press run to execute run from this example.
rosetta-code popular strings parsing basics browser-runner