2109. Adding Spaces to a String Medium
1/**
2 * [2109] Adding Spaces to a String
3 *
4 * You are given a 0-indexed string s and a 0-indexed integer array spaces that describes the indices in the original string where spaces will be added. Each space should be inserted before the character at the given index.
5 *
6 * For example, given s = "EnjoyYourCoffee" and spaces = [5, 9], we place spaces before 'Y' and 'C', which are at indices 5 and 9 respectively. Thus, we obtain "Enjoy <u>Y</u>our <u>C</u>offee".
7 *
8 * Return the modified string after the spaces have been added.
9 *
10 * Example 1:
11 *
12 * Input: s = "LeetcodeHelpsMeLearn", spaces = [8,13,15]
13 * Output: "Leetcode Helps Me Learn"
14 * Explanation:
15 * The indices 8, 13, and 15 correspond to the underlined characters in "Leetcode<u>H</u>elps<u>M</u>e<u>L</u>earn".
16 * We then place spaces before those characters.
17 *
18 * Example 2:
19 *
20 * Input: s = "icodeinpython", spaces = [1,5,7,9]
21 * Output: "i code in py thon"
22 * Explanation:
23 * The indices 1, 5, 7, and 9 correspond to the underlined characters in "i<u>c</u>ode<u>i</u>n<u>p</u>y<u>t</u>hon".
24 * We then place spaces before those characters.
25 *
26 * Example 3:
27 *
28 * Input: s = "spacing", spaces = [0,1,2,3,4,5,6]
29 * Output: " s p a c i n g"
30 * Explanation:
31 * We are also able to place spaces before the first character of the string.
32 *
33 *
34 * Constraints:
35 *
36 * 1 <= s.length <= 3 * 10^5
37 * s consists only of lowercase and uppercase English letters.
38 * 1 <= spaces.length <= 3 * 10^5
39 * 0 <= spaces[i] <= s.length - 1
40 * All the values of spaces are strictly increasing.
41 *
42 */
43pub struct Solution {}
44
45// problem: https://leetcode.com/problems/adding-spaces-to-a-string/
46// discuss: https://leetcode.com/problems/adding-spaces-to-a-string/discuss/?currentPage=1&orderBy=most_votes&query=
47
48// submission codes start here
49
50impl Solution {
51 pub fn add_spaces(s: String, spaces: Vec<i32>) -> String {
52 String::new()
53 }
54}
55
56// submission codes end
57
58#[cfg(test)]
59mod tests {
60 use super::*;
61
62 #[test]
63 fn test_2109() {
64 }
65}
66
Back
© 2025 bowen.ge All Rights Reserved.