<문제 1528. Shuffle String>
[문제]
번역 :
[문자열 s와 길이가 같은 정수 배열 인덱스가 주어집니다.
문자열 s는 ith 위치에 있는 문자가 셔플된 문자열의 indices[i]로 이동하도록 셔플됩니다. 해당 문자열 s를 반환하세요.]
→ 예시에서 보듯이, String s = "codeleet" / int[] indices = {4,5,6,7,0,2,1,3}일 경우 0번 인덱스에 해당하는 문자 "l", 1번 인덱스에 해당하는 문자 "e"...등등 반복하여 문자를 indices의 인덱스에 맞게 조합해주면 되는 문제
[답안]
class Solution {
public String restoreString(String s, int[] indices) {
char[] ans = new char[indices.length];
for (int i = 0; i < indices.length; i++) {
ans[indices[i]] = s.charAt(i);
}
return String.valueOf(ans);
}
public static void main(String[] args) {
Solution sol = new Solution();
String s = "codeleet";
int[] indices = {4, 5, 6, 7, 0, 2, 1, 3};
System.out.println(sol.restoreString(s, indices));
}
}
출처
https://leetcode.com/problems/shuffle-string/description/
Shuffle String - LeetCode
Can you solve this real interview question? 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. Ret
leetcode.com
'알고리즘 풀이(JAVA) > leetcode' 카테고리의 다른 글
[leetcode] 1816. Truncate Sentence (java) (0) | 2023.04.21 |
---|---|
[leetcode] 1281. Subtract the Product and Sum of Digits of an Integer (java) (0) | 2023.03.30 |
[leetcode] 1678. Goal Parser Interpretation (java) (0) | 2023.03.22 |
[leetcode] 1365. How Many Numbers Are Smaller Than the Current Number (java) (2) | 2023.03.14 |
[leetcode] 1431. Kids With the Greatest Number of Candies (java) (2) | 2023.03.11 |