clueless coding // TODO: be smarter

LeetCode Reverse String II: CPP Solution



TL;DR Code Solution



void rev(string& s, int start, int end) {
    while (start < end) {
        char temp = s[start];
        s[start] = s[end];
        s[end] = temp;
        start++;
        end--;
    }
}

string reverseStr(string s, int k) {
    for (int i = 0; i < s.length(); i += 2 * k) {
        int end = min(i + k - 1, (int)s.length() - 1);
        rev(s, i, end);
    }
    return s;
}