코린이의 소소한 공부노트

[LeetCode/Easy] 1528. Shuffle String 본문

코딩테스트 풀이/JAVA

[LeetCode/Easy] 1528. Shuffle String

무지맘 2022. 12. 27. 00:45

1. Input

1) 문자열 s

2) [0, n-1]의 정수가 담긴 배열 indices

- n == s.length

 

2. Output

1) s의 문자들을 indices에 나온것처럼 섞었을때의 결과 문자열

 

3. Constraint

1) s.length == indices.length == n

2) 1 <= n <= 100

3) s는 영어 소문자로만 이루어져 있다.

4) 0 <= indices[i] < n

5) indices에는 중복 숫자가 없다,

 

4. Example

Input: s = "codeleet", indices = [4,5,6,7,0,2,1,3] -> Output: "leetcode"

설명:

- 0번째인 c4번째로

- 1번째인 o5번째로

- 2번째인 d6번째로

...

- 다 섞고 나면 “leetcode”가 된다.

 

5. Code

1) 첫 코드(2022/06/04)

int n = s.length();
String result = "";
for(int i=0 ; i<n ; i++){
    for(int j=0 ; j<n ; j++){
        if(indices[j] == i){
            result += s.charAt(j) + "";
            break;
        }
    } // for j
} // for i
return result;