File tree Expand file tree Collapse file tree
src/test/java/LeetCode/Lexicographically_Smallest_String_After_Applying_Operations Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1+ class LC_lexsmalleststrafterop {
2+
3+ public String findLexSmallestString (String s , int a , int b ) {
4+ int n = s .length ();
5+ boolean [] vis = new boolean [n ];
6+ String res = s ;
7+ // double the length of s for convenience in extracting the rotated string t
8+ s = s + s ;
9+ for (int i = 0 ; !vis [i ]; i = (i + b ) % n ) {
10+ vis [i ] = true ;
11+ for (int j = 0 ; j < 10 ; j ++) {
12+ int kLimit = b % 2 == 0 ? 0 : 9 ;
13+ for (int k = 0 ; k <= kLimit ; k ++) {
14+ // before each accumulation, re-truncate t
15+ char [] t = s .substring (i , i + n ).toCharArray ();
16+ for (int p = 1 ; p < n ; p += 2 ) {
17+ t [p ] = (char ) ('0' + ((t [p ] - '0' + j * a ) % 10 ));
18+ }
19+ for (int p = 0 ; p < n ; p += 2 ) {
20+ t [p ] = (char ) ('0' + ((t [p ] - '0' + k * a ) % 10 ));
21+ }
22+ String tStr = new String (t );
23+ if (tStr .compareTo (res ) < 0 ) {
24+ res = tStr ;
25+ }
26+ }
27+ }
28+ }
29+ return res ;
30+ }
31+ }
You can’t perform that action at this time.
0 commit comments