We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 06b495b commit 399f496Copy full SHA for 399f496
Easy/Shuffle String.java
@@ -1,9 +1,24 @@
1
class Solution {
2
public String restoreString(String s, int[] indices) {
3
- char[] letters = new char[s.length()];
+ char[] letters = s.toCharArray();
4
for (int i = 0; i < indices.length; i++) {
5
- letters[indices[i]] = s.charAt(i);
+ while (indices[i] != i) {
6
+ swapLetter(letters, i, indices[i]);
7
+ swapIndex(indices, i, indices[i]);
8
+ }
9
}
10
return String.valueOf(letters);
11
12
+
13
+ private void swapLetter(char[] arr, int idxOne, int idxTwo) {
14
+ char temp = arr[idxOne];
15
+ arr[idxOne] = arr[idxTwo];
16
+ arr[idxTwo] = temp;
17
18
19
+ private void swapIndex(int[] arr, int idxOne, int idxTwo) {
20
+ int temp = arr[idxOne];
21
22
23
24
0 commit comments