Given a string s, perform the following two operations in order and return the resulting string:
-
Combine: append the reverse of
s
to the end of
s
.
-
Replace: in the combined string, replace every space character (
' '
) with the URL-encoded sequence
"%20"
.
Return the final transformed string.
Function Signature
def combine_and_replace(s: str) -> str:
...
Rules
-
The reverse of
s
is its characters in reverse order, spaces included.
-
The replacement runs on the combined string, so each space in
s
produces two
%20
sequences in the result. Every other character is copied unchanged.
Constraints
-
1 <= len(s) <= 100
-
s
consists of lowercase English letters and spaces only.
-
The combined string after replacement will not exceed 300 characters.
Examples
Input: s = "hello world"
Output: "hello%20worlddlrow%20olleh"
The reverse of "hello world" is "dlrow olleh", so the combined string is "hello worlddlrow olleh". Each of its two spaces becomes %20.
Input: s = "abc"
Output: "abccba"
The reverse is "cba" and the combined string "abccba" has no spaces, so nothing is replaced.
Input: s = " a b"
Output: "%20a%20bb%20a%20"
The reverse is "b a ", so the combined string is " a bb a " with four spaces, including one at each end. The result has 8 + 4 * 2 = 16 characters.