Created
August 18, 2020 20:34
-
-
Save bparanj/fffeb0ceddd9c07fec7f24ff249a0acd to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# @param {String} s | |
# @param {Integer[]} indices | |
# @return {String} | |
def restore_string(s, indices) | |
result = '' * indices.size | |
indices.each_with_index do |item, index| | |
result[item] = s[index] | |
end | |
result | |
end |
A bit confusing, cleanup:
def restore_string(s, indices)
result = '0' * (s.size)
indices.each_with_index do |target_index, index|
result[target_index] = s[index]
end
result
end
It's easy to get confused. The indices where the string needs to be copied is provided, but the index where we need to copy the character from the given string is different.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Without creating a string of the required size, you will get an error.