-
Notifications
You must be signed in to change notification settings - Fork 50
/
Copy path1625. Lexicographically Smallest String After Applying Operations Medium.cpp
166 lines (135 loc) · 4.28 KB
/
1625. Lexicographically Smallest String After Applying Operations Medium.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
//dfs
//but the traverse is incomplete
//WA
//43 / 80 test cases passed.
/*
"31"
4
1
Expected: "11"
*/
class Solution {
public:
int n;
unordered_set<string> visited;
unordered_map<string, string> memo;
string add(string& s, int a){
string ret = s;
for(int i = 1; i < n; i+=2){
ret[i] = '0'+(ret[i]-'0'+a)%10;
}
return ret;
}
string rotate(string& s, int b){
string ret = s;
ret = s.substr(b) + s.substr(0, b);
return ret;
}
string dfs(string& s, int& a, int& b){
if(memo.find(s) != memo.end()){
// cout << "memo" << endl;
return memo[s];
}else if(visited.find(s) != visited.end()){
//to jump out of the loop!
return "aaaa";
}else{
visited.insert(s);
string sadd = add(s, a);
string srot = rotate(s, b);
// cout << s << ", add: " << sadd << ", rot: " << srot << endl;
//stop condition
if(sadd >= s && srot >= s){
memo[s] = s;
// cout << s << " is minimum" << endl;
return memo[s];
}
memo[s] = min(dfs(sadd, a, b), dfs(srot, a, b));
// cout << s << " -> " << memo[s] << endl;
return memo[s];
}
}
string findLexSmallestString(string s, int a, int b) {
n = s.size();
return dfs(s, a, b);
}
};
//dfs, corrected from above
//Runtime: 592 ms, faster than 29.07% of C++ online submissions for Lexicographically Smallest String After Applying Operations.
//Memory Usage: 169.2 MB, less than 5.03% of C++ online submissions for Lexicographically Smallest String After Applying Operations.
class Solution {
public:
int n;
unordered_set<string> visited;
unordered_map<string, string> memo;
string add(string& s, int a){
string ret = s;
for(int i = 1; i < n; i+=2){
ret[i] = '0'+(ret[i]-'0'+a)%10;
}
return ret;
}
string rotate(string& s, int b){
string ret = s;
ret = s.substr(b) + s.substr(0, b);
return ret;
}
string dfs(string& s, int& a, int& b){
if(memo.find(s) != memo.end()){
// cout << "memo" << endl;
return memo[s];
}else if(visited.find(s) != visited.end()){
//to jump out of the loop!
return "aaaa";
}else{
visited.insert(s);
string sadd = add(s, a);
string srot = rotate(s, b);
// cout << s << ", add: " << sadd << ", rot: " << srot << endl;
memo[s] = min({s, dfs(sadd, a, b), dfs(srot, a, b)});
// cout << s << " -> " << memo[s] << endl;
return memo[s];
}
}
string findLexSmallestString(string s, int a, int b) {
n = s.size();
return dfs(s, a, b);
}
};
//dfs
//https://leetcode.com/problems/lexicographically-smallest-string-after-applying-operations/discuss/899489/Basic-DFS-Brute-force-or-This-kind-of-questions-DFS
//no early stop make it correct?
//Runtime: 308 ms, faster than 71.01% of C++ online submissions for Lexicographically Smallest String After Applying Operations.
//Memory Usage: 91.9 MB, less than 5.13% of C++ online submissions for Lexicographically Smallest String After Applying Operations.
class Solution {
public:
int n, a, b;
unordered_set<string> visited;
string ans;
string add(string& s){
string ret = s;
for(int i = 1; i < n; i+=2){
ret[i] = '0'+(ret[i]-'0'+a)%10;
}
return ret;
}
string rotate(string& s){
string ret = s;
ret = s.substr(b) + s.substr(0, b);
return ret;
}
void dfs(string s){
if(visited.find(s) != visited.end()) return;
visited.insert(s);
ans = min(ans, s);
dfs(add(s));
dfs(rotate(s));
}
string findLexSmallestString(string s, int a, int b) {
n = s.size();
this->a = a;
this->b = b;
ans = "aaaa";
dfs(s);
return ans;
}
};