Easy / Simple [ C | C++ | Java ] -- Dry Run Beats 100% in runtime & memory..
Intuition\n\nC++ []\nclass Solution {\npublic:\n    string modifyString(string s) {\n        for (auto i = 0; i < s.size(); ++i)\n            if (s[i] == \'?\')
# Intuition\n![Screenshot 2024-02-04 132949.png](https://assets.leetcode.com/users/images/c33ca3f9-fed2-4863-9706-b8807efa51ad_1707033617.7761476.png)\n```C++ []\nclass Solution {\npublic:\n    string modifyString(string s) {\n        for (auto i = 0; i < s.size(); ++i)\n            if (s[i] == \'?\')\n                for (s[i] = \'a\'; s[i] <= \'c\'; ++s[i])\n                    if ((i == 0 || s[i - 1] != s[i]) && (i == s.size() - 1 || s[i + 1] != s[i]))\n                        break;\n        return s;\n    }\n};\n```\n```java []\nclass Solution {\n    public String modifyString(String s) {\n        char[] ch = s.toCharArray();\n        for (int i = 0; i < ch.length; i++) {\n            if (ch[i] == \'?\') {\n                for (char j = \'a\'; j <= \'c\'; j++) {\n                    if (i > 0 && ch[i - 1] == j)\n                        continue;\n                    if (i < ch.length - 1 && ch[i + 1] == j)\n                        continue; \n                    ch[i] = j; \n                    break;\n                }\n            }\n        }\n        return new String(ch);\n    }\n}\n```\n```C++ []\nclass Solution {\npublic:\n    int counts[26];\n    string modifyString(string s) {\n        int n = s.size();\n        char prevC, nextC;\n        for (int i = 0; i < n; i++) {\n            if (s[i] == \'?\') {\n                if (i - 1 < 0)\n                    prevC = \' \';\n                else\n                    prevC = s[i - 1];\n                if (i + 1 >= n)\n                    nextC = \' \';\n                else\n                    nextC = s[i + 1];\n                for (int j = 0; j < 26; j++) {\n                    if ((\'a\' + j) != prevC && (\'a\' + j) != nextC) {\n                        s[i] = \'a\' + j;\n                    }\n                }\n            }\n        }\n\n        return s;\n    }\n};\n```\n```C []\nchar* modifyString(char* s) {\n    for (int i = 0; s[i] != \'\\0\'; i++) {\n        if (s[i] == \'?\') {\n            for (char j = \'a\'; j <= \'c\'; j++) {\n                if (i > 0 && s[i - 1] == j)\n                    continue;\n                if (i < strlen(s) - 1 && s[i + 1] == j)\n                    continue;\n                s[i] = j;\n                break;\n            }\n        }\n    }\n    return s;\n}\n```\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n     O(n)\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n    O(1)\n# Code\n```\nchar* modifyString(char* s) {\n    for (int i = 0; s[i] != \'\\0\'; i++) {\n        if (s[i] == \'?\') {\n            for (char j = \'a\'; j <= \'c\'; j++) {\n                if (i > 0 && s[i - 1] == j)\n                    continue;\n                if (i < strlen(s) - 1 && s[i + 1] == j)\n                    continue;\n                s[i] = j;\n                break;\n            }\n        }\n    }\n    return s;\n}\n```\n# Please upvote if it\'s useful..\n![th.jpeg](https://assets.leetcode.com/users/images/6dfe44ce-532b-4988-9f8d-cf8d32d6aca9_1707033855.9435267.jpeg)\n
['String', 'C', 'C++', 'Java']