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 — #899 - Orderly Queue - LeetCode JavaScript Solutions

Back to all solutions

#899 - Orderly Queue

Hard • View on LeetCode • View on GitHub
String Math Sorting

Problem Description

You are given a string s and an integer k. You can choose one of the first k letters of s and append it at the end of the string.

Return the lexicographically smallest string you could have after applying the mentioned step any number of moves.

Solution

/**
 * @param {string} s
 * @param {number} k
 * @return {string}
 */
var orderlyQueue = function(s, k) {
  if (k === 1) {
    let smallest = s;
    for (let i = 0; i < s.length; i++) {
      const rotated = s.slice(i) + s.slice(0, i);
      if (rotated < smallest) smallest = rotated;
    }
    return smallest;
  }
  return [...s].sort().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.