848. Shifting Letters

class Solution { public: void shiftHelper(string & res, vector<int>& shifts, size_t idx){ int shift = shifts[idx]; for (size_t i = 0; i <= idx; i++) res[i] = 'a' + (res[i] - 'a' + shift) % 26; } string shiftingLetters(string S, vector<int>& shifts) { string res = S; for (size_t idx = 0; idx < shifts.size(); idx++) shiftHelper(res, shifts, idx); return res; } };
Just follow the logic. Tricky part is how to scale down the shifting positions.

Be the first to comment

You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.