Skip to content

Commit 158283d

Browse files
authored
Create 899-orderly-queue.js
1 parent b7c6681 commit 158283d

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

899-orderly-queue.js

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/**
2+
* @param {string} S
3+
* @param {number} K
4+
* @return {string}
5+
*/
6+
const orderlyQueue = function (S, K) {
7+
if (K === 0) return S
8+
else if (K > 1) return S.split('').sort().join('')
9+
let result = 0,
10+
L = S.length
11+
for (let i = 1; i < L; i++) {
12+
for (let j = 0; j < L; j++) {
13+
let d = S.charCodeAt((result + j) % L) - S.charCodeAt((i + j) % L)
14+
if (d !== 0) {
15+
if (d > 0) result = i
16+
break
17+
}
18+
}
19+
}
20+
return S.slice(result) + S.slice(0, result)
21+
}

0 commit comments

Comments
 (0)