
✨ 문제
Shuffle String
You are given a string s and an integer array indices of the same length.
The string s will be shuffled such that the character at the ith position moves to indices[i] in the shuffled string.
Return the shuffled string.
https://leetcode.com/problems/shuffle-string/description/
✨ 최종코드
class Solution {
public String restoreString(String s, int[] indices) {
char[] answer = new char[indices.length];
for (int i = 0; i < indices.length; i++)
answer[indices[i]] = s.charAt(i);
return new String(answer);
}
}
문제는 문자열 s와 s와 길이가 같은 정수형 배열 indices가 주어진다. 문자열 s를 배열 indices의 값에 맞춰 재배열하여 반환하는 문제이다. s = "codeleet", indices = [4, 5, 6, 7, 0, 2, 1, 3] 이면 "leetcode"를 반환하게 되는 것이다.
s.charAt(i)가 indices[i]에 위치해야 하므로 answer[indices[i]]에 s.charAt(i)를 넣어주었다.
마지막으로 재배열된 answer를 String으로 변환하여 반환하였다.
'알고리즘 > 99클럽' 카테고리의 다른 글

✨ 문제
Shuffle String
You are given a string s and an integer array indices of the same length.
The string s will be shuffled such that the character at the ith position moves to indices[i] in the shuffled string.
Return the shuffled string.
https://leetcode.com/problems/shuffle-string/description/
✨ 최종코드
class Solution { public String restoreString(String s, int[] indices) { char[] answer = new char[indices.length]; for (int i = 0; i < indices.length; i++) answer[indices[i]] = s.charAt(i); return new String(answer); } }
문제는 문자열 s와 s와 길이가 같은 정수형 배열 indices가 주어진다. 문자열 s를 배열 indices의 값에 맞춰 재배열하여 반환하는 문제이다. s = "codeleet", indices = [4, 5, 6, 7, 0, 2, 1, 3] 이면 "leetcode"를 반환하게 되는 것이다.
s.charAt(i)가 indices[i]에 위치해야 하므로 answer[indices[i]]에 s.charAt(i)를 넣어주었다.
마지막으로 재배열된 answer를 String으로 변환하여 반환하였다.