Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit b6173b9

Browse files
committed
Add solution #1625
1 parent a12e2e0 commit b6173b9

File tree

2 files changed

+62
-1
lines changed

2 files changed

+62
-1
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# 1,429 LeetCode solutions in JavaScript
1+
# 1,430 LeetCode solutions in JavaScript
22

33
[https://leetcodejavascript.com](https://leetcodejavascript.com)
44

@@ -1253,6 +1253,7 @@
12531253
1621|[Number of Sets of K Non-Overlapping Line Segments](./solutions/1621-number-of-sets-of-k-non-overlapping-line-segments.js)|Medium|
12541254
1622|[Fancy Sequence](./solutions/1622-fancy-sequence.js)|Hard|
12551255
1624|[Largest Substring Between Two Equal Characters](./solutions/1624-largest-substring-between-two-equal-characters.js)|Easy|
1256+
1625|[Lexicographically Smallest String After Applying Operations](./solutions/1625-lexicographically-smallest-string-after-applying-operations.js)|Medium|
12561257
1657|[Determine if Two Strings Are Close](./solutions/1657-determine-if-two-strings-are-close.js)|Medium|
12571258
1668|[Maximum Repeating Substring](./solutions/1668-maximum-repeating-substring.js)|Easy|
12581259
1669|[Merge In Between Linked Lists](./solutions/1669-merge-in-between-linked-lists.js)|Medium|
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/**
2+
* 1625. Lexicographically Smallest String After Applying Operations
3+
* https://leetcode.com/problems/lexicographically-smallest-string-after-applying-operations/
4+
* Difficulty: Medium
5+
*
6+
* You are given a string s of even length consisting of digits from 0 to 9, and two integers a
7+
* and b.
8+
*
9+
* You can apply either of the following two operations any number of times and in any order on s:
10+
* - Add a to all odd indices of s (0-indexed). Digits post 9 are cycled back to 0. For example,
11+
* if s = "3456" and a = 5, s becomes "3951".
12+
* - Rotate s to the right by b positions. For example, if s = "3456" and b = 1, s becomes "6345".
13+
*
14+
* Return the lexicographically smallest string you can obtain by applying the above operations any
15+
* number of times on s.
16+
*
17+
* A string a is lexicographically smaller than a string b (of the same length) if in the first
18+
* position where a and b differ, string a has a letter that appears earlier in the alphabet than
19+
* the corresponding letter in b. For example, "0158" is lexicographically smaller than "0190"
20+
* because the first position they differ is at the third letter, and '5' comes before '9'.
21+
*/
22+
23+
/**
24+
* @param {string} s
25+
* @param {number} a
26+
* @param {number} b
27+
* @return {string}
28+
*/
29+
var findLexSmallestString = function(s, a, b) {
30+
const visited = new Set();
31+
let smallestString = s;
32+
const queue = [s];
33+
34+
const addOperation = str => {
35+
const chars = str.split('');
36+
for (let i = 1; i < str.length; i += 2) {
37+
chars[i] = String((parseInt(chars[i]) + a) % 10);
38+
}
39+
return chars.join('');
40+
};
41+
42+
const rotateOperation = str => {
43+
return str.slice(-b) + str.slice(0, -b);
44+
};
45+
46+
while (queue.length) {
47+
const current = queue.shift();
48+
if (visited.has(current)) continue;
49+
visited.add(current);
50+
if (current < smallestString) smallestString = current;
51+
52+
const added = addOperation(current);
53+
const rotated = rotateOperation(current);
54+
55+
queue.push(added);
56+
queue.push(rotated);
57+
}
58+
59+
return smallestString;
60+
};

0 commit comments

Comments
 (0)