Skip to content

Commit 7485378

Browse files
authored
Update 1625-lexicographically-smallest-string-after-applying-operations.js
1 parent ee51d06 commit 7485378

File tree

1 file changed

+48
-0
lines changed

1 file changed

+48
-0
lines changed

1625-lexicographically-smallest-string-after-applying-operations.js

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,51 @@
1+
/**
2+
* @param {string} s
3+
* @param {number} a
4+
* @param {number} b
5+
* @return {string}
6+
*/
7+
const findLexSmallestString = function(s, a, b) {
8+
let res = s;
9+
const n = s.length;
10+
11+
let evenLimit = 10;
12+
if (b % 2 === 0) evenLimit = 1;
13+
14+
for (let i = 0; i < evenLimit; i++) {
15+
for (let j = 0; j < 10; j++) {
16+
let t = s.split('');
17+
18+
for (let k = 0; k < n; k += 2) {
19+
t[k] = (parseInt(t[k]) + a * i) % 10;
20+
}
21+
for (let k = 1; k < n; k += 2) {
22+
t[k] = (parseInt(t[k]) + a * j) % 10;
23+
}
24+
25+
t = t.join('');
26+
let p = t;
27+
const gcdValue = gcd(n, b);
28+
for (let k = 0; k <= n / gcdValue; k++) {
29+
p = p.slice(n - b) + p.slice(0, n - b);
30+
res = res < p ? res : p;
31+
}
32+
}
33+
}
34+
35+
return res;
36+
};
37+
38+
39+
function gcd(x, y) {
40+
while (y !== 0) {
41+
let temp = y;
42+
y = x % y;
43+
x = temp;
44+
}
45+
return x;
46+
}
47+
// another
48+
149
/**
250
* @param {string} s
351
* @param {number} a

0 commit comments

Comments
 (0)