Skip to content

Commit c150b17

Browse files
committed
New Problem Solution -"Shuffle String"
1 parent 18da053 commit c150b17

File tree

2 files changed

+62
-0
lines changed

2 files changed

+62
-0
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ LeetCode
7272
|1550|[Three Consecutive Odds](https://leetcode.com/problems/three-consecutive-odds/) | [C++](./algorithms/cpp/threeConsecutiveOdds/ThreeConsecutiveOdds.cpp)|Easy|
7373
|1541|[Minimum Insertions to Balance a Parentheses String](https://leetcode.com/problems/minimum-insertions-to-balance-a-parentheses-string/) | [C++](./algorithms/cpp/minimumInsertionsToBalanceAParenthesesString/MinimumInsertionsToBalanceAParenthesesString.cpp)|Medium|
7474
|1535|[Find the Winner of an Array Game](https://leetcode.com/problems/find-the-winner-of-an-array-game/) | [C++](./algorithms/cpp/findTheWinnerOfAnArrayGame/FindTheWinnerOfAnArrayGame.cpp)|Medium|
75+
|1528|[Shuffle String](https://leetcode.com/problems/shuffle-string/submissions/) | [C++](./algorithms/cpp/shuffleString/ShuffleString.cpp)|Easy|
7576
|1525|[Number of Good Ways to Split a String](https://leetcode.com/problems/number-of-good-ways-to-split-a-string/) | [C++](./algorithms/cpp/numberOfGoodWaysToSplitAString/NumberOfGoodWaysToSplitAString.cpp)|Medium|
7677
|1524|[Number of Sub-arrays With Odd Sum](https://leetcode.com/problems/number-of-sub-arrays-with-odd-sum/) | [C++](./algorithms/cpp/numberOfSubArraysWithOddSum/NumberOfSubArraysWithOddSum.cpp)|Medium|
7778
|1523|[Count Odd Numbers in an Interval Range](https://leetcode.com/problems/count-odd-numbers-in-an-interval-range/) | [C++](./algorithms/cpp/countOddNumbersInAnIntervalRange/CountOddNumbersInAnIntervalRange.cpp)|Easy|
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
// Source : https://leetcode.com/problems/shuffle-string/submissions/
2+
// Author : Hao Chen
3+
// Date : 2021-03-29
4+
5+
/*****************************************************************************************************
6+
*
7+
* Given a string s and an integer array indices of the same length.
8+
*
9+
* The string s will be shuffled such that the character at the i^th position moves to indices[i] in
10+
* the shuffled string.
11+
*
12+
* Return the shuffled string.
13+
*
14+
* Example 1:
15+
*
16+
* Input: s = "codeleet", indices = [4,5,6,7,0,2,1,3]
17+
* Output: "leetcode"
18+
* Explanation: As shown, "codeleet" becomes "leetcode" after shuffling.
19+
*
20+
* Example 2:
21+
*
22+
* Input: s = "abc", indices = [0,1,2]
23+
* Output: "abc"
24+
* Explanation: After shuffling, each character remains in its position.
25+
*
26+
* Example 3:
27+
*
28+
* Input: s = "aiohn", indices = [3,1,4,2,0]
29+
* Output: "nihao"
30+
*
31+
* Example 4:
32+
*
33+
* Input: s = "aaiougrt", indices = [4,0,2,6,7,3,1,5]
34+
* Output: "arigatou"
35+
*
36+
* Example 5:
37+
*
38+
* Input: s = "art", indices = [1,0,2]
39+
* Output: "rat"
40+
*
41+
* Constraints:
42+
*
43+
* s.length == indices.length == n
44+
* 1 <= n <= 100
45+
* s contains only lower-case English letters.
46+
* 0 <= indices[i] < n
47+
* All values of indices are unique (i.e. indices is a permutation of the integers from 0 to n
48+
* - 1).
49+
******************************************************************************************************/
50+
51+
class Solution {
52+
public:
53+
string restoreString(string s, vector<int>& indices) {
54+
string result(s.size(), ' ');
55+
for (int i = 0; i < indices.size(); i++) {
56+
result[indices[i]] = s[i];
57+
}
58+
return result;
59+
}
60+
61+
};

0 commit comments

Comments
 (0)