classSolution { public: string findLexSmallestString(string s, int a, int b){ int n = s.size(); vector<int> vis(n); string res = s; // 将 s 延长一倍,方便截取轮转后的字符串 t s = s + s; for (int i = 0; vis[i] == 0; i = (i + b) % n) { vis[i] = 1; for (int j = 0; j < 10; j++) { int k_limit = b % 2 == 0 ? 0 : 9; for (int k = 0; k <= k_limit; k++) { // 每次进行累加之前,重新截取 t string t = s.substr(i, n); for (int p = 1; p < n; p += 2) { t[p] = '0' + (t[p] - '0' + j * a) % 10; } for (int p = 0; p < n; p += 2) { t[p] = '0' + (t[p] - '0' + k * a) % 10; } res = min(res, t); } } } return res; } };
classSolution { public String findLexSmallestString(String s, int a, int b) { intn= s.length(); boolean[] vis = newboolean[n]; Stringres= s; // 将 s 延长一倍,方便截取轮转后的字符串 t s = s + s; for (inti=0; !vis[i]; i = (i + b) % n) { vis[i] = true; for (intj=0; j < 10; j++) { intkLimit= b % 2 == 0 ? 0 : 9; for (intk=0; k <= kLimit; k++) { // 每次进行累加之前,重新截取 t char[] t = s.substring(i, i + n).toCharArray(); for (intp=1; p < n; p += 2) { t[p] = (char) ('0' + (t[p] - '0' + j * a) % 10); } for (intp=0; p < n; p += 2) { t[p] = (char) ('0' + (t[p] - '0' + k * a) % 10); } StringtStr=newString(t); if (tStr.compareTo(res) < 0) { res = tStr; } } } } return res; } }
publicclassSolution { publicstringFindLexSmallestString(string s, int a, int b) { int n = s.Length; bool[] vis = newbool[n]; string res = s; // 将 s 延长一倍,方便截取轮转后的字符串 t s = s + s; for (int i = 0; !vis[i]; i = (i + b) % n) { vis[i] = true; for (int j = 0; j < 10; j++) { int kLimit = b % 2 == 0 ? 0 : 9; for (int k = 0; k <= kLimit; k++) { // 每次进行累加之前,重新截取 t char[] t = s.Substring(i, n).ToCharArray(); for (int p = 1; p < n; p += 2) { t[p] = (char) ('0' + (t[p] - '0' + j * a) % 10); } for (int p = 0; p < n; p += 2) { t[p] = (char) ('0' + (t[p] - '0' + k * a) % 10); } string tStr = newstring(t); if (tStr.CompareTo(res) < 0) { res = tStr; } } } } return res; } }
char * findLexSmallestString(char * s, int a, int b) { int n = strlen(s); int vis[n]; memset(vis, 0, sizeof(vis)); char *res = (char *)calloc(sizeof(char), n + 1); strcpy(res, s); // 将 s 延长一倍,方便截取轮转后的字符串 t char str[2 * n + 1]; sprintf(str, "%s%s", s, s); for (int i = 0; vis[i] == 0; i = (i + b) % n) { vis[i] = 1; for (int j = 0; j < 10; j++) { int k_limit = b % 2 == 0 ? 0 : 9; for (int k = 0; k <= k_limit; k++) { // 每次进行累加之前,重新截取 t char t[n + 1]; strncpy(t, str + i, n); t[n] = '\0'; for (int p = 1; p < n; p += 2) { t[p] = '0' + (t[p] - '0' + j * a) % 10; } for (int p = 0; p < n; p += 2) { t[p] = '0' + (t[p] - '0' + k * a) % 10; } if (strcmp(t, res) < 0) { strcpy(res, t); } } } } return res; }
var findLexSmallestString = function(s, a, b) { const n = s.length; const vis = newArray(n).fill(false); let res = s; // 将 s 延长一倍,方便截取轮转后的字符串 t s = s + s; for (let i = 0; !vis[i]; i = (i + b) % n) { vis[i] = true; for (let j = 0; j < 10; j++) { let kLimit = b % 2 === 0 ? 0 : 9; for (let k = 0; k <= kLimit; k++) { // 每次进行累加之前,重新截取 t const t = [...s.slice(i, i + n)]; for (let p = 1; p < n; p += 2) { t[p] = String.fromCharCode('0'.charCodeAt() + (t[p].charCodeAt() - '0'.charCodeAt() + j * a) % 10); } for (let p = 0; p < n; p += 2) { t[p] = String.fromCharCode('0'.charCodeAt() + (t[p].charCodeAt() - '0'.charCodeAt() + k * a) % 10); } const tStr = t.join(''); if (tStr < res) { res = tStr; } } } } return res; };
枚举轮转次数过程中,我们依次考虑了如下位置:0 \times b \pmod n,1 \times b \pmod n,2 \times b \pmod n,\cdots, x \times b\pmod n。我们可以用一个表达式来计算最终到达的位置:
classSolution { public: string findLexSmallestString(string s, int a, int b){ int n = s.size(); string res = s; s = s + s; int g = gcd(b, n); for (int i = 0; i < n; i += g) { for (int j = 0; j < 10; j++) { int k_limit = b % 2 == 0 ? 0 : 9; for (int k = 0; k <= k_limit; k++) { string t = s.substr(i, n); for (int p = 1; p < n; p += 2) { t[p] = '0' + (t[p] - '0' + j * a) % 10; } for (int p = 0; p < n; p += 2) { t[p] = '0' + (t[p] - '0' + k * a) % 10; } res = min(res, t); } } } return res; } };
classSolution { public String findLexSmallestString(String s, int a, int b) { intn= s.length(); Stringres= s; s = s + s; intg= gcd(b, n); for (inti=0; i < n; i += g) { for (intj=0; j < 10; j++) { intkLimit= b % 2 == 0 ? 0 : 9; for (intk=0; k <= kLimit; k++) { char[] t = s.substring(i, i + n).toCharArray(); for (intp=1; p < n; p += 2) { t[p] = (char) ('0' + (t[p] - '0' + j * a) % 10); } for (intp=0; p < n; p += 2) { t[p] = (char) ('0' + (t[p] - '0' + k * a) % 10); } StringtStr=newString(t); if (tStr.compareTo(res) < 0) { res = tStr; } } } } return res; }
publicclassSolution { publicstringFindLexSmallestString(string s, int a, int b) { int n = s.Length; string res = s; s = s + s; int g = GCD(b, n); for (int i = 0; i < n; i += g) { for (int j = 0; j < 10; j++) { int kLimit = b % 2 == 0 ? 0 : 9; for (int k = 0; k <= kLimit; k++) { char[] t = s.Substring(i, n).ToCharArray(); for (int p = 1; p < n; p += 2) { t[p] = (char) ('0' + (t[p] - '0' + j * a) % 10); } for (int p = 0; p < n; p += 2) { t[p] = (char) ('0' + (t[p] - '0' + k * a) % 10); } string tStr = newstring(t); if (tStr.CompareTo(res) < 0) { res = tStr; } } } } return res; }
publicintGCD(int num1, int num2) { while (num2 != 0) { int temp = num1; num1 = num2; num2 = temp % num2; } return num1; } }
intgcd(int num1, int num2) { while (num2 != 0) { int temp = num1; num1 = num2; num2 = temp % num2; } return num1; }
char * findLexSmallestString(char * s, int a, int b) { int n = strlen(s); int vis[n]; memset(vis, 0, sizeof(vis)); char *res = (char *)calloc(sizeof(char), n + 1); strcpy(res, s); // 将 s 延长一倍,方便截取轮转后的字符串 t char str[2 * n + 1]; sprintf(str, "%s%s", s, s); int g = gcd(b, n); for (int i = 0; i < n; i += g) { vis[i] = 1; for (int j = 0; j < 10; j++) { int k_limit = b % 2 == 0 ? 0 : 9; for (int k = 0; k <= k_limit; k++) { // 每次进行累加之前,重新截取 t char t[n + 1]; strncpy(t, str + i, n); t[n] = '\0'; for (int p = 1; p < n; p += 2) { t[p] = '0' + (t[p] - '0' + j * a) % 10; } for (int p = 0; p < n; p += 2) { t[p] = '0' + (t[p] - '0' + k * a) % 10; } if (strcmp(t, res) < 0) { strcpy(res, t); } } } } return res; }
var findLexSmallestString = function(s, a, b) { const n = s.length; let res = s; s = s + s; const g = gcd(b, n); for (let i = 0; i < n; i += g) { for (let j = 0; j < 10; j++) { const kLimit = b % 2 === 0 ? 0 : 9; for (let k = 0; k <= kLimit; k++) { const t = [...s.slice(i, i + n)]; for (let p = 1; p < n; p += 2) { t[p] = String.fromCharCode('0'.charCodeAt() + (t[p].charCodeAt() - '0'.charCodeAt() + j * a) % 10); } for (let p = 0; p < n; p += 2) { t[p] = String.fromCharCode('0'.charCodeAt() + (t[p].charCodeAt() - '0'.charCodeAt() + k * a) % 10); } const tStr = t.join(''); if (tStr < res) { res = tStr; } } } } return res; }
classSolution { public: string findLexSmallestString(string s, int a, int b){ int n = s.size(); string res = s; s = s + s; int g = gcd(b, n); auto add = [&](string& t, int start) { int minVal = 10, times = 0; for (int i = 0; i < 10; i++) { int added = ((t[start] - '0') + i * a) % 10; if (added < minVal) { minVal = added; times = i; } } for (int i = start; i < n; i += 2) { t[i] = '0' + ((t[i] - '0') + times * a) % 10; } };
for (int i = 0; i < n; i += g) { string t = s.substr(i, n); add(t, 1); if (b % 2) { add(t, 0); } res = min(res, t); } return res; } };
classSolution { public String findLexSmallestString(String s, int a, int b) { intn= s.length(); Stringres= s; s = s + s; intg= gcd(b, n);
for (inti=0; i < n; i += g) { char[] t = s.substring(i, i + n).toCharArray(); add(t, n, a, 1); if (b % 2 != 0) { add(t, n, a, 0); } StringtStr=newString(t); if (tStr.compareTo(res) < 0) { res = tStr; } } return res; }
publicvoidadd(char[] t, int n, int a, int start) { intminVal=10, times = 0; for (inti=0; i < 10; i++) { intadded= ((t[start] - '0') + i * a) % 10; if (added < minVal) { minVal = added; times = i; } } for (inti= start; i < n; i += 2) { t[i] = (char) ('0' + ((t[i] - '0') + times * a) % 10); } }
publicclassSolution { publicstringFindLexSmallestString(string s, int a, int b) { int n = s.Length; string res = s; s = s + s; int g = GCD(b, n);
for (int i = 0; i < n; i += g) { char[] t = s.Substring(i, n).ToCharArray(); Add(t, n, a, 1); if (b % 2 != 0) { Add(t, n, a, 0); } string tStr = newstring(t); if (tStr.CompareTo(res) < 0) { res = tStr; } } return res; }
publicvoidAdd(char[] t, int n, int a, int start) { int minVal = 10, times = 0; for (int i = 0; i < 10; i++) { int added = ((t[start] - '0') + i * a) % 10; if (added < minVal) { minVal = added; times = i; } } for (int i = start; i < n; i += 2) { t[i] = (char) ('0' + ((t[i] - '0') + times * a) % 10); } }
publicintGCD(int num1, int num2) { while (num2 != 0) { int temp = num1; num1 = num2; num2 = temp % num2; } return num1; } }
intgcd(int num1, int num2) { while (num2 != 0) { int temp = num1; num1 = num2; num2 = temp % num2; } return num1; }
voidadd(char *t, int n, int a, int start) { int minVal = 10, times = 0; for (int i = 0; i < 10; i++) { int added = ((t[start] - '0') + i * a) % 10; if (added < minVal) { minVal = added; times = i; } } for (int i = start; i < n; i += 2) { t[i] = '0' + ((t[i] - '0') + times * a) % 10; } };
char * findLexSmallestString(char * s, int a, int b) { int n = strlen(s); int vis[n]; memset(vis, 0, sizeof(vis)); char *res = (char *)calloc(sizeof(char), n + 1); strcpy(res, s); // 将 s 延长一倍,方便截取轮转后的字符串 t char str[2 * n + 1]; sprintf(str, "%s%s", s, s); int g = gcd(b, n); for (int i = 0; i < n; i += g) { char t[n + 1]; strncpy(t, str + i, n); t[n] = '\0'; add(t, n, a, 1); if (b % 2 != 0) { add(t, n, a, 0); } if (strcmp(t, res) < 0) { strcpy(res, t); } } return res; }
var findLexSmallestString = function(s, a, b) { const n = s.length; let res = s; s = s + s; const g = gcd(b, n);
for (let i = 0; i < n; i += g) { const t = [...s.slice(i, i + n)]; add(t, n, a, 1); if (b % 2 !== 0) { add(t, n, a, 0); } const tStr = t.join(''); if (tStr < res) { res = tStr; } } return res; }
constadd = (t, n, a, start) => { let minVal = 10, times = 0; for (let i = 0; i < 10; i++) { const added = ((t[start].charCodeAt() - '0'.charCodeAt()) + i * a) % 10; if (added < minVal) { minVal = added; times = i; } } for (let i = start; i < n; i += 2) { t[i] = String.fromCharCode('0'.charCodeAt() + ((t[i].charCodeAt() - '0'.charCodeAt()) + times * a) % 10); } }