class Solution {
public:
string deDup(string input) {
// write your solution here
if (input.empty()) return input;
string res = "";
// set up
char prev = input[0];
input.erase(0, 1);
res.push_back(prev);
// loop through the string
for (auto & c : input){
if (c == prev) continue;
else {
res.push_back(c);
prev = c;
}
}
return res;
}
};
One pass... Easy problem.
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.