Skip to content

Latest commit

 

History

History
58 lines (42 loc) · 1.11 KB

1528-kir3i.md

File metadata and controls

58 lines (42 loc) · 1.11 KB

1528. Shuffle String

Solution

  • 시간복잡도: O(N)

  • 알고리즘

    문자열 처리

  • 풀이설명

    새로운 문자열을 만들어서 주어진 인덱스에 알맞은 문자를 넣으면 됩니다.

  • 소스코드

    • C++

      class Solution {
      public:
          string restoreString(string s, vector<int>& indices) {
              int sz = s.size();
              char* ans = new char[sz+1];
              ans[sz] = '\0';
              for(int i=0; i<sz; i++)
                  ans[indices[i]] = s[i];
              return ans;
          }
      };
    • Python

      class Solution:
          def restoreString(self, s, indices):
              ans = [''] * len(indices)
              for i, c in zip(indices, s):
                  ans[i] = c
              return ''.join(ans)
    • Java

      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);
          }
      }