Reverse String Prefix

You are given a string s and an integer k.

Reverse the first k characters of s and return the resulting string.

Example 1
Inputs = "abcd", k = 2
Output"bacd"
The first k = 2 characters "ab" are reversed to "ba", so the final resulting string is "bacd".
Example 2
Inputs = "xyz", k = 3
Output"zyx"
The first k = 3 characters "xyz" are reversed to "zyx", so the final resulting string is "zyx".

Constraints

  • 1 <= s.length <= 100
  • s consists of lowercase English letters.
  • 1 <= k <= s.length

Asked at 1 companies

</>

Your Solution

(Ctrl/Cmd + Enter)

Switching Language

Loading template...

Loading...

Sign in to save your progress

AI code evaluation

Get a correctness verdict, missed edge cases, and complexity analysis of your solution.

Sign in to evaluate