Maximum number of characters using keystrokes A, Ctrl+A, Ctrl+C and Ctrl+V

前端 未结 14 1497
忘掉有多难
忘掉有多难 2020-12-07 07:03

This is an interview question from google. I am not able to solve it by myself. Can somebody shed some light?

Write a program to print the sequence of keystrokes suc

14条回答
  •  佛祖请我去吃肉
    2020-12-07 07:37

    Here's how I would approach it:

    • assume CtrlA = select all
    • assume CtrlC = copy selection
    • assume CtrlV = paste copied selection

    given some text, it takes 4 keystrokes to duplicate it:

    • CtrlA to select it all
    • CtrlC to copy it
    • CtrlV to paste (this will paste over the selection - STATE YOUR ASSUMPTIONS)
    • CtrlV to paste again which doubles it.

    From there, you can consider doing 4 or 5 A's, then looping through the above. Note that doing ctrl + a, c, v, v will grow your text exponentially as you loop through. If remaining strokes < 4, just keep doing a CtrlV

    The key to interviews @ places like Google is to state your assumptions, and communicate your thinking. they want to know how you solve problems.

提交回复
热议问题