GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • LeetCodeJavascript.com
  • Star on GitHub

This view

  • Original page
  • New address

GeezerVison is showing leetcodejavascript.com — #2810 - Faulty Keyboard - LeetCode JavaScript Solutions

Back to all solutions

#2810 - Faulty Keyboard

Easy • View on LeetCode • View on GitHub
String Simulation

Problem Description

Your laptop keyboard is faulty, and whenever you type a character 'i' on it, it reverses the string that you have written. Typing other characters works as expected.

You are given a 0-indexed string s, and you type each character of s using your faulty keyboard.

Return the final string that will be present on your laptop screen.

Solution

/**
 * @param {string} s
 * @return {string}
 */
var finalString = function(s) {
  const result = [];

  for (const char of s) {
    if (char === 'i') {
      result.reverse();
    } else {
      result.push(char);
    }
  }

  return result.join('');
};
You are visitor 01402520 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.