Best solutions for Microsoft interview tasks. String Without 3 Identical Consecutive Letters

Best solutions for Microsoft interview tasks. String Without 3 Identical Consecutive Letters

Description:

Solution:

This task is very simple. We just need to pass through the given string and add to a new string the letters only if previous two letters are not equal to the current one.

C++ code:

string solution(const string & s) {
    int s_len = s.length();
    string res(s.begin(), s.begin()+2);
    for (int i = 2; i < s_len; ++i) {
        if (s[i] != s[i-1] || s[i] != s[i-2]) {
            res.push_back(s[i]);
        }
    }
    return res;
}

Repository with the full project you can find here: https://github.com/jolly-fellow/microsoft/tree/master/string_without_3_identical_consecutive_letters

Return to the table of contents.

要查看或添加评论,请登录

Alexander Molchevskyi的更多文章

社区洞察

其他会员也浏览了