[LeetCode] Faulty Keyboard

2810. Faulty Keyboard

Your laptop keyboard is faulty, and whenever you type a character 'i' on it, it reverses the string that you have written. Typing other characters works as expected.

You are given a 0-indexed string s, and you type each character of s using your faulty keyboard.

Return the final string that will be present on your laptop screen.

1
2
3
4
5
6
7
8
9
10
11
12
class Solution {
public:
string finalString(string s) {
string res = "";
for(auto& ch : s) {
if(ch == 'i') {
reverse(begin(res), end(res));
} else res.push_back(ch);
}
return res;
}
};
Author: Song Hayoung
Link: https://songhayoung.github.io/2023/08/06/PS/LeetCode/faulty-keyboard/
Copyright Notice: All articles in this blog are licensed under CC BY-NC-SA 4.0 unless stating additionally.