Skip to content

Commit ed3c202

Browse files
committed
New Problem Solution -"Lexicographically Smallest String After Applying Operations"
1 parent f34d0ff commit ed3c202

File tree

2 files changed

+107
-0
lines changed

2 files changed

+107
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ LeetCode
5050
|1734|[Decode XORed Permutation](https://leetcode.com/problems/decode-xored-permutation/) | [C++](./algorithms/cpp/decodeXORedPermutation/DecodeXoredPermutation.cpp)|Medium|
5151
|1733|[Minimum Number of People to Teach](https://leetcode.com/problems/minimum-number-of-people-to-teach/) | [C++](./algorithms/cpp/minimumNumberOfPeopleToTeach/MinimumNumberOfPeopleToTeach.cpp)|Medium|
5252
|1732|[Find the Highest Altitude](https://leetcode.com/problems/find-the-highest-altitude/) | [C++](./algorithms/cpp/findTheHighestAltitude/FindTheHighestAltitude.cpp)|Easy|
53+
|1625|[Lexicographically Smallest String After Applying Operations](https://leetcode.com/problems/lexicographically-smallest-string-after-applying-operations/) | [C++](./algorithms/cpp/lexicographicallySmallestStringAfterApplyingOperations/LexicographicallySmallestStringAfterApplyingOperations.cpp)|Medium|
5354
|1624|[Largest Substring Between Two Equal Characters](https://leetcode.com/problems/largest-substring-between-two-equal-characters/) | [C++](./algorithms/cpp/largestSubstringBetweenTwoEqualCharacters/LargestSubstringBetweenTwoEqualCharacters.cpp)|Easy|
5455
|1605|[Find Valid Matrix Given Row and Column Sums](https://leetcode.com/problems/find-valid-matrix-given-row-and-column-sums/) | [C++](./algorithms/cpp/FindValidMatrixGivenRowAndColumnSums/FindValidMatrixGivenRowAndColumnSums.cpp)|Medium|
5556
|1573|[Number of Ways to Split a String](https://leetcode.com/problems/number-of-ways-to-split-a-string/) | [C++](./algorithms/cpp/NumberOfWaysToSplitString/NumberOfWaysToSplitString.cpp)|Medium|
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
// Source : https://leetcode.com/problems/lexicographically-smallest-string-after-applying-operations/
2+
// Author : Hao Chen
3+
// Date : 2021-03-24
4+
5+
/*****************************************************************************************************
6+
*
7+
* You are given a string s of even length consisting of digits from 0 to 9, and two integers a and b.
8+
*
9+
* You can apply either of the following two operations any number of times and in any order on s:
10+
*
11+
* Add a to all odd indices of s (0-indexed). Digits post 9 are cycled back to 0. For example,
12+
* if s = "3456" and a = 5, s becomes "3951".
13+
* Rotate s to the right by b positions. For example, if s = "3456" and b = 1, s becomes
14+
* "6345".
15+
*
16+
* Return the lexicographically smallest string you can obtain by applying the above operations any
17+
* number of times on s.
18+
*
19+
* A string a is lexicographically smaller than a string b (of the same length) if in the first
20+
* position where a and b differ, string a has a letter that appears earlier in the alphabet than the
21+
* corresponding letter in b. For example, "0158" is lexicographically smaller than "0190" because the
22+
* first position they differ is at the third letter, and '5' comes before '9'.
23+
*
24+
* Example 1:
25+
*
26+
* Input: s = "5525", a = 9, b = 2
27+
* Output: "2050"
28+
* Explanation: We can apply the following operations:
29+
* Start: "5525"
30+
* Rotate: "2555"
31+
* Add: "2454"
32+
* Add: "2353"
33+
* Rotate: "5323"
34+
* Add: "5222"
35+
* ​​​​​​​Add: "5121"
36+
* ​​​​​​​Rotate: "2151"
37+
* ​​​​​​​Add: "2050"​​​​​​​​​​​​
38+
* There is no way to obtain a string that is lexicographically smaller then "2050".
39+
*
40+
* Example 2:
41+
*
42+
* Input: s = "74", a = 5, b = 1
43+
* Output: "24"
44+
* Explanation: We can apply the following operations:
45+
* Start: "74"
46+
* Rotate: "47"
47+
* ​​​​​​​Add: "42"
48+
* ​​​​​​​Rotate: "24"​​​​​​​​​​​​
49+
* There is no way to obtain a string that is lexicographically smaller then "24".
50+
*
51+
* Example 3:
52+
*
53+
* Input: s = "0011", a = 4, b = 2
54+
* Output: "0011"
55+
* Explanation: There are no sequence of operations that will give us a lexicographically smaller
56+
* string than "0011".
57+
*
58+
* Example 4:
59+
*
60+
* Input: s = "43987654", a = 7, b = 3
61+
* Output: "00553311"
62+
*
63+
* Constraints:
64+
*
65+
* 2 <= s.length <= 100
66+
* s.length is even.
67+
* s consists of digits from 0 to 9 only.
68+
* 1 <= a <= 9
69+
* 1 <= b <= s.length - 1
70+
******************************************************************************************************/
71+
72+
class Solution {
73+
private:
74+
unordered_map<string, bool> processed;
75+
void rotate_str(string& s, int n) {
76+
std::rotate(s.begin(), s.begin()+n, s.end());
77+
}
78+
void add_str(string& s, int n) {
79+
for(int i=1; i<s.size(); i+=2) {
80+
s[i] = (s[i] -'0' + n) % 10 +'0';
81+
}
82+
}
83+
public:
84+
string findLexSmallestString(string s, int a, int b) {
85+
string result = s;
86+
dfs(s, a, b, result);
87+
return result;
88+
}
89+
90+
void dfs(string& s, int a, int b, string& result) {
91+
92+
if (processed.find(s) != processed.end()) return;
93+
processed[s] = true;
94+
95+
if (s < result) result = s;
96+
97+
string str = s;
98+
rotate_str(str, b);
99+
dfs(str, a, b, result);
100+
101+
str = s;
102+
add_str(str, a);
103+
dfs(str, a, b, result);
104+
105+
}
106+
};

0 commit comments

Comments
 (0)