Algorithm to Replace All ?’s to Avoid Consecutive Repeatin

  • 时间:2020-10-09 18:35:39
  • 分类:网络文摘
  • 阅读:126 次

Given a string s containing only lower case English letters and the ‘?’ character, convert all the ‘?’ characters into lower case letters such that the final string does not contain any consecutive repeating characters. You cannot modify the non ‘?’ characters. It is guaranteed that there are no consecutive repeating characters in the given string except for ‘?’.

Return the final string after all the conversions (possibly zero) have been made. If there is more than one solution, return any of them. It can be shown that an answer is always possible with the given constraints.

Example 1:
Input: s = “?zs”
Output: “azs”
Explanation: There are 25 solutions for this problem. From “azs” to “yzs”, all are valid. Only “z” is an invalid modification as the string will consist of consecutive repeating characters in “zzs”.

Example 2:
Input: s = “ubv?w”
Output: “ubvaw”
Explanation: There are 24 solutions for this problem. Only “v” and “w” are invalid modifications as the strings will consist of consecutive repeating characters in “ubvvw” and “ubvww”.

Example 3:
Input: s = “j?qg??b”
Output: “jaqgacb”

Example 4:
Input: s = “??yw?ipkj?”
Output: “acywaipkja”

Constraints:
1 <= s.length <= 100
s contains only lower case English letters and ‘?’.

Hints:
Processing string from left to right, whenever you get a ‘?’ character, check left character and right character, and select a character not equal to either of them
Do take care to compare with replaced occurrence of ‘?’ when checking the left

String Algorithm to Avoid Consecutive Repeating Characters by Replacing the ? Character

All the characters are lowercases, thus if we are to replace the ? character so that to avoid consecutive repeating characters, we can use the following function to return the first character that is not the same as previous and next character.

1
2
3
4
5
6
7
8
9
private:
    char diff(char a, char b) {
        for (auto ans = 'a'; ans <= 'z'; ++ ans) {
            if ((ans != a) && (ans != b)) {
                return ans;
            }
        }
        return ' ';
    }
private:
    char diff(char a, char b) {
        for (auto ans = 'a'; ans <= 'z'; ++ ans) {
            if ((ans != a) && (ans != b)) {
                return ans;
            }
        }
        return ' ';
    }

Then the algorithm works like this: we process the string from left to right, when we meet a ‘?’ to replace – we replace it with a character (above function) that is neither previous and next character. Please note that when we replace it with a character – we have to updated the previous character.

The following implementation store the new string separately – O(N) time and O(N) space complexity.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
class Solution {
public:
    string modifyString(string s) {
        string ans;
        char prev = ' ';
        for (int i = 0; i + 1 < s.size(); ++ i) {
            if (s[i] == '?') {
                ans += diff(prev, s[i + 1]);
                prev = ans.back();
            } else {
                ans += s[i];
                prev = s[i];
            }
        }    
        if (s.back() == '?') {
            ans += diff(prev, ' ');
        } else {
            ans += s.back();
        }
        return ans;
    }
};
class Solution {
public:
    string modifyString(string s) {
        string ans;
        char prev = ' ';
        for (int i = 0; i + 1 < s.size(); ++ i) {
            if (s[i] == '?') {
                ans += diff(prev, s[i + 1]);
                prev = ans.back();
            } else {
                ans += s[i];
                prev = s[i];
            }
        }    
        if (s.back() == '?') {
            ans += diff(prev, ' ');
        } else {
            ans += s.back();
        }
        return ans;
    }
};

This can also be implementing by replacing the characters in place:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
class Solution {
public:
    string modifyString(string s) {
        char prev = ' ';
        for (int i = 0; i + 1 < s.size(); ++ i) {
            if (s[i] == '?') {
                s[i] = diff(prev, s[i + 1]);
                prev = s[i];
            } else {
                prev = s[i];
            }
        }    
        if (s.back() == '?') {
            s.back() = diff(prev, ' ');
        } 
        return s;
    }
}
class Solution {
public:
    string modifyString(string s) {
        char prev = ' ';
        for (int i = 0; i + 1 < s.size(); ++ i) {
            if (s[i] == '?') {
                s[i] = diff(prev, s[i + 1]);
                prev = s[i];
            } else {
                prev = s[i];
            }
        }    
        if (s.back() == '?') {
            s.back() = diff(prev, ' ');
        } 
        return s;
    }
}

We can also use the index to get the previous character:

1
2
3
4
5
6
7
8
9
10
11
12
13
class Solution {
public:
    string modifyString(string s) {
        for (int i = 0; i + 1 < s.size(); ++ i) {
            if (s[i] == '?') {
                s[i] = diff(i > 0 ? s[i - 1] : ' ', s[i + 1]);
            } 
        }    
        if (s.back() == '?') {
            s.back() = diff(s.size() > 1 ? s[s.size() - 2] : ' ', ' ');
        } 
        return s;
    }
class Solution {
public:
    string modifyString(string s) {
        for (int i = 0; i + 1 < s.size(); ++ i) {
            if (s[i] == '?') {
                s[i] = diff(i > 0 ? s[i - 1] : ' ', s[i + 1]);
            } 
        }    
        if (s.back() == '?') {
            s.back() = diff(s.size() > 1 ? s[s.size() - 2] : ' ', ' ');
        } 
        return s;
    }

This implementation runs O(N) time and O(1) space complexity.

–EOF (The Ultimate Computing & Technology Blog) —

推荐阅读:
保健食品当做药品卖“脑力风暴”骗局  “公益网站”牵线 保健食品当药品卖  消费者如何正确选择购买保健食品  中国拟新制定五项食品安全国家标准  脑力劳动者如何科学补充食物营养?  食品中的肉毒杆菌对儿童的危害很大  质检总局要求雅培召回两批次风险奶粉  洋奶粉频“出事”国产奶粉难“翻身”  新西兰肉毒杆菌事件中“涉毒”品牌  食疗养生:治疗牙龈出血的食疗方 
评论列表
添加评论