개발 일지
1528. Shuffle String (문자열 셔플) 본문
동일한 길이의 문자열 및 정수 배열 인덱스가 제공됩니다. ith 위치에 있는 문자가 shuffled string의 인덱스 [i]로 이동하도록 문자열이 shuffled됩니다.
섞였던 문자열을 반환합니다.
예시 1:

입력: s = "codelet", indices = [4,5,6,7,0,2,1,3]
출력: "leetcode"
설명: 그림과 같이 "codelet"은 셔플링 후 "leetcode"가 됩니다.
예시 2:
입력: s = "s", 인덱스 = [0,1,2]
출력: "abc"
설명: 뒤죽박죽이 된 후에도 각 캐릭터는 제자리를 유지합니다.
조건:
- s.length == index.length == n
- 1 <= n <= 100
- s는 영어 소문자로만 구성됩니다.
- 0 < = 인덱스 [i] < n
- 인덱스의 모든 값은 고유합니다.
정답:
public class Solution {
public string RestoreString(string s, int[] indices) {
char[] result = new char[s.Length];
for(int i = 0; i < result.Length; i++)
{
result[indices[i]] = s[i];
}
return new string(result);
}
}
해설: indices의 길이만큼 char 리스트를 만든 후 indices의 위치대로 넣어 준 후 문자열로 return해주었습니다.
'코딩 테스트 > LeetCode' 카테고리의 다른 글
896. Monotonic Array (단조로운 배열) (0) | 2023.10.26 |
---|---|
507. Perfect Number (완벽한 수) (0) | 2023.10.26 |
1689. Partitioning Into Minimum Number Of Deci-Binary Numbers (최소 십진수 이진수로 분할) (0) | 2023.06.07 |
2248. Intersection of Multiple Arrays (다중 배열 교차점) (0) | 2023.06.07 |
136. Single Number (단일 번호) (0) | 2023.06.04 |