返回介绍

solution / 1500-1599 / 1528.Shuffle String / README_EN

发布于 2024-06-17 01:03:18 字数 3134 浏览 0 评论 0 收藏 0

1528. Shuffle String

中文文档

Description

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.

Return _the shuffled string_.

 

Example 1:

Input: s = "codeleet", indices = [4,5,6,7,0,2,1,3]
Output: "leetcode"
Explanation: As shown, "codeleet" becomes "leetcode" after shuffling.

Example 2:

Input: s = "abc", indices = [0,1,2]
Output: "abc"
Explanation: After shuffling, each character remains in its position.

 

Constraints:

  • s.length == indices.length == n
  • 1 <= n <= 100
  • s consists of only lowercase English letters.
  • 0 <= indices[i] < n
  • All values of indices are unique.

Solutions

Solution 1

class Solution:
  def restoreString(self, s: str, indices: List[int]) -> str:
    ans = [0] * len(s)
    for i, c in enumerate(s):
      ans[indices[i]] = c
    return ''.join(ans)
class Solution {
  public String restoreString(String s, int[] indices) {
    int n = s.length();
    char[] ans = new char[n];
    for (int i = 0; i < n; ++i) {
      ans[indices[i]] = s.charAt(i);
    }
    return String.valueOf(ans);
  }
}
class Solution {
public:
  string restoreString(string s, vector<int>& indices) {
    int n = s.size();
    string ans(n, 0);
    for (int i = 0; i < n; ++i) {
      ans[indices[i]] = s[i];
    }
    return ans;
  }
};
func restoreString(s string, indices []int) string {
  ans := make([]rune, len(s))
  for i, c := range s {
    ans[indices[i]] = c
  }
  return string(ans)
}
/**
 * @param {string} s
 * @param {number[]} indices
 * @return {string}
 */
var restoreString = function (s, indices) {
  let rs = [];
  for (let i = 0; i < s.length; i++) {
    rs[indices[i]] = s[i];
  }
  return rs.join('');
};

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文