Skip to content

Commit 7e90579

Browse files
solves #3210: Find the Encrypted String in java
1 parent bc2aacb commit 7e90579

File tree

3 files changed

+23
-2
lines changed

3 files changed

+23
-2
lines changed

Diff for: README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -925,7 +925,7 @@
925925
| 3199 | 🔒 [Count Triplets With Even XOR Set Bits I](https://leetcode.com/problems/count-triplets-with-even-xor-set-bits-i) | | |
926926
| 3200 | [Maximum Height of a Triangle](https://leetcode.com/problems/maximum-height-of-a-triangle) | [![Java](assets/java.png)](src/MaximumHeightOfATriangle.java) | |
927927
| 3206 | [Alternating Groups I](https://leetcode.com/problems/alternating-groups-i) | [![Java](assets/java.png)](src/AlternatingGroupsI.java) | |
928-
| 3210 | [Find the Encrypted String](https://leetcode.com/problems/find-the-encrypted-string) | | |
928+
| 3210 | [Find the Encrypted String](https://leetcode.com/problems/find-the-encrypted-string) | [![Java](assets/java.png)](src/FindTheEncryptedString.java) | |
929929
| 3216 | [Lexicographically Smallest String After a Swap](https://leetcode.com/problems/lexicographically-smallest-string-after-a-swap) | | |
930930
| 3222 | [Find the Winning Player in Coin Game](https://leetcode.com/problems/find-the-winning-player-in-coin-game) | | |
931931
| 3226 | [Number of Bit Changes to Make Two Integers Equal](https://leetcode.com/problems/number-of-bit-changes-to-make-two-integers-equal) | | |

Diff for: src/AlternatingGroupsI.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,6 @@ public int numberOfAlternatingGroups(int[] colors) {
1414
}
1515
private static boolean isAlternating(int[] colors, int startIndex) {
1616
return colors[startIndex] == colors[(startIndex + 2) % colors.length] &&
17-
colors[startIndex] != colors[(startIndex + 1) % colors.length]
17+
colors[startIndex] != colors[(startIndex + 1) % colors.length];
1818
}
1919
}

Diff for: src/FindTheEncryptedString.java

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// https://leetcode.com/problems/find-the-encrypted-string
2+
// T: O(|s|)
3+
// S: O(|s|)
4+
5+
public class FindTheEncryptedString {
6+
public String getEncryptedString(String s, int k) {
7+
final int rotations = k % s.length();
8+
if (rotations == 0) {
9+
return s;
10+
}
11+
return rotateString(s, rotations);
12+
}
13+
14+
private static String rotateString(String s, int rotations) {
15+
final StringBuilder builder = new StringBuilder();
16+
for (int i = 0 ; i < s.length() ; i++) {
17+
builder.append(s.charAt((i + rotations) % s.length()));
18+
}
19+
return builder.toString();
20+
}
21+
}

0 commit comments

Comments
 (0)