{"id":7149,"date":"2020-07-25T22:38:17","date_gmt":"2020-07-26T05:38:17","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=7149"},"modified":"2020-07-25T22:46:49","modified_gmt":"2020-07-26T05:46:49","slug":"1528-shuffle-string","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/simulation\/1528-shuffle-string\/","title":{"rendered":"\u82b1\u82b1\u9171 1528. Shuffle String"},"content":{"rendered":"\n<p>Given a string&nbsp;<code>s<\/code>&nbsp;and an integer array&nbsp;<code>indices<\/code>&nbsp;of the&nbsp;<strong>same length<\/strong>.<\/p>\n\n\n\n<p>The string&nbsp;<code>s<\/code>&nbsp;will be shuffled such that the character at the&nbsp;<code>i<sup>th<\/sup><\/code>&nbsp;position moves to&nbsp;<code>indices[i]<\/code>&nbsp;in the shuffled string.<\/p>\n\n\n\n<p>Return&nbsp;<em>the shuffled string<\/em>.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/assets.leetcode.com\/uploads\/2020\/07\/09\/q1.jpg\" alt=\"\"\/><\/figure>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> s = \"codeleet\", <code>indices<\/code> = [4,5,6,7,0,2,1,3]\n<strong>Output:<\/strong> \"leetcode\"\n<strong>Explanation:<\/strong> As shown, \"codeleet\" becomes \"leetcode\" after shuffling.\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> s = \"abc\", <code>indices<\/code> = [0,1,2]\n<strong>Output:<\/strong> \"abc\"\n<strong>Explanation:<\/strong> After shuffling, each character remains in its position.\n<\/pre>\n\n\n\n<p><strong>Example 3:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> s = \"aiohn\", <code>indices<\/code> = [3,1,4,2,0]\n<strong>Output:<\/strong> \"nihao\"\n<\/pre>\n\n\n\n<p><strong>Example 4:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> s = \"aaiougrt\", <code>indices<\/code> = [4,0,2,6,7,3,1,5]\n<strong>Output:<\/strong> \"arigatou\"\n<\/pre>\n\n\n\n<p><strong>Example 5:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> s = \"art\", <code>indices<\/code> = [1,0,2]\n<strong>Output:<\/strong> \"rat\"\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>s.length == indices.length == n<\/code><\/li><li><code>1 &lt;= n &lt;= 100<\/code><\/li><li><code>s<\/code>&nbsp;contains only lower-case English letters.<\/li><li><code>0 &lt;= indices[i] &lt;&nbsp;n<\/code><\/li><li>All values of&nbsp;<code>indices<\/code>&nbsp;are unique (i.e.&nbsp;<code>indices<\/code>&nbsp;is a permutation of the integers from&nbsp;<code>0<\/code>&nbsp;to&nbsp;<code>n - 1<\/code>).<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Simulation<\/strong><\/h2>\n\n\n\n<p>Time complexity: O(n)<br>Space complexity: O(n)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">C++<\/h2>\n<div class=\"tabcontent\">\n\n<pre>\n\/\/ Author: Huahua\nclass Solution {\npublic:\n  string restoreString(string s, vector<int>& indices) {\n    string ans(s);\n    for (int i = 0; i < indices.size(); ++i)\n      ans[indices[i]] = s[i];\n    return ans;\n  }\n};\n<\/pre>\n\n<\/div><h2 class=\"tabtitle\">Java<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"java\">\n\/\/ Author: Huahua\nclass Solution {\n  public String restoreString(String s, int[] indices) {\n    char[] ans = new char[s.length()];\n    for (int i = 0; i < s.length(); ++i)\n      ans[indices[i]] = s.charAt(i);\n    return new String(ans);\n  }\n}\n<\/pre>\n\n<\/div><h2 class=\"tabtitle\">Pyhton3<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"python\">\nclass Solution:\n  def restoreString(self, s: str, indices: List[int]) -> str:\n    ans = [None] * len(s)\n    for i, idx in enumerate(indices):\n      ans[idx] = s[i]\n    return ''.join(ans)\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Given a string&nbsp;s&nbsp;and an integer array&nbsp;indices&nbsp;of the&nbsp;same length. The string&nbsp;s&nbsp;will be shuffled such that the character at the&nbsp;ith&nbsp;position moves to&nbsp;indices[i]&nbsp;in the shuffled string. Return&nbsp;the shuffled&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[48],"tags":[222,179,4],"class_list":["post-7149","post","type-post","status-publish","format-standard","hentry","category-simulation","tag-easy","tag-simulation","tag-string","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7149","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=7149"}],"version-history":[{"count":3,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7149\/revisions"}],"predecessor-version":[{"id":7153,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7149\/revisions\/7153"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=7149"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=7149"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=7149"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}