2810. Faulty Keyboard LeetCode Solution
In this guide, you will get 2810. Faulty Keyboard LeetCode Solution with the best time and space complexity. The solution to Faulty Keyboard problem is provided in various programming languages like C++, Java, and Python. This will be helpful for you if you are preparing for placements, hackathons, interviews, or practice purposes. The solutions provided here are very easy to follow and include detailed explanations.
Table of Contents
- Problem Statement
- Complexity Analysis
- Faulty Keyboard solution in C++
- Faulty Keyboard solution in Java
- Faulty Keyboard solution in Python
- Additional Resources

Problem Statement of 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.
Example 1:
Input: s = “string”
Output: “rtsng”
Explanation:
After typing first character, the text on the screen is “s”.
After the second character, the text is “st”.
After the third character, the text is “str”.
Since the fourth character is an ‘i’, the text gets reversed and becomes “rts”.
After the fifth character, the text is “rtsn”.
After the sixth character, the text is “rtsng”.
Therefore, we return “rtsng”.
Example 2:
Input: s = “poiinter”
Output: “ponter”
Explanation:
After the first character, the text on the screen is “p”.
After the second character, the text is “po”.
Since the third character you type is an ‘i’, the text gets reversed and becomes “op”.
Since the fourth character you type is an ‘i’, the text gets reversed and becomes “po”.
After the fifth character, the text is “pon”.
After the sixth character, the text is “pont”.
After the seventh character, the text is “ponte”.
After the eighth character, the text is “ponter”.
Therefore, we return “ponter”.
Constraints:
1 <= s.length <= 100
s consists of lowercase English letters.
s[0] != 'i'
Complexity Analysis
- Time Complexity: O(n)
- Space Complexity: O(n)
2810. Faulty Keyboard LeetCode Solution in C++
class Solution {
public:
string finalString(string s) {
deque<char> dq;
bool inversed = false;
for (const char c : s)
if (c == 'i')
inversed = !inversed;
else if (inversed)
dq.push_front(c);
else
dq.push_back(c);
return inversed ? string{dq.rbegin(), dq.rend()}
: string{dq.begin(), dq.end()};
}
};
/* code provided by PROGIEZ */
2810. Faulty Keyboard LeetCode Solution in Java
class Solution {
public String finalString(String s) {
StringBuilder sb = new StringBuilder();
Deque<Character> dq = new ArrayDeque<>();
boolean inversed = false;
for (final char c : s.toCharArray())
if (c == 'i')
inversed = !inversed;
else if (inversed)
dq.offerFirst(c);
else
dq.offerLast(c);
while (!dq.isEmpty())
sb.append(dq.pollFirst());
return inversed ? sb.reverse().toString() : sb.toString();
}
}
// code provided by PROGIEZ
2810. Faulty Keyboard LeetCode Solution in Python
class Solution:
def finalString(self, s: str) -> str:
dq = collections.deque()
inversed = False
for c in s:
if c == 'i':
inversed = not inversed
elif inversed:
dq.appendleft(c)
else:
dq.append(c)
return ''.join(reversed(dq)) if inversed else ''.join(dq)
# code by PROGIEZ
Additional Resources
- Explore all LeetCode problem solutions at Progiez here
- Explore all problems on LeetCode website here
Happy Coding! Keep following PROGIEZ for more updates and solutions.