[LeetCode] Reverse String Prefix

3794. 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.

1
2
3
4
5
6
7
class Solution {
public:
string reversePrefix(string s, int k) {
reverse(begin(s), begin(s) + k);
return s;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2026/09/04/PS/LeetCode/reverse-string-prefix/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.