Question:
Method: first establish the mapping relationship between letters and index, through the way of map, so that the complexity of the search again is O(1), and then iterate to rebuild the answer.
package com.eric.leetcode
import java.lang.StringBuilder
class ShuffleString {
fun restoreString(s: String, indices: IntArray): String {
val map = mutableMapOf<Int, Char>()
for (index in s.indices) {
map[indices[index]] = s[index]
}
val result = StringBuilder()
for (index in map.keys.indices) {
result.append(map[index])
}
return result.toString()
}
}
Copy the code
Feel free to communicate if you have any questions
Specific code implementation can refer to Github