question_slug
stringlengths 3
77
| title
stringlengths 1
183
| slug
stringlengths 12
45
| summary
stringlengths 1
160
⌀ | author
stringlengths 2
30
| certification
stringclasses 2
values | created_at
stringdate 2013-10-25 17:32:12
2025-04-12 09:38:24
| updated_at
stringdate 2013-10-25 17:32:12
2025-04-12 09:38:24
| hit_count
int64 0
10.6M
| has_video
bool 2
classes | content
stringlengths 4
576k
| upvotes
int64 0
11.5k
| downvotes
int64 0
358
| tags
stringlengths 2
193
| comments
int64 0
2.56k
|
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
score-of-a-string | Easy 3 line code | easy-3-line-code-by-nithilab2106-lzat | IntuitionThe question asked us to find the absolute difference of each letter with the next and sum up all these diffrencesApproachWhen we try storing a charcte | nithilab2106 | NORMAL | 2025-01-14T14:06:09.089564+00:00 | 2025-01-14T14:06:09.089564+00:00 | 98 | false | # Intuition
The question asked us to find the absolute difference of each letter with the next and sum up all these diffrences
<!-- Describe your first thoughts on how to solve this problem. -->
# Approach
When we try storing a charcter in an integer varaible or perform mathematical functions java automatically converts the character to it's ascii value
the difference I need to find is ascii value of - current letter and letter+1
so ,
> s.charAt(i)-s.charAt(i+1)
<!-- Describe your approach to solving the problem. -->
gives me the the difference
now to make it absolute we use
> Math.abs
<!-->
the value returned by math .abs is double so we use
> (int)
<!-->
for type conversion from double to integer
we keep adding this difference every iteration of the loop until length-1 of the string storing it in score
returning score as the final answer
# Complexity
- Time complexity: O(n)
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
- Space complexity:O(1)
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
# Code
```java []
class Solution {
public int scoreOfString(String s)
{
int score=0;
for (int i=0;i<s.length()-1;i++)
{
score+=(int)Math.abs(s.charAt(i)-s.charAt(i+1));
}
return score;
}
}
``` | 1 | 0 | ['String', 'Java'] | 0 |
replace-all-s-to-avoid-consecutive-repeating-characters | C++/Python O(n) | cpython-on-by-votrubac-4o9o | We do not need more than 3 letters to build a non-repeating character sequence.\n\nFor Python, we can use set difference to determine which one to use.\n\nPytho | votrubac | NORMAL | 2020-09-06T04:01:02.465128+00:00 | 2020-09-06T04:59:25.577416+00:00 | 10,276 | false | We do not need more than 3 letters to build a non-repeating character sequence.\n\nFor Python, we can use set difference to determine which one to use.\n\n**Python**\n```python\ndef modifyString(self, s: str) -> str:\n\tres, prev = "", \'?\'\n\tfor i, c in enumerate(s):\n\t\tnext = s[i + 1] if i + 1 < len(s) else \'?\'\n\t\tprev = c if c != \'?\' else {\'a\', \'b\', \'c\'}.difference({prev, next}).pop()\n\t\tres += prev\n\treturn res\n```\n\n**C++**\n```cpp\nstring 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``` | 117 | 1 | [] | 11 |
replace-all-s-to-avoid-consecutive-repeating-characters | Java Simple O(n) loop | java-simple-on-loop-by-hobiter-2uqh | for each char, just try \u2018a\u2019, \u2018b\u2019, \u2018c\u2019, and select the one not the same as neighbors.\n\n\n public String modifyString(String s) | hobiter | NORMAL | 2020-09-06T04:01:14.868400+00:00 | 2020-09-06T04:01:14.868459+00:00 | 8,211 | false | for each char, just try \u2018a\u2019, \u2018b\u2019, \u2018c\u2019, and select the one not the same as neighbors.\n\n```\n public String modifyString(String s) {\n char[] arr = s.toCharArray();\n for (int i = 0; i < arr.length; i++) {\n if (arr[i] == \'?\') {\n for (int j = 0; j < 3; j++) {\n if (i > 0 && arr[i - 1] - \'a\' == j) continue;\n if (i + 1 < arr.length && arr[i + 1] - \'a\' == j) continue;\n arr[i] = (char) (\'a\' + j);\n break;\n }\n }\n }\n return String.valueOf(arr);\n }\n```\n | 81 | 3 | [] | 7 |
replace-all-s-to-avoid-consecutive-repeating-characters | [Python3] one of three letters | python3-one-of-three-letters-by-ye15-qqaj | \n\nclass Solution:\n def modifyString(self, s: str) -> str:\n s = list(s)\n for i in range(len(s)):\n if s[i] == "?": \n | ye15 | NORMAL | 2020-09-06T04:02:34.600206+00:00 | 2020-09-06T04:02:34.600246+00:00 | 4,825 | false | \n```\nclass Solution:\n def modifyString(self, s: str) -> str:\n s = list(s)\n for i in range(len(s)):\n if s[i] == "?": \n for c in "abc": \n if (i == 0 or s[i-1] != c) and (i+1 == len(s) or s[i+1] != c): \n s[i] = c\n break \n return "".join(s)\n``` | 79 | 1 | ['Python3'] | 8 |
replace-all-s-to-avoid-consecutive-repeating-characters | c++ | 0ms Code | easy to understand | with explanation | c-0ms-code-easy-to-understand-with-expla-qb7x | \n\'\'\'\n\n\t\tclass Solution {\n\t\tpublic:\n\t\tstring modifyString(string s) {\n int n = s.length();\n for(int i=0;i<n;i++){\n \n | pranjalmittal21 | NORMAL | 2020-09-07T09:10:08.596855+00:00 | 2020-09-24T09:25:30.018041+00:00 | 2,094 | false | \n\'\'\'\n\n\t\tclass Solution {\n\t\tpublic:\n\t\tstring modifyString(string s) {\n int n = s.length();\n for(int i=0;i<n;i++){\n \n //checking every character, if character is \'?\'\n if(s[i] == \'?\'){\n \n //loop over every alphabet\n for(char j = \'a\';j<=\'z\';j++){\n //compare with previous char\n if(i-1>=0 && j==s[i-1]) continue;\n //compare with next char\n if(i+1<n && j==s[i+1]) continue;\n //if both the above conditions are false, fix the current alphabet in that\n //position and break the loop\n s[i] = j;\n break;\n }\n }\n \n //check next char of the string\n \n }\n return s;\n }\n\t};\n\n\'\'\'\nPlease **Upvote** my ans if you understand | 29 | 0 | ['C', 'C++'] | 5 |
replace-all-s-to-avoid-consecutive-repeating-characters | Super easy solution, O(n), beats 100% | super-easy-solution-on-beats-100-by-eaim-44wt | \npublic String modifyString(String s) {\n if (s == null || s.isEmpty()) return "";\n \n char[] chars = s.toCharArray();\n for (int i=0; i<chars.len | eaiman | NORMAL | 2020-09-06T07:11:09.432675+00:00 | 2020-09-06T07:11:38.793083+00:00 | 3,524 | false | ```\npublic String modifyString(String s) {\n if (s == null || s.isEmpty()) return "";\n \n char[] chars = s.toCharArray();\n for (int i=0; i<chars.length; i++) {\n if (chars[i] == \'?\') {\n for (char j=\'a\'; j<=\'z\'; j++) {\n chars[i] = j;\n if (i>0 && chars[i-1] == j) continue;\n if(i<chars.length-1 && chars[i+1] == j) continue;\n break;\n }\n }\n }\n \n return new String(chars);\n}\n``` | 21 | 0 | ['Java'] | 6 |
replace-all-s-to-avoid-consecutive-repeating-characters | Python simple solution | python-simple-solution-by-tovam-kwxf | Python :\n\n\ndef modifyString(self, s: str) -> str:\n\ts = list(s)\n\n\tfor i in range(len(s)):\n\t\tif s[i] == \'?\':\n\t\t\tfor c in "abc":\n\t\t\t\tif (i == | TovAm | NORMAL | 2021-11-03T22:52:46.999158+00:00 | 2021-11-03T22:52:46.999201+00:00 | 1,333 | false | **Python :**\n\n```\ndef modifyString(self, s: str) -> str:\n\ts = list(s)\n\n\tfor i in range(len(s)):\n\t\tif s[i] == \'?\':\n\t\t\tfor c in "abc":\n\t\t\t\tif (i == 0 or s[i - 1] != c) and (i + 1 == len(s) or s[i + 1] != c):\n\t\t\t\t\ts[i] = c\n\t\t\t\t\tbreak\n\n\treturn "".join(s)\n```\n\n**Like it ? please upvote !** | 12 | 0 | ['Python', 'Python3'] | 1 |
replace-all-s-to-avoid-consecutive-repeating-characters | C# | c-by-mhorskaya-7x0o | \npublic string ModifyString(string s) {\n\tvar chars = s.ToArray();\n\n\tfor (var i = 0; i < s.Length; i++) {\n\t\tif (chars[i] != \'?\') continue;\n\n\t\tvar | mhorskaya | NORMAL | 2020-09-09T10:29:27.969362+00:00 | 2020-09-09T10:29:27.969392+00:00 | 429 | false | ```\npublic string ModifyString(string s) {\n\tvar chars = s.ToArray();\n\n\tfor (var i = 0; i < s.Length; i++) {\n\t\tif (chars[i] != \'?\') continue;\n\n\t\tvar left = i > 0 ? chars[i - 1] : (char?)null;\n\t\tvar right = i < s.Length - 1 ? chars[i + 1] : (char?)null;\n\n\t\tif (left != \'a\' && right != \'a\') chars[i] = \'a\';\n\t\telse if (left != \'b\' && right != \'b\') chars[i] = \'b\';\n\t\telse if (left != \'c\' && right != \'c\') chars[i] = \'c\';\n\t}\n\n\treturn new string(chars);\n}\n``` | 10 | 0 | [] | 0 |
replace-all-s-to-avoid-consecutive-repeating-characters | EASY C++ solution with comments || O(n) | easy-c-solution-with-comments-on-by-sky9-sjgv | \nclass Solution {\npublic:\n string modifyString(string s) {\n for(int i = 0;i < s.size();i++){\n if(s[i] == \'?\'){\n \n | sky97 | NORMAL | 2020-09-06T04:04:21.383703+00:00 | 2020-09-06T04:04:21.383768+00:00 | 1,141 | false | ```\nclass Solution {\npublic:\n string modifyString(string s) {\n for(int i = 0;i < s.size();i++){\n if(s[i] == \'?\'){\n \n if(i == 0){ // if we are starting then we have to check only next character\n if(i + 1 < s.size()){\n if(s[i+1] != \'a\') s[i] = \'a\';\n else s[i] = \'b\';\n } \n else s[i] = \'a\';\n }\n \n else if(i == s.size() - 1){ // if we are at last position\n if(s[i-1] != \'a\') s[i] = \'a\';\n else s[i] = \'b\';\n }\n \n else{\n if(s[i + 1] != \'a\' && s[i-1] != \'a\') s[i] = \'a\';\n else if(s[i + 1] != \'b\' && s[i-1] != \'b\') s[i] = \'b\';\n else s[i] = \'c\';\n }\n \n }\n }\n return s;\n }\n};\n``` | 10 | 0 | [] | 1 |
replace-all-s-to-avoid-consecutive-repeating-characters | 1576. Replace All ?'s to Avoid Consecutive Re..., Time Complexity: O(N), Space Complexity: O(1) | 1576-replace-all-s-to-avoid-consecutive-5lyai | IntuitionApproachComplexity
Time complexity: O(N)
Space complexity: O(1)
Code | richardmantikwang | NORMAL | 2024-12-19T10:44:55.036685+00:00 | 2024-12-19T10:47:29.251629+00:00 | 141 | false | # Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
# Approach
<!-- Describe your approach to solving the problem. -->
# Complexity
- Time complexity: O(N)
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
- Space complexity: O(1)
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
# Code
```python3 []
class Solution:
def modifyString(self, s):
all_chars_ords = range(97, 122 + 1)
all_chars = [chr(o) for o in all_chars_ords]
len_s = len(s)
ret_val = ''
i = 0
while (i < len_s):
if (s[i] == '?'):
replaced_chars = all_chars.copy()
prev_c = None
if (i > 0):
prev_c = ret_val[i-1] # prev_c = s[i-1]
replaced_chars.remove(prev_c)
next_c = None
if (i < len(s) - 1):
next_c = s[i+1]
try:
replaced_chars.remove(next_c)
except ValueError:
pass
ret_val += replaced_chars[0]
else:
ret_val += s[i]
i += 1
return ret_val
``` | 9 | 0 | ['Python3'] | 0 |
replace-all-s-to-avoid-consecutive-repeating-characters | [Java] a, b, c solution beats 100% - comment explained | java-a-b-c-solution-beats-100-comment-ex-1xpc | \nclass Solution {\n public String modifyString(String s) {\n char[] ch = s.toCharArray();\n for (int i = 0;i<ch.length;i++){\n if ( | vinsinin | NORMAL | 2021-02-23T07:40:34.426814+00:00 | 2021-02-23T07:40:34.426850+00:00 | 1,070 | false | ```\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) continue; //skip if previous character is one of a,b,c\n if (i < ch.length-1 && ch[i+1] == j) continue; //skip if next character is one of a,b,c\n ch[i] = j; //set with the current character from a,b,c\n break;\n }\n }\n }\n return new String(ch);\n }\n}\n``` | 9 | 1 | ['Java'] | 1 |
replace-all-s-to-avoid-consecutive-repeating-characters | python3 beginner friendly | python3-beginner-friendly-by-m0u1ea5-rrwr | python\nclass Solution:\n def modifyString(self, s: str) -> str:\n if len(s)==1:\n return \'a\' \n s=list(s)\n for i in range | M0u1ea5 | NORMAL | 2020-12-07T05:55:18.202491+00:00 | 2021-03-20T12:30:07.042145+00:00 | 937 | false | ```python\nclass Solution:\n def modifyString(self, s: str) -> str:\n if len(s)==1:\n return \'a\' \n s=list(s)\n for i in range(len(s)):\n if s[i] == \'?\':\n for x in \'abc\': \n if i == 0 and s[i+1] != x: \n s[i]=x\n elif i != 0 and i+1 != len(s):\n if s[i+1] != x and s[i-1] != x:\n s[i]=x \n elif i==len(s)-1 and s[i-1] != x:\n s[i]=x\n return \'\'.join(s)\n``` | 9 | 0 | [] | 2 |
replace-all-s-to-avoid-consecutive-repeating-characters | [C++] Simple Brute Force Solution | Self-Explanatory | c-simple-brute-force-solution-self-expla-c7gd | \nclass Solution\n{\npublic:\n string modifyString(string s)\n {\n char temp1, temp2;\n int index;\n for (int k = 0; k < s.length(); | ravireddy07 | NORMAL | 2020-09-06T04:41:03.755820+00:00 | 2021-03-12T17:05:52.045818+00:00 | 836 | false | ```\nclass Solution\n{\npublic:\n string modifyString(string s)\n {\n char temp1, temp2;\n int index;\n for (int k = 0; k < s.length(); ++k)\n {\n if (s[k] == \'?\')\n {\n index = k;\n for (int i = 0; i < 26; ++i)\n {\n if (index == 0)\n {\n if (s[k + 1] != (char)(97 + i))\n {\n s[k] = (char)(97 + i);\n continue;\n }\n }\n else\n {\n temp1 = s[k - 1], temp2 = s[k + 1];\n //cout<<temp1<<" "<<temp2<<" "<<(int)temp2<<"\\n";\n if (temp1 != (char)(97 + i) && temp2 != (char)(97 + i))\n {\n s[k] = (char)(97 + i);\n continue;\n }\n }\n }\n }\n }\n return s;\n }\n};\n``` | 9 | 1 | ['C'] | 1 |
replace-all-s-to-avoid-consecutive-repeating-characters | 💻✅BEATS 100%⌛⚡[C++/Java/Py3/JS]🍨| EASY n CLEAN EXPLANATION⭕💌 | beats-100cjavapy3js-easy-n-clean-explana-blry | IntuitionThe problem requires replacing each '?' in the given string with a lowercase English letter such that no two adjacent characters are the same. Since th | Fawz-Haaroon | NORMAL | 2025-03-20T05:13:34.799380+00:00 | 2025-03-20T05:13:34.799380+00:00 | 60 | false | # Intuition
The problem requires replacing each `'?'` in the given string with a lowercase English letter such that no two adjacent characters are the same. Since there are only 26 possible letters, we can greedily replace `'?'` with the smallest possible letter that does not match its adjacent characters.
# Approach
1. Traverse the string character by character.
2. If a character is `'?'`, determine the possible letters that do not match its left and right adjacent characters.
3. Select the smallest valid letter (`'a'`, `'b'`, or `'c'` should be enough).
4. Replace `'?'` with this letter and continue.
5. Return the modified string.
### Steps:
- Iterate through the string.
- If `s[i]` is `'?'`:
- Find `s[i-1]` (left character) if it exists.
- Find `s[i+1]` (right character) if it exists.
- Choose a replacement character (`'a'`, `'b'`, or `'c'`) that is not the same as `left` or `right`.
- Assign this character to `s[i]`.
- Continue until all `'?'` are replaced.
# Complexity
- **Time Complexity**: `O(n)`, as we traverse the string once.
- **Space Complexity**: `O(1)`, as we modify the string in place.
# Code
```C++ []
class Solution {
public:
string modifyString(string s) {
int n = s.length();
for (int i = 0; i < n; i++) {
if (s[i] == '?') {
char left = (i > 0) ? s[i - 1] : ' ';
char right = (i + 1 < n) ? s[i + 1] : ' ';
s[i] = (left != 'a' && right != 'a')? 'a' : (left != 'b' && right != 'b')? 'b' : 'c';
}
}return s;
}
};
```
```python []
class Solution:
def modifyString(self, s: str) -> str:
s = list(s)
n = len(s)
for i in range(n):
if s[i] == '?':
left = s[i - 1] if i > 0 else ' '
right = s[i + 1] if i + 1 < n else ' '
if left != 'a' and right != 'a':
s[i] = 'a'
elif left != 'b' and right != 'b':
s[i] = 'b'
else:
s[i] = 'c'
return "".join(s)
```
```java []
class Solution {
public String modifyString(String s) {
char[] arr = s.toCharArray();
int n = arr.length;
for (int i = 0; i < n; i++) {
if (arr[i] == '?') {
char left = (i > 0) ? arr[i - 1] : ' ';
char right = (i + 1 < n) ? arr[i + 1] : ' ';
if (left != 'a' && right != 'a') arr[i] = 'a';
else if (left != 'b' && right != 'b') arr[i] = 'b';
else arr[i] = 'c';
}
}
return new String(arr);
}
}
```
```javascript []
/**
* @param {string} s
* @return {string}
*/
var modifyString = function(s) {
let arr = s.split('');
let n = arr.length;
for (let i = 0; i < n; i++) {
if (arr[i] === '?') {
let left = (i > 0) ? arr[i - 1] : ' ';
let right = (i + 1 < n) ? arr[i + 1] : ' ';
arr[i] = (left !== 'a' && right !== 'a') ? 'a' :
(left !== 'b' && right !== 'b') ? 'b' : 'c';
}
}
return arr.join('');
};
```
✨ AN UPVOTE WILL BE APPRECIATED ^_~ ✨
``` | 8 | 0 | ['String', 'Python', 'C++', 'Java', 'Python3', 'JavaScript'] | 0 |
replace-all-s-to-avoid-consecutive-repeating-characters | Python 3 Solution for Beginners ( 3 letter soln) | python-3-solution-for-beginners-3-letter-geau | \nclass Solution:\n def modifyString(self, s: str) -> str:\n if len(s)==1: #if input contains only a \'?\'\n if s[0]==\'?\':\n | nikhilsmanu | NORMAL | 2020-09-06T04:37:07.589668+00:00 | 2020-09-06T04:41:53.043467+00:00 | 1,448 | false | ```\nclass Solution:\n def modifyString(self, s: str) -> str:\n if len(s)==1: #if input contains only a \'?\'\n if s[0]==\'?\':\n return \'a\'\n s=list(s)\n for i in range(len(s)):\n if s[i]==\'?\':\n for c in \'abc\':\n if i==0 and s[i+1]!=c: #if i=0 means it is first letter so there is no s[ i-1]\n s[i]=c\n break\n if i==len(s)-1 and s[i-1]!=c: #if i=len(s) means it is last letter so there is no s[i+1]\n s[i]=c\n break\n if (i>0 and i<len(s)-1) and s[i-1]!=c and s[i+1]!=c: \n s[i]=c\n break\n return \'\'.join(s)\n\t``` | 8 | 1 | ['Python', 'Python3'] | 1 |
replace-all-s-to-avoid-consecutive-repeating-characters | C++ O(n) 0ms solution | c-on-0ms-solution-by-ashshetty333-w2tq | \nclass Solution {\npublic:\n string modifyString(string s) {\n for(int i=0; i<s.size(); ++i)\n {\n if(s[i]==\'?\')\n {\n | ashshetty333 | NORMAL | 2020-09-09T09:54:45.986671+00:00 | 2020-09-09T09:54:45.986700+00:00 | 411 | false | ```\nclass Solution {\npublic:\n string modifyString(string s) {\n for(int i=0; i<s.size(); ++i)\n {\n if(s[i]==\'?\')\n {\n s[i]=\'a\';\n while((i>0 && s[i]==s[i-1]) || ((i+1)<s.size() && s[i]==s[i+1]))\n {\n s[i]++;\n }\n }\n }\n return s;\n }\n};\n``` | 7 | 0 | [] | 0 |
replace-all-s-to-avoid-consecutive-repeating-characters | Python | Easy Solution✅ | python-easy-solution-by-gmanayath-ettc | Code\u2705\n\nclass Solution:\n def modifyString(self, s: str) -> str:\n if s =="?":\n return "a"\n if len(s) == 1:\n ret | gmanayath | NORMAL | 2023-02-03T07:50:55.772381+00:00 | 2023-02-03T07:50:55.772434+00:00 | 971 | false | # Code\u2705\n```\nclass Solution:\n def modifyString(self, s: str) -> str:\n if s =="?":\n return "a"\n if len(s) == 1:\n return s\n \n s_list = [x for x in s]\n for index, letter in enumerate(s_list):\n replace_char = {"a","b","c"} \n consecutive_set = set()\n if index == 0 and s_list[index+1] != "?":\n consecutive_set = {s_list[index+1]}\n elif index == len(s_list)-1 and s_list[index-1] != "?":\n consecutive_set = {s_list[index-1]}\n else: \n consecutive_set = {s_list[index-1]} if s_list[index+1] == "?" else {s_list[index-1],s_list[index+1]}\n if letter == "?":\n replace_char = replace_char - consecutive_set\n s_list[index] = replace_char.pop()\n return "".join(s_list)\n``` | 6 | 0 | ['String', 'Python', 'Python3'] | 0 |
replace-all-s-to-avoid-consecutive-repeating-characters | Easy to understand Java solution beats 100% | easy-to-understand-java-solution-beats-1-g9cw | \nclass Solution {\n public String modifyString(String s) {\n char[] S = s.toCharArray();\n for (int i = 0; i < S.length; i++) {\n i | endianless | NORMAL | 2021-03-03T02:00:47.951507+00:00 | 2021-03-03T02:00:47.951564+00:00 | 427 | false | ```\nclass Solution {\n public String modifyString(String s) {\n char[] S = s.toCharArray();\n for (int i = 0; i < S.length; i++) {\n if (S[i] != \'?\') continue;\n char prev = i > 0 ? S[i - 1] : (char) (\'a\' - 1);\n char next = i < S.length - 1 ? S[i + 1] : (char) (\'z\' + 1);\n char c = \'a\';\n while (c == prev || c == next) c++;\n S[i] = c;\n }\n return String.valueOf(S);\n }\n}\n``` | 6 | 0 | [] | 0 |
replace-all-s-to-avoid-consecutive-repeating-characters | python solution | python-solution-by-akaghosting-ia2t | \tclass Solution:\n\t\tdef modifyString(self, s: str) -> str:\n\t\t\tif s == "?":\n\t\t\t\treturn "a"\n\t\t\tletters = "abcdefghijklmnopqrstuvwxyz"\n\t\t\tres = | akaghosting | NORMAL | 2020-09-06T04:23:17.749337+00:00 | 2020-09-06T04:23:17.749392+00:00 | 555 | false | \tclass Solution:\n\t\tdef modifyString(self, s: str) -> str:\n\t\t\tif s == "?":\n\t\t\t\treturn "a"\n\t\t\tletters = "abcdefghijklmnopqrstuvwxyz"\n\t\t\tres = ""\n\t\t\tfor i in range(len(s)):\n\t\t\t\tif s[i] != "?":\n\t\t\t\t\tres += s[i]\n\t\t\t\telse:\n\t\t\t\t\tif i == 0:\n\t\t\t\t\t\tfor j in letters:\n\t\t\t\t\t\t\tif j != s[1]:\n\t\t\t\t\t\t\t\tres += j\n\t\t\t\t\t\t\t\tbreak\n\t\t\t\t\telif i == len(s) - 1:\n\t\t\t\t\t\tfor j in letters:\n\t\t\t\t\t\t\tif j != res[i - 1]:\n\t\t\t\t\t\t\t\tres += j\n\t\t\t\t\t\t\t\tbreak\n\t\t\t\t\telse:\n\t\t\t\t\t\tfor j in letters:\n\t\t\t\t\t\t\tif j != res[i - 1] and j != s[i + 1]:\n\t\t\t\t\t\t\t\tres += j\n\t\t\t\t\t\t\t\tbreak\n\t\t\treturn res | 6 | 1 | [] | 2 |
replace-all-s-to-avoid-consecutive-repeating-characters | JavaScript | javascript-by-adrianlee0118-i2px | \nconst modifyString = s => {\n for (let i = 0; i < s.length; i++){ //Iterating through the string\n if (s[i] === \'?\'){ | adrianlee0118 | NORMAL | 2020-09-06T04:00:26.517857+00:00 | 2020-09-06T04:00:26.517903+00:00 | 441 | false | ```\nconst modifyString = s => {\n for (let i = 0; i < s.length; i++){ //Iterating through the string\n if (s[i] === \'?\'){ //If a \'?\' is encountered\n let rep = \'\';\n if (s[i-1] !== \'a\' && s[i+1] !== \'a\') rep = \'a\'; //determine the replacement\n else if (s[i-1] !== \'b\' && s[i+1] !== \'b\') rep = \'b\';\n else rep = \'c\';\n s = s.slice(0,i)+rep+s.slice(i+1); //and replace in string\n }\n }\n return s; //return modified string\n};\n``` | 6 | 0 | [] | 0 |
replace-all-s-to-avoid-consecutive-repeating-characters | JAVA | Easy solution ✅ | java-easy-solution-by-sourin_bruh-6pj7 | Please Upvote :D\njava []\nclass Solution {\n public String modifyString(String s) {\n if (s.equals("?")) { // edge case\n return "a";\n | sourin_bruh | NORMAL | 2023-01-22T18:06:21.683340+00:00 | 2023-01-22T18:11:08.080962+00:00 | 937 | false | # Please Upvote :D\n``` java []\nclass Solution {\n public String modifyString(String s) {\n if (s.equals("?")) { // edge case\n return "a";\n }\n\n char[] a = s.toCharArray();\n for (int i = 0; i < a.length; i++) {\n if (a[i] == \'?\') {\n // corner case 1\n if (i == 0) { \n // look for character until its unequal \n // with character at index i+1\n for (char c = \'a\'; c <= \'z\'; c++) {\n if (c != a[i+1]) {\n a[i] = c;\n break;\n }\n }\n } \n // corner case 1\n else if (i == a.length - 1) { \n // look for character until its unequal \n // with character at index i-1\n for (char c = \'a\'; c <= \'z\'; c++) {\n if (c != a[i-1]) {\n a[i] = c;\n break;\n }\n }\n } \n // regular case\n else {\n // look for character until its unequal \n // with characters at indices i-1 and i+1\n for (char c = \'a\'; c <= \'z\'; c++) {\n if (c != a[i+1] && c != a[i-1]) {\n a[i] = c;\n break;\n }\n }\n }\n }\n }\n\n return new String(a);\n }\n}\n```\n---\n#### Shorter solution:\n``` java []\nclass Solution {\n public String modifyString(String s) { \n char[] a = s.toCharArray();\n for (int i = 0; i<a.length; i++) {\n if (a[i] == \'?\') {\n for (char c = \'a\'; c <= \'z\'; c++) {\n a[i] = c;\n if (i > 0 && a[i-1] == c) {\n continue;\n }\n if (i < a.length - 1 && a[i+1] == c) {\n continue;\n }\n break;\n }\n }\n }\n \n return new String(a);\n }\n}\n```\n---\n**Time Complexity:** $$O(n * 26) => O(n)$$\n**Space Complexity:** $$O(n)$$\n--- | 4 | 0 | ['String', 'Java'] | 1 |
replace-all-s-to-avoid-consecutive-repeating-characters | Simple Python solution | simple-python-solution-by-c_n-ci88 | Idea:\nFor every ? in the string, recplace it with either a, b, or c, as there are only two sides for \neach letter therefor there is no need for other letters | C_N_ | NORMAL | 2021-11-05T11:30:01.287339+00:00 | 2021-11-29T21:19:39.308719+00:00 | 458 | false | **Idea:**\nFor every `?` in the string, recplace it with either `a, b, or c`, as there are only two sides for \neach letter therefor there is no need for other letters in the alphabet to recplace it in order to receive a valid string.\n(The spaces added to the beginning and end of the string are in order to skip checking\nevery iteration` if i>0 and i<n.`)\n\n**Time Complexity:** O(n) (where n is the length of the string.)\nWe iterate over every char in the string 4 times:\n- list(s)\n- in the for loop.\n- string[1:n+1] \n- join\n\n**Space Complexity:** O(n) - (for storing `string`)\n\n```\ndef modifyString(self, s: str) -> str:\n string=[" "]+list(s)+[" "]\n n=len(s)\n \n for i in range(1,n+1): \n if string[i]!="?":\n continue\n for letter in "abc":\n if letter!=string[i-1] and letter!=string[i+1]:\n string[i]=letter\n break\n \n return "".join(string[1:n+1])\n```\n\n*Thanx for reading, please **upvote** if it helped you :)* | 4 | 0 | ['Python3'] | 2 |
replace-all-s-to-avoid-consecutive-repeating-characters | EASY JAVA CODE | easy-java-code-by-190030664-peiy | \nclass Solution {\n public String modifyString(String s) {\n char c;\n char[] chars = s.toCharArray();\n for (int i=0; i<s.length(); i+ | 190030664 | NORMAL | 2021-03-13T05:10:07.012015+00:00 | 2021-03-13T05:10:07.012052+00:00 | 675 | false | ```\nclass Solution {\n public String modifyString(String s) {\n char c;\n char[] chars = s.toCharArray();\n for (int i=0; i<s.length(); i++){\n if(chars[i] == \'?\'){\n for(c=\'a\'; c<=\'z\'; c++){\n if((i == 0 || c != chars[i-1]) && (i == s.length()-1 || c != chars[i+1])) {\n chars[i] = c;\n break;\n }\n }\n }\n }\n return String.valueOf(chars);\n }\n}\n``` | 4 | 0 | ['String', 'Java'] | 1 |
replace-all-s-to-avoid-consecutive-repeating-characters | simple and easy C++ solution 😍❤️🔥 | simple-and-easy-c-solution-by-shishirrsi-uxt1 | if it\'s help, please up \u2B06 vote! \u2764\uFE0F\n\n\n# Code\n\nclass Solution {\npublic:\n bool isValidIndex(int i, int n)\n {\n return (i >= 0 | shishirRsiam | NORMAL | 2024-06-14T15:20:51.096025+00:00 | 2024-06-14T15:20:51.096058+00:00 | 273 | false | # if it\'s help, please up \u2B06 vote! \u2764\uFE0F\n\n\n# Code\n```\nclass Solution {\npublic:\n bool isValidIndex(int i, int n)\n {\n return (i >= 0 and i < n);\n }\n string modifyString(string s) \n {\n int n = s.size();\n if(n == 1 and s[0] == \'?\') return "a";\n for(int i=0;i<n;i++)\n {\n if(s[i] == \'?\')\n {\n if(isValidIndex(i+1, n) and isValidIndex(i-1, n))\n {\n for(char ch=\'a\';ch<=\'z\';ch++)\n {\n if(s[i-1] != ch and s[i+1] != ch)\n {\n s[i] = ch;\n break;\n }\n }\n }\n else if(!isValidIndex(i+1, n) and isValidIndex(i-1, n))\n {\n for(char ch=\'a\';ch<=\'z\';ch++)\n {\n if(s[i-1] != ch)\n {\n s[i] = ch;\n break;\n }\n }\n }\n else if(isValidIndex(i+1, n) and !isValidIndex(i-1, n))\n {\n for(char ch=\'a\';ch<=\'z\';ch++)\n {\n if(s[i+1] != ch)\n {\n s[i] = ch;\n break;\n }\n }\n }\n }\n } \n return s;\n }\n};\n``` | 3 | 0 | ['String', 'Simulation', 'C++'] | 3 |
replace-all-s-to-avoid-consecutive-repeating-characters | Python random choice solution | python-random-choice-solution-by-inversi-z2xh | Intuition\n Describe your first thoughts on how to solve this problem. \nI came up with this random choice solution. \nHowever, I realized that we only need thr | inversion39 | NORMAL | 2022-12-20T03:55:53.017538+00:00 | 2022-12-20T03:55:53.017578+00:00 | 665 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\nI came up with this random choice solution. \nHowever, I realized that we only need three letters when I checked out the other solutions. LOL\nAnyway, the upper lengh of the string is 100. \nSo I guess there is no big difference in time complexity.\n\n# Code\n```python\nclass Solution:\n def modifyString(self, s: str) -> str:\n s = list("#" + s + "#")\n for i in range(1, len(s) - 1):\n if s[i] == "?":\n while True:\n char = random.choice(string.ascii_lowercase)\n if char not in [s[i-1], s[i+1]]:\n s[i] = char\n break\n\n return "".join(s[1:-1])\n``` | 3 | 0 | ['Python3'] | 1 |
replace-all-s-to-avoid-consecutive-repeating-characters | javaScript, Simple Solution | javascript-simple-solution-by-petr12332-4q2p | var modifyString = function (s) {\n let arr = s.split("");\n for (let i = 0; i <= arr.length; i++) {\n let res = ["a", "b", "c"];\n if (arr[i] === "?") | Petr12332 | NORMAL | 2022-12-05T12:01:11.085837+00:00 | 2022-12-05T13:21:47.986569+00:00 | 680 | false | var modifyString = function (s) {\n let arr = s.split("");\n for (let i = 0; i <= arr.length; i++) {\n let res = ["a", "b", "c"];\n if (arr[i] === "?") {\n if (arr[i - 1] === "a" || arr[i + 1] === "a") {\n res.splice(res.indexOf("a"), 1);\n }\n if (arr[i - 1] === "b" || arr[i + 1] === "b") {\n res.splice(res.indexOf("b"), 1);\n }\n arr[i] = res[0];\n }\n }\n return arr.join("");\n}; | 3 | 0 | ['JavaScript'] | 0 |
replace-all-s-to-avoid-consecutive-repeating-characters | JavaScript Simple Solution (Faster than 99.4%) | javascript-simple-solution-faster-than-9-3vx0 | javascript\nconst modifyString = function(s) {\n let res = [...s];\n \n for (let i = 0; i < res.length; i++) {\n if (res[i] !== "?") continue;\n if (re | Cookie_Ryu | NORMAL | 2021-07-21T14:21:17.187636+00:00 | 2021-07-21T14:21:17.187673+00:00 | 304 | false | ```javascript\nconst modifyString = function(s) {\n let res = [...s];\n \n for (let i = 0; i < res.length; i++) {\n if (res[i] !== "?") continue;\n if (res[i-1] !== "a" && res[i+1] !== "a") { res[i] = "a"; continue; }\n if (res[i-1] !== "b" && res[i+1] !== "b") { res[i] = "b"; continue; }\n res[i] = "c";\n }\n \n return res.join("");\n};\n``` | 3 | 0 | ['JavaScript'] | 1 |
replace-all-s-to-avoid-consecutive-repeating-characters | As easy as "abc" | as-easy-as-abc-by-ecgan-x5w4 | js\nconst convertChar = (arr, i) => {\n if (arr[i] !== \'?\') {\n return arr[i]\n }\n\n if (arr[i - 1] !== \'a\' && arr[i + 1] !== \'a\') {\n return \' | ecgan | NORMAL | 2020-09-06T09:05:29.143489+00:00 | 2020-09-06T09:07:44.805888+00:00 | 449 | false | ```js\nconst convertChar = (arr, i) => {\n if (arr[i] !== \'?\') {\n return arr[i]\n }\n\n if (arr[i - 1] !== \'a\' && arr[i + 1] !== \'a\') {\n return \'a\'\n }\n\n if (arr[i - 1] !== \'b\' && arr[i + 1] !== \'b\') {\n return \'b\'\n }\n\n return \'c\'\n}\n\n/**\n * @param {string} s\n * @return {string}\n */\nconst modifyString = function (s) {\n const arr = s.split(\'\')\n\n for (let i = 0; i < s.length; i++) {\n arr[i] = convertChar(arr, i)\n }\n\n return arr.join(\'\')\n}\n\nmodule.exports = modifyString\n```\n\n---\n\nSee more of my LeetCode JavaScript solutions with 100% code coverage tests: https://github.com/ecgan/leetcode | 3 | 0 | ['JavaScript'] | 0 |
replace-all-s-to-avoid-consecutive-repeating-characters | Simple Python, We just need 3 characters('a', 'b', 'c') | simple-python-we-just-need-3-charactersa-029y | Okay so the idea is basically, we dont want any 2 characters to be consecutive, so all we have to make sure that: if both the adjacent characters are not \'a\', | shubhitt | NORMAL | 2020-09-06T04:06:14.668803+00:00 | 2020-09-06T04:06:14.668905+00:00 | 247 | false | Okay so the idea is basically, we dont want any 2 characters to be consecutive, so all we have to make sure that: if both the adjacent characters are not \'a\', we can replace \'?\' by \'a\'\nsimilarly with \'b\' and \'c\'\n\n```\nclass Solution:\n def modifyString(self, s: str) -> str:\n s = \'1\' + s + \'1\'\n for i in range(1, len(s) - 1):\n if s[i] == \'?\':\n if s[i-1] != \'a\' and s[i+1] != \'a\':\n s = s[:i] + \'a\' + s[i+1:]\n elif s[i-1] != \'b\' and s[i+1] != \'b\':\n s = s[:i] + \'b\' + s[i+1:]\n elif s[i-1] != \'c\' and s[i+1] != \'c\':\n s = s[:i] + \'c\' + s[i+1:]\n \n s = s[1:-1]\n return s\n \n``` | 3 | 1 | [] | 0 |
replace-all-s-to-avoid-consecutive-repeating-characters | Very simple & easy to understand. | very-simple-easy-to-understand-by-srh_ab-cf72 | \n# Code\npython3 []\nclass Solution:\n def modifyString(self, s: str) -> str:\n # Convert the string to a list of characters since strings are immuta | srh_abhay | NORMAL | 2024-11-18T02:52:18.266817+00:00 | 2024-11-18T02:52:47.874760+00:00 | 98 | false | \n# Code\n```python3 []\nclass Solution:\n def modifyString(self, s: str) -> str:\n # Convert the string to a list of characters since strings are immutable\n s = list(s)\n \n # Helper function to get a valid character for replacement\n def get_valid_char(i):\n # We need to make sure that the character at index i doesn\'t equal its neighbors\n for char in \'abcdefghijklmnopqrstuvwxyz\':\n # Check if the current character is not the same as the previous or next character\n if (i > 0 and s[i - 1] == char) or (i < len(s) - 1 and s[i + 1] == char):\n continue\n return char\n return \'a\' # This line will never be reached as there\'s always a valid character\n \n # Loop through the string and replace each \'?\'\n for i in range(len(s)):\n if s[i] == \'?\':\n s[i] = get_valid_char(i)\n \n # Convert the list back to a string and return it\n return \'\'.join(s)\n\n\n``` | 2 | 0 | ['Python3'] | 0 |
replace-all-s-to-avoid-consecutive-repeating-characters | Python Easy Solution | python-easy-solution-by-sumedh0706-b1nu | Code\n\nclass Solution:\n def modifyString(self, s: str) -> str:\n new=s\n for i in range(len(s)):\n if new[i]=="?":\n | Sumedh0706 | NORMAL | 2023-06-30T10:41:47.010466+00:00 | 2023-06-30T10:41:47.010494+00:00 | 189 | false | # Code\n```\nclass Solution:\n def modifyString(self, s: str) -> str:\n new=s\n for i in range(len(s)):\n if new[i]=="?":\n if len(new)==1:\n return "p"\n if i==0:\n lis=[new[i+1]]\n if i==len(s)-1:\n lis=[new[i-1]]\n else:\n lis=[new[i-1],new[i+1]]\n n="a"\n while(n in lis):\n n=chr(ord(n)+1) \n new=new[:i]+n+new[i+1:]\n return new\n``` | 2 | 0 | ['Python3'] | 0 |
replace-all-s-to-avoid-consecutive-repeating-characters | CPP || EASY || 3ms | cpp-easy-3ms-by-peeronappper-2iuo | \nclass Solution {\npublic:\n string modifyString(string s) {\n for(int i=0;i<s.length();i++){\n if(i==0 && s[i]==\'?\') s[i]=(s[i+1]+1)%26 | PeeroNappper | NORMAL | 2022-06-13T11:08:09.207677+00:00 | 2022-06-13T11:08:09.207717+00:00 | 235 | false | ```\nclass Solution {\npublic:\n string modifyString(string s) {\n for(int i=0;i<s.length();i++){\n if(i==0 && s[i]==\'?\') s[i]=(s[i+1]+1)%26+\'a\';\n else if(i==s.length()-1 && s[i]==\'?\') s[i]=(s[i-1]-\'a\'+1)%26+\'a\';\n else if(s[i]==\'?\'){\n int p=1;\n while(s[i]==s[i-1] || s[i]==s[i+1] || s[i]==\'?\'){\n s[i]=(max(s[i-1]-\'a\',s[i+1]-\'a\')+p)%26+\'a\';\n p++;\n }\n }\n }\n return s;\n }\n};\n``` | 2 | 0 | ['C++'] | 0 |
replace-all-s-to-avoid-consecutive-repeating-characters | Golang Simple O(n) | golang-simple-on-by-alfinm01-aerq | \nfunc modifyString(s string) string {\n str := strings.Split(s, "")\n chars := []string{"a", "b", "c"}\n\t\n for i := range str {\n if str[i] ! | alfinm01 | NORMAL | 2021-11-08T02:39:45.218523+00:00 | 2022-07-25T09:50:06.891231+00:00 | 147 | false | ```\nfunc modifyString(s string) string {\n str := strings.Split(s, "")\n chars := []string{"a", "b", "c"}\n\t\n for i := range str {\n if str[i] != "?" {\n\t\t\tcontinue\n\t\t}\n\t\t\n\t\tfor _, c := range chars {\n\t\t\tif (i == 0 || str[i-1] != c) && (i == len(s)-1 || str[i+1] != c) {\n\t\t\t\tstr[i] = c\n\t\t\t\tbreak\n\t\t\t}\n\t\t}\n }\n\t\n return strings.Join(str[:], "")\n}\n``` | 2 | 0 | ['Go'] | 0 |
replace-all-s-to-avoid-consecutive-repeating-characters | [Python3] Faster than 90% submissions, simple code | python3-faster-than-90-submissions-simpl-wxg5 | Execution Result\n\n\nRuntime: 28 ms, faster than 90.93% of Python3 online submissions for Replace All ?\'s to Avoid Consecutive Repeating Characters.\nMemory U | GauravKK08 | NORMAL | 2021-07-05T14:49:52.039789+00:00 | 2021-07-05T14:50:51.529125+00:00 | 155 | false | `Execution Result`\n\n```\nRuntime: 28 ms, faster than 90.93% of Python3 online submissions for Replace All ?\'s to Avoid Consecutive Repeating Characters.\nMemory Usage: 14.1 MB, less than 92.19% of Python3 online submissions for Replace All ?\'s to Avoid Consecutive Repeating Characters.\n```\n\n`Solution`\n```\nclass Solution:\n def modifyString(self, s: str) -> str:\n chars = \'abcdefghijklmnopqrstuvwxyz\'\n s = list(s)\n while s.count(\'?\')!=0:\n index = s.index(\'?\')\n\n _prev = s[index-1] if index-1 >=0 else \'\'\n _next = s[index+1] if index+1 <= len(s)-1 else \'\'\n\n for char in chars:\n if char != _prev and char != _next:\n s[index] = char\n break\n return \'\'.join(s)\n```\n\n`Tested using below lines of code`\n```\nprint(Solution().modifyString(s = "?zs"))\nprint(Solution().modifyString(s = "ubv?w"))\nprint(Solution().modifyString(s = "j?qg??b"))\nprint(Solution().modifyString(s = "??yw?ipkj?"))\n``` | 2 | 0 | ['Python3'] | 1 |
replace-all-s-to-avoid-consecutive-repeating-characters | C++ 100% Faster | c-100-faster-by-ayush_gupta4-k3gy | \nclass Solution {\npublic:\n string modifyString(string s) {\n int i,n=s.size();\n for(i=0;i<n;i++){\n if(s[i]==\'?\'){\n | ayush_gupta4 | NORMAL | 2021-06-05T08:49:05.138769+00:00 | 2021-06-05T08:49:05.138801+00:00 | 97 | false | ```\nclass Solution {\npublic:\n string modifyString(string s) {\n int i,n=s.size();\n for(i=0;i<n;i++){\n if(s[i]==\'?\'){\n s[i]=\'a\';\n while((i>0 && s[i]==s[i-1]) || (i+1<n && s[i]==s[i+1])) s[i]++;\n }\n }\n return s;\n }\n};\n``` | 2 | 0 | [] | 0 |
replace-all-s-to-avoid-consecutive-repeating-characters | Javascript easy to read | javascript-easy-to-read-by-fbecker11-2low | \nvar modifyString = function(s) {\n const arr = s.split(\'\')\n let res = \'\'\n for(let i=0;i<s.length;i++){\n const curr = arr[i]\n if(curr !== \'?\ | fbecker11 | NORMAL | 2021-05-01T14:40:17.607185+00:00 | 2021-05-01T14:41:22.811547+00:00 | 231 | false | ```\nvar modifyString = function(s) {\n const arr = s.split(\'\')\n let res = \'\'\n for(let i=0;i<s.length;i++){\n const curr = arr[i]\n if(curr !== \'?\') continue\n const prevCharCode = (arr[i-1] || \'\').charCodeAt(0)\n const nextCharCode = (arr[i+1] || \'\').charCodeAt(0)\n let random = 0\n while(!random){\n random = Math.floor(Math.random() * 26);\n random += 97 // starting at \'a\'\n if(random === prevCharCode || random === nextCharCode){\n random = 0\n continue\n } \n arr[i]= String.fromCharCode(random)\n break\n } \n }\n return arr.join(\'\')\n};\n``` | 2 | 0 | ['JavaScript'] | 0 |
replace-all-s-to-avoid-consecutive-repeating-characters | C# solution | c-solution-by-daraa-3h4j | \nStringBuilder sb = new StringBuilder();\n sb.Append(s);\n for(int i=0;i<sb.Length;i++){\n if(s[i]==\'?\'){\n for(char | daraa | NORMAL | 2021-04-24T10:29:58.930272+00:00 | 2021-04-24T10:29:58.930303+00:00 | 89 | false | ```\nStringBuilder sb = new StringBuilder();\n sb.Append(s);\n for(int i=0;i<sb.Length;i++){\n if(s[i]==\'?\'){\n for(char j=\'a\'; j<=\'z\';j++){\n if(i-1>=0 && j == sb[i-1]) continue;\n if(i+1<sb.Length && j == sb[i+1]) continue;\n sb[i] = j;\n }\n }\n }\n return sb.ToString();\n``` | 2 | 0 | ['String'] | 0 |
replace-all-s-to-avoid-consecutive-repeating-characters | C++ 0ms | c-0ms-by-leetweeb-mh3o | ```\nclass Solution {\npublic:\n string modifyString(string s) {\n int n = s.size();\n for(int i=0;i=0)\n prev = s[i-1];\n | LeetWeeb | NORMAL | 2021-04-13T08:37:25.220937+00:00 | 2021-04-13T08:37:25.220977+00:00 | 69 | false | ```\nclass Solution {\npublic:\n string modifyString(string s) {\n int n = s.size();\n for(int i=0;i<n;i++)\n {\n if(s[i] == \'?\')\n {\n char c = \'a\';\n char prev = \'a\',next = \'a\';\n if(i-1 >=0)\n prev = s[i-1];\n if(i+1 < n)\n next = s[i+1];\n while(c == prev || c == next)\n c++;\n s[i] = c;\n }\n }\n return s; \n }\n}; | 2 | 0 | [] | 0 |
replace-all-s-to-avoid-consecutive-repeating-characters | easy to understand java | easy-to-understand-java-by-d_eat_h-teko | ```\nclass Solution {\n public String modifyString(String s) {\n char[] arr = s.toCharArray();\n for(int i = 0; i < arr.length; i++){\n | D_eat_H | NORMAL | 2021-04-11T01:34:36.816454+00:00 | 2021-04-11T01:34:36.816493+00:00 | 79 | false | ```\nclass Solution {\n public String modifyString(String s) {\n char[] arr = s.toCharArray();\n for(int i = 0; i < arr.length; i++){\n if(s.charAt(i) != \'?\')\n continue;\n for(char ch = \'a\'; ch <= \'z\'; ch++){\n if(satisfies(arr, ch, i)){\n arr[i] = ch;\n break; \n }\n }\n }\n return new String(arr);\n }\n \n public boolean satisfies(char[] arr, char ch, int index){\n return satisfiesLeft(arr, ch, index) && satisfiesRight(arr, ch, index);\n }\n \n public boolean satisfiesLeft(char[] arr, char ch, int index){\n if(index == 0)\n return true;\n return arr[index - 1] != ch;\n }\n \n public boolean satisfiesRight(char[] arr, char ch, int index){\n if(index == arr.length - 1)\n return true;\n return arr[index + 1] != ch;\n }\n \n} | 2 | 1 | [] | 1 |
replace-all-s-to-avoid-consecutive-repeating-characters | C# straightforward solution 76ms | c-straightforward-solution-76ms-by-iiii1-uhgl | \npublic class Solution {\n public string ModifyString(string s) {\n char[] letters = new char[]{\'a\',\'b\',\'c\',\'d\',\'e\',\'f\',\'g\',\'h\',\'i\' | iiii110270 | NORMAL | 2021-03-25T06:07:53.751056+00:00 | 2021-03-25T06:07:53.751089+00:00 | 69 | false | ```\npublic class Solution {\n public string ModifyString(string s) {\n char[] letters = new char[]{\'a\',\'b\',\'c\',\'d\',\'e\',\'f\',\'g\',\'h\',\'i\',\n \'j\',\'k\',\'l\',\'m\',\'n\',\'o\',\'p\',\'q\',\'r\',\n \'s\',\'t\',\'u\',\'v\',\'w\',\'x\',\'y\',\'z\'};\n char[] ans = s.ToCharArray();\n for(int i=0; i<ans.Length; i++)\n {\n if(ans[i] != \'?\') continue;\n \n char[] sides = new char[2];\n if(i != 0) sides[0] = ans[i-1];\n if(i != ans.Length-1)sides[1] = ans[i+1];\n ans[i] = letters.Where(o => !sides.Contains(o)).FirstOrDefault();\n }\n return new string(ans);\n }\n}\n``` | 2 | 0 | [] | 0 |
replace-all-s-to-avoid-consecutive-repeating-characters | c++ simple solution (0ms) | c-simple-solution-0ms-by-dilipsuthar60-umh1 | \nclass Solution {\npublic:\n string modifyString(string s) \n {\n int n=s.size();\n for(int i=0;i<n;i++)\n {\n if(s[i]==\ | dilipsuthar17 | NORMAL | 2021-02-04T15:48:48.810259+00:00 | 2021-02-04T15:48:48.810310+00:00 | 112 | false | ```\nclass Solution {\npublic:\n string modifyString(string s) \n {\n int n=s.size();\n for(int i=0;i<n;i++)\n {\n if(s[i]==\'?\')\n {\n s[i]=\'a\';\n while(((i>0)&&(s[i-1]==s[i]))||(i+1<n&&(s[i+1]==s[i])))\n {\n s[i]++;\n }\n }\n }\n return s;\n }\n};\n``` | 2 | 0 | [] | 2 |
replace-all-s-to-avoid-consecutive-repeating-characters | Java Straightforward Solution | java-straightforward-solution-by-ensifer-36u8 | \nclass Solution {\n public String modifyString(String s) {\n char[] res = s.toCharArray();\n for(int i = 0; i < res.length; i++) {\n | ensiferum | NORMAL | 2021-01-30T19:53:00.086126+00:00 | 2021-01-31T00:39:32.631544+00:00 | 177 | false | ```\nclass Solution {\n public String modifyString(String s) {\n char[] res = s.toCharArray();\n for(int i = 0; i < res.length; i++) {\n char c = res[i];\n if(c == \'?\') {\n char repl = \'a\';\n while((i > 0 && res[i - 1] == repl)\n || (i < res.length - 1 && res[i + 1] == repl)) {\n repl++;\n }\n res[i] = repl;\n } \n }\n return String.valueOf(res);\n }\n}\n``` | 2 | 0 | [] | 0 |
replace-all-s-to-avoid-consecutive-repeating-characters | simple solution - python | simple-solution-python-by-waveletus-jvpy | \nclass Solution:\n def modifyString(self, s: str) -> str:\n res = list(s)\n \n for i in range(len(res)):\n if res[i] == \'?\ | waveletus | NORMAL | 2020-11-23T04:01:42.448679+00:00 | 2020-11-23T04:01:42.448705+00:00 | 155 | false | ```\nclass Solution:\n def modifyString(self, s: str) -> str:\n res = list(s)\n \n for i in range(len(res)):\n if res[i] == \'?\':\n left = res[i-1] if i > 0 else \'\'\n right = res[i+1] if i < len(s) - 1 else \'\'\n \n for ch in \'abc\':\n if ch != left and ch != right:\n res[i] = ch\n break\n \n return \'\'.join(res)\n``` | 2 | 0 | [] | 0 |
replace-all-s-to-avoid-consecutive-repeating-characters | Python solution faster than 100 percent! | python-solution-faster-than-100-percent-2clfn | \nclass Solution:\n def modifyString(self, s: str) -> str:\n ans=\'\'\n if len(s)==1:\n if s[0]==\'?\':\n return \'a\ | man_it | NORMAL | 2020-09-07T10:28:57.130515+00:00 | 2020-09-07T10:28:57.130569+00:00 | 628 | false | ```\nclass Solution:\n def modifyString(self, s: str) -> str:\n ans=\'\'\n if len(s)==1:\n if s[0]==\'?\':\n return \'a\'\n else:\n return s\n \n if s[0]=="?":\n for j in range(97,124):\n if ord(s[1])!=j:\n ans+=chr(j)\n break\n else:\n ans+=s[0]\n for i in range(1,len(s)):\n if s[i]=="?":\n if i==len(s)-1:\n for j in range(97,124):\n if ord(ans[i-1])!=j:\n ans+=chr(j)\n break\n else:\n for j in range(97,124):\n if ord(ans[i-1])!=j and ord(s[i+1])!=j:\n ans+=chr(j)\n break\n else:\n ans+=s[i]\n return ans\n``` | 2 | 0 | ['Python', 'Python3'] | 0 |
replace-all-s-to-avoid-consecutive-repeating-characters | C++ O(N) One Pass with explanation | c-on-one-pass-with-explanation-by-lzl124-w73e | See my latest update in repo LeetCode\n\n## Solution 1.\n\nWhen we see a s[i] == \'?\', we set it as s[i - 1] + 1 and round it to \'a\' if necessary.\n\nWhen s[ | lzl124631x | NORMAL | 2020-09-06T04:08:40.041625+00:00 | 2020-09-06T04:11:55.882085+00:00 | 187 | false | See my latest update in repo [LeetCode](https://github.com/lzl124631x/LeetCode)\n\n## Solution 1.\n\nWhen we see a `s[i] == \'?\'`, we set it as `s[i - 1] + 1` and round it to `\'a\'` if necessary.\n\nWhen `s[i] == \'?\' && s[i + 1] != \'?\'`, there is a chance of conflict with `s[i + 1]`. If there is a conflict, we simply increment and round `s[i]` again.\n\n```cpp\n// OJ: https://leetcode.com/problems/replace-all-s-to-avoid-consecutive-repeating-characters/\n// Author: github.com/lzl124631x\n// Time: O(N)\n// Space: O(1)\nclass Solution {\npublic:\n string modifyString(string s) {\n int i = 0, N = s.size();\n while (i < N) {\n while (i < N && s[i] != \'?\') ++i;\n int c = (i == 0 ? 0 : (s[i - 1] - \'a\' + 1) % 26);\n while (i < N && s[i] == \'?\') {\n s[i] = c + \'a\';\n c = (c + 1) % 26;\n if (i + 1 < N && s[i + 1] != \'?\' && s[i] == s[i + 1]) s[i] = c + \'a\';\n }\n }\n return s;\n }\n};\n``` | 2 | 0 | [] | 0 |
replace-all-s-to-avoid-consecutive-repeating-characters | 100% beats in c++ solution. | 100-beats-in-c-solution-by-jahidulcse15-80im | IntuitionApproachComplexity
Time complexity:O(N)
Space complexity:O(N)
Code | jahidulcse15 | NORMAL | 2025-04-12T05:50:24.091296+00:00 | 2025-04-12T05:50:24.091296+00:00 | 1 | false | # Intuition
<!-- Describe your first thoughts on how to solve this problem. -->
# Approach
<!-- Describe your approach to solving the problem. -->
# Complexity
- Time complexity:O(N)
<!-- Add your time complexity here, e.g. $$O(n)$$ -->
- Space complexity:O(N)
<!-- Add your space complexity here, e.g. $$O(n)$$ -->
# Code
```cpp []
class Solution {
public:
string modifyString(string s) {
if(s.size()==1){
if(s[0]=='?'){
s[0]='s';
}
return s;
}
string s1;
for(int ch='a';ch<='z';ch++){
s1+=ch;
}
for(int i=1;i<s.size();i++){
if(s[i]=='?'){
for(int j=0;j<s1.size();j++){
if(s1[j]!=s[i-1]&&s1[j]!=s[i+1]){
s[i]=s1[j];
break;
}
}
}
}
if(s[0]=='?'){
if(s[1]=='z'){
s[0]='a';
}
else{
s[0]=s[1]+1;
}
}
if(s[s.size()-1]=='?'){
if(s[s.size()-2]=='z'){
s[s.size()-1]='a';
}
else{
s[s.size()-1]=s[s.size()-2]+1;
}
}
return s;
}
};
``` | 1 | 0 | ['C++'] | 0 |
replace-all-s-to-avoid-consecutive-repeating-characters | <<BEAT 100% SOLUTIONS>> | beat-100-solutions-by-dakshesh_vyas123-09mu | PLEASE UPVOTE MECode | Dakshesh_vyas123 | NORMAL | 2025-03-18T16:41:35.442207+00:00 | 2025-03-18T16:41:35.442207+00:00 | 21 | false | # PLEASE UPVOTE ME
# Code
```cpp []
class Solution {
public:
string modifyString(string s) {
for (auto i = 0; i < s.size(); ++i)
if (s[i] == '?')
for (s[i] = 'a'; s[i] <= 'c'; ++s[i])
if ((i == 0 || s[i - 1] != s[i]) && (i == s.size() - 1 || s[i + 1] != s[i]))
break;
return s;
}
};
``` | 1 | 0 | ['C++'] | 0 |
replace-all-s-to-avoid-consecutive-repeating-characters | Easy Solution for beginners || Beats 100%+✨ || ✌️🥱 | easy-solution-for-beginners-beats-100-by-0at5 | \n# Approach:\n Describe your approach to solving the problem. \nTo solve this, we turn the string into a list to easily change its characters. As we go through | 07_deepak | NORMAL | 2024-10-22T12:44:42.643009+00:00 | 2024-10-22T12:45:29.805009+00:00 | 16 | false | \n# Approach:\n<!-- Describe your approach to solving the problem. -->\n*To solve this, we turn the string into a list to easily change its characters. As we go through the string, we replace each `\'?\'` with a letter, making sure it\u2019s different from its neighbors. For the first and last characters, we only check one side. Once all the `\'?\'` are replaced, we join the list back into a string and return it, ensuring no two adjacent characters are the same.*\n\n# Complexity\n- Time complexity: **O(N)**\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity: **O(N)**\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```python3 []\nclass Solution:\n def modifyString(self, s: str) -> str:\n l=[\'a\',\'b\',\'c\',\'d\',\'e\',\'f\',\'g\',\'x\',\'q\']\n s=list(s)\n for i in range(len(s)):\n if s[i]==\'?\':\n for j in range(len(l)):\n if (i == 0 or s[i-1] != l[j]) and (i == len(s)-1 or s[i+1] != l[j]):\n s[i] = l[j]\n break\n return \'\'.join(s)\n\n``` | 1 | 1 | ['Python', 'Python3'] | 0 |
replace-all-s-to-avoid-consecutive-repeating-characters | Easy C++ Solution | easy-c-solution-by-bharijamegha-tu5g | Intuition\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 | bharijamegha | NORMAL | 2024-10-05T11:05:06.013768+00:00 | 2024-10-05T11:05:06.013796+00:00 | 56 | false | # Intuition\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\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```cpp []\nclass Solution {\npublic:\n string modifyString(string s) {\n string word="";\n int n=s.length();\n for(int i=0;i<n;i++){\n while(i<n && s[i]!=\'?\') {\n word+=s[i];\n i++;\n }\n if(s[i]==\'?\'){\n char left=i>0?word.back():\' \';\n char right=i+1<n?s[i+1]:\' \';\n cout<<left<<" "<<right<<endl;\n for(char ch=\'a\';ch<=\'z\';ch++){\n if(ch==left || ch==right) continue;\n else{\n word+=ch;\n break;\n }\n }\n }\n }\n return word;\n }\n};\n``` | 1 | 0 | ['C++'] | 1 |
replace-all-s-to-avoid-consecutive-repeating-characters | abba accepted !!! Ganapati Bappa Morya ,Mangal Murthi Morya !!!!! :D | abba-accepted-ganapati-bappa-morya-manga-qatf | Intuition\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 | yesyesem | NORMAL | 2024-09-06T18:59:06.257258+00:00 | 2024-09-06T18:59:06.257284+00:00 | 33 | false | # Intuition\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\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```cpp []\nclass Solution {\npublic:\n string modifyString(string s) {\n\n if(s[0]==\'?\')\n {\n int b=s[1];\n\n for(int j=97;j<=122;j++)\n {\n if(j!=b)\n {\n s[0]=char(j);\n break;\n }\n j++;\n }\n }\n\n if(s[s.length()-1]==\'?\')\n {\n int a=int(s[s.length()-1-1]);\n\n for(int j=97;j<=122;j++)\n {\n if(j!=a)\n {\n s[s.length()-1]=char(j);\n break;\n }\n j++;\n }\n\n }\n\n\n for(int i=1;i<s.length()-1;i++)\n {\n if(s[i]==\'?\')\n {\n int a=int(s[i-1]);\n int b=int(s[i+1]);\n\n for(int j=97;j<=122;j++)\n {\n if(j!=a&&j!=b)\n {\n s[i]=char(j);\n break;\n }\n j++;\n }\n }\n }\n\n\n return s;\n }\n};\n``` | 1 | 0 | ['C++'] | 0 |
replace-all-s-to-avoid-consecutive-repeating-characters | Dart Solution | dart-solution-by-abbos2101-c0i8 | \nclass Solution {\n String modifyString(String s) {\n final letters = List.generate(26, (i) => String.fromCharCode(i + 97));\n int index = s.indexOf(\'? | abbos2101 | NORMAL | 2024-07-10T22:10:47.249780+00:00 | 2024-07-10T22:10:47.249808+00:00 | 4 | false | ```\nclass Solution {\n String modifyString(String s) {\n final letters = List.generate(26, (i) => String.fromCharCode(i + 97));\n int index = s.indexOf(\'?\');\n while (index != -1) {\n String first = \'\';\n String second = \'\';\n if (index > 0) first = s[index - 1];\n if (index < s.length - 1) second = s[index + 1];\n\n for (int i = 0; i < letters.length; i++) {\n if (first != letters[i] && second != letters[i]) {\n s = s.replaceRange(index, index + 1, letters[i]);\n break;\n }\n }\n index = s.indexOf(\'?\');\n }\n return s;\n }\n}\n``` | 1 | 0 | ['Dart'] | 0 |
replace-all-s-to-avoid-consecutive-repeating-characters | [Python] ugly code | python-ugly-code-by-pbelskiy-09t9 | \nclass Solution:\n def modifyString(self, s: str) -> str:\n a = list(s)\n\n for i in range(len(a)):\n if a[i] != \'?\':\n | pbelskiy | NORMAL | 2024-05-16T07:27:53.173020+00:00 | 2024-05-16T07:27:53.173052+00:00 | 14 | false | ```\nclass Solution:\n def modifyString(self, s: str) -> str:\n a = list(s)\n\n for i in range(len(a)):\n if a[i] != \'?\':\n continue\n\n used = set()\n if i > 0:\n used.add(a[i - 1])\n if i + 1 < len(s):\n used.add(a[i + 1])\n\n a[i] = next(iter(set(string.ascii_lowercase) - set(used)))\n\n return \'\'.join(a)\n``` | 1 | 0 | ['Python'] | 0 |
replace-all-s-to-avoid-consecutive-repeating-characters | Simple java code 1 ms beats 100 % && 42 mb beats 88.78 % | simple-java-code-1-ms-beats-100-42-mb-be-rhyc | \n# Complexity\n\n- image.png\n# Code\n\nclass Solution {\n public char help(char p,char n){\n for(char c=\'b\';c<=\'y\';c++){\n if(c!=p&&c | Arobh | NORMAL | 2024-03-25T06:23:22.040499+00:00 | 2024-03-25T06:23:22.040518+00:00 | 268 | false | \n# Complexity\n\n- image.png\n# Code\n```\nclass Solution {\n public char help(char p,char n){\n for(char c=\'b\';c<=\'y\';c++){\n if(c!=p&&c!=n) return c;\n }\n return \'x\';\n }\n public String modifyString(String s) {\n StringBuilder sb=new StringBuilder();\n for(int i=0;i<s.length();i++){\n if(s.charAt(i)==\'?\'){\n char p=\'a\';\n char n=\'z\';\n if(i>0) p=sb.charAt(i-1);\n if(i<s.length()-1) n=s.charAt(i+1);\n char c=help(p,n);\n sb.append(c);\n }\n else{\n sb.append(s.charAt(i));\n }\n }\n return sb.toString();\n }\n}\n``` | 1 | 0 | ['String', 'Java'] | 0 |
replace-all-s-to-avoid-consecutive-repeating-characters | Easy to understand solution | easy-to-understand-solution-by-pratik_pa-eghl | Intuition\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 | Pratik_Pathak_05 | NORMAL | 2024-02-10T15:57:38.438396+00:00 | 2024-02-10T15:57:38.438424+00:00 | 14 | false | # Intuition\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\n- Space complexity:\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\n public String modifyString(String s) {\n\n if(s.equals("?")){\n return "p";\n }\n\n char [] arr = s.toCharArray();\n for(int i = 0; i<arr.length; i++){\n if(arr[i] == \'?\'){\n if(i == 0){\n for(char c = \'a\'; c<= \'z\'; c++){\n if(c != arr[i+1]){\n arr[i] = c;\n break;\n }\n }\n }\n else if(i == arr.length -1){\n for(char c = \'a\'; c <= \'z\'; c++){\n if(c != arr[i-1]){\n arr[i] = c;\n break;\n }\n }\n }\n else{\n for(char c = \'a\'; c<= \'z\'; c++){\n if(c != arr[i-1] && c != arr[i+1]){\n arr[i] = c;\n break;\n }\n }\n }\n }\n }\n return new String(arr);\n \n }\n}\n``` | 1 | 0 | ['Java'] | 0 |
replace-all-s-to-avoid-consecutive-repeating-characters | Easy / Simple [ C | C++ | Java ] -- Dry Run Beats 100% in runtime & memory.. | easy-simple-c-c-java-dry-run-beats-100-i-kovd | Intuition\n\nC++ []\nclass Solution {\npublic:\n string modifyString(string s) {\n for (auto i = 0; i < s.size(); ++i)\n if (s[i] == \'?\') | Edwards310 | NORMAL | 2024-02-04T08:04:17.850195+00:00 | 2024-02-04T08:04:17.850219+00:00 | 26 | false | # Intuition\n\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\n | 1 | 0 | ['String', 'C', 'C++', 'Java'] | 0 |
replace-all-s-to-avoid-consecutive-repeating-characters | JS || Soution by Bharadwaj | js-soution-by-bharadwaj-by-manu-bharadwa-r401 | Intuition\nfunction modifyString(s) {\n // Array of lowercase letters for replacement\n const letters = [\'a\', \'b\', ..., \'z\'];\n\n // Iterate through ea | Manu-Bharadwaj-BN | NORMAL | 2024-01-11T06:39:17.647960+00:00 | 2024-01-11T06:39:17.647982+00:00 | 51 | false | # Intuition\nfunction modifyString(s) {\n // Array of lowercase letters for replacement\n const letters = [\'a\', \'b\', ..., \'z\'];\n\n // Iterate through each character in the string\n for (let i = 0; i < s.length; i++) {\n // If the character is \'?\', replace it with a distinct letter\n if (s[i] === \'?\') {\n s = s.replace(\n s[i],\n // Find a letter that\'s different from adjacent characters\n letters.find((el) => el !== s[i - 1] && el !== s[i + 1])\n );\n }\n }\n // Return the modified string\n return s;\n}\n\n\n# Code\n```\nvar modifyString = function (s) {\n const a = [\n \'a\', \'b\', \'c\', \'d\', \'e\', \'f\', \'g\', \'h\', \'i\', \'j\', \'k\', \'l\', \'m\', \'n\', \'o\', \'p\', \'q\', \'r\', \'s\', \'t\', \'u\', \'v\', \'w\', \'x\', \'y\', \'z\',\n ];\n for (let i = 0; i < s.length; i++) {\n if (s[i] === \'?\') {\n s = s.replace(\n s[i],\n a.find(el => el !== s[i - 1] && el !== s[i + 1]),\n );\n }\n }\n return s;\n};\n``` | 1 | 0 | ['JavaScript'] | 1 |
replace-all-s-to-avoid-consecutive-repeating-characters | beats 100% cpp soln | beats-100-cpp-soln-by-vermasachin6101-5ees | Intuition\nIf the input string has length 1 and the only character is \'?\', it replaces it with \'a\'. Otherwise, it returns the original string.\n\nIf the fir | gyuyul | NORMAL | 2023-12-20T15:08:55.262963+00:00 | 2023-12-20T15:08:55.262986+00:00 | 12 | false | # Intuition\nIf the input string has length 1 and the only character is \'?\', it replaces it with \'a\'. Otherwise, it returns the original string.\n\nIf the first character of the string is \'?\', it replaces it with \'a\' if the next character is \'a\', and with \'b\' otherwise.\n\nFor each \'?\' in the middle of the string (excluding the first and last characters), it replaces it with the lexicographically smallest character that is different from both the preceding and succeeding characters.\n\nIf the last character of the string is \'?\', it replaces it with \'a\' if the preceding character is \'a\', and with \'b\' otherwise.\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n\n# Complexity\n- Time complexity:\n0(n*26)\n\n- Space complexity:\n0(26)\n\n# Code\n```\nclass Solution {\npublic:\n string modifyString(string s) {\n int n = s.length();\n if(n==1){\n if(s[0]==\'?\'){\n return "a";\n }\n return s;\n }\n if(s[0]==\'?\'){\n if(s[1]==\'a\'){\n s[0]=\'b\';\n }\n else{\n s[0]=\'a\';\n }\n }\n for(int i = 1; i<= n-2;i++){\n if(s[i]==\'?\'){\n vector<int> v(256,1);\n v[(int)s[i-1]]=0;\n v[(int)s[i+1]]=0;\n for(int k = 97 ; k<125;k++){\n if(v[k]){\n s[i]=char(k);\n break;\n }\n }\n \n }\n \n \n }\n if(s[n-1]==\'?\'){\n if(s[n-2]==\'a\'){\n s[n-1]=\'b\';\n }\n else{\n s[n-1]=\'a\';\n }\n }\n return s;\n }\n};\n``` | 1 | 0 | ['C++'] | 0 |
replace-all-s-to-avoid-consecutive-repeating-characters | Easy Solution for beginners || Beats 95%+✨💫 || 🧛✌️ | easy-solution-for-beginners-beats-95-by-ech8m | Intuition\nIt seems that you want to replace the question mark (\'?\') in the input string \'s\' with lowercase English alphabets (\'a\' to \'z\') in such a way | EraOfKaushik003 | NORMAL | 2023-07-23T09:00:07.826303+00:00 | 2023-07-23T09:02:12.042393+00:00 | 453 | false | # Intuition\nIt seems that you want to replace the question mark (\'?\') in the input string \'s\' with lowercase English alphabets (\'a\' to \'z\') in such a way that adjacent characters are not the same. The code uses a dictionary to keep track of the positions of the question marks and then iterates through the string to perform replacements based on certain conditions.\n\n# Approach\nThe approach followed in the code is to iterate through the positions of the question marks in the string \'s\'. For each position, it tries to replace the question mark with a lowercase English alphabet that is different from its adjacent characters (if any).\n\n# Complexity\n- Time complexity: O(n)\n\n- Space complexity: O(n)\n\n# Code\n```\nclass Solution:\n def modifyString(self, s: str) -> str:\n alp=\'abcdefghijklmnopqrstuvwxyz\'\n #print(dic)\n d=defaultdict(list)\n for i in range(len(s)):\n if s[i]==\'?\':\n d[s[i]].append(i)\n #print(d)\n if len(s)==1:\n return "a"\n for j in d[\'?\']:\n if j > 0 and j < len(s)-1:\n for k in alp:\n if s[j-1] != k and s[j+1] != k:\n #print(s)\n s=s[:j] + k + s[j+1:]\n break\n elif j == 0 and len(s) != 1:\n for k in alp:\n if s[j+1] != k:\n #print(k)\n s=k+s[j+1:]\n break\n elif j == len(s)-1:\n for k in alp:\n if s[j-1] != k:\n #print(k)\n s=s[:j] + k\n break\n return s\n``` | 1 | 0 | ['String', 'Python3'] | 0 |
replace-all-s-to-avoid-consecutive-repeating-characters | Easy, Simple and Fastest way in C++ | easy-simple-and-fastest-way-in-c-by-ashu-3ncx | Complexity\n- Time complexity:\nO(n)\n\n- Space complexity:\nO(1)\n\n# Code\n\nclass Solution {\npublic:\n string modifyString(string s) {\n int len = | ashujain | NORMAL | 2023-06-25T04:03:21.986686+00:00 | 2023-06-25T04:03:21.986705+00:00 | 113 | false | # Complexity\n- Time complexity:\nO(n)\n\n- Space complexity:\nO(1)\n\n# Code\n```\nclass Solution {\npublic:\n string modifyString(string s) {\n int len = s.size();\n for(int i=0; i<s.size(); i++) {\n char c1, c2;\n if(s[i] == \'?\') {\n if(i-1>=0 && i+1<len) {\n c1=s[i-1];\n c2=s[i+1];\n }\n else if(i-1>=0) {\n c1=c2=s[i-1];\n }\n else {\n c1=c2=s[i+1];\n }\n \n for(char ch=\'a\'; ch<=\'z\'; ch++) {\n if(ch!=c1 && ch!=c2) {\n s[i] = ch;\n break;\n }\n }\n }\n }\n\n return s;\n }\n};\n``` | 1 | 0 | ['C++'] | 1 |
replace-all-s-to-avoid-consecutive-repeating-characters | Java O(n) | 100% | java-on-100-by-im_obid-3k2e | We do not need more than 3 letters to build a non-repeating character sequence.\n\n# Code\n\nclass Solution {\n public String modifyString(String s) {\n\n | im_obid | NORMAL | 2023-05-16T04:07:53.139485+00:00 | 2023-05-16T04:08:24.736906+00:00 | 64 | false | **We do not need more than 3 letters to build a non-repeating character sequence.**\n\n# Code\n```\nclass Solution {\n public String modifyString(String s) {\n\n if(s.length()==1) return s.equals("?")?"a":s;\n\n char[] ch = s.toCharArray();\n\n for(int i=1;i<ch.length-1;i++){\n if(ch[i]==\'?\'){\n if(ch[i-1]!=\'a\' && ch[i+1]!=\'a\') ch[i]=\'a\';\n else if(ch[i-1]!=\'b\' && ch[i+1]!=\'b\') ch[i]=\'b\';\n else if(ch[i-1]!=\'c\' && ch[i+1]!=\'c\') ch[i]=\'c\';\n }\n }\n\n if(ch[0]==\'?\')\n ch[0]=ch[1]==\'a\'?\'b\':\'a\';\n\n if(ch[ch.length-1]==\'?\')\n ch[ch.length-1]=ch[ch.length-2]==\'a\'?\'b\':\'a\';\n\n return new String(ch);\n }\n}\n``` | 1 | 0 | ['Java'] | 0 |
replace-all-s-to-avoid-consecutive-repeating-characters | c++ simple solution 100% beats in 0ms. | c-simple-solution-100-beats-in-0ms-by-co-5e9e | Intuition\n Describe your first thoughts on how to solve this problem. \n\n# Approach\n Describe your approach to solving the problem. \n1- iterate over the str | code_breaker_42 | NORMAL | 2023-05-05T14:04:37.901933+00:00 | 2023-05-05T14:04:37.901970+00:00 | 598 | false | # Intuition\n<!-- Describe your first thoughts on how to solve this problem. -->\n\n# Approach\n<!-- Describe your approach to solving the problem. -->\n1- iterate over the string.\n2-than make a function of default arguments to handle multiple cases in one function.\n3-function contain a for loop fron a to z.\n4-if(i th charecter doesnt match with both the s[i-1] and s[i+1])\nvalue the assing the ith charecter value in position of ?. \n\n# Complexity\n- Time complexity: O(N)\n<!-- Add your time complexity here, e.g. $$O(n)$$ -->\n\n- Space complexity:O(1)\n<!-- Add your space complexity here, e.g. $$O(n)$$ -->\n\n# Code\n```\nclass Solution {\npublic:\n void ans(string &str, int pos, char a=\' \', char b=\' \')\n {\n for(int i=\'a\'; i<=\'z\'; i++)\n {\n if((char)i !=a && (char)i!=b)\n {\n str[pos]=char(i);\n return ;\n }\n }\n }\n string modifyString(string s) {\n for(int i=0 ; i<s.length(); i++)\n {\n if(i==0)\n {\n if(s[i]==\'?\' )\n {\n ans(s,i,s[i+1]);\n }\n }\n else if(i==s.length()-1)\n {\n if(s[i]==\'?\' )\n {\n ans(s,i,s[i-1]);\n }\n }\n else\n {\n if(s[i]==\'?\' )\n {\n ans(s,i,s[i-1],s[i+1]);\n }\n }\n }\n return s;\n }\n};\n``` | 1 | 0 | ['C++'] | 1 |
replace-all-s-to-avoid-consecutive-repeating-characters | Replace All ?'s to Avoid Consecutive Repeating Characters Solution Java | replace-all-s-to-avoid-consecutive-repea-iv7t | class Solution {\n public String modifyString(String s) {\n char[] array = s.toCharArray();\n int length = array.length;\n if (array[0] | bhupendra786 | NORMAL | 2022-09-15T08:13:33.973164+00:00 | 2022-09-15T08:13:33.973204+00:00 | 310 | false | class Solution {\n public String modifyString(String s) {\n char[] array = s.toCharArray();\n int length = array.length;\n if (array[0] == \'?\') {\n if (length == 1)\n array[0] = \'a\';\n else {\n if (array[1] == \'a\')\n array[0] = \'b\';\n else\n array[0] = \'a\';\n }\n }\n if (array[length - 1] == \'?\') {\n if (array[length - 2] == \'a\')\n array[length - 1] = \'b\';\n else\n array[length - 1] = \'a\';\n }\n for (int i = 1; i < length - 1; i++) {\n char curr = array[i];\n if (curr == \'?\') {\n char prev = array[i - 1], next = array[i + 1];\n char letter = \'a\';\n while (letter == prev || letter == next)\n letter++;\n array[i] = letter;\n }\n }\n return new String(array);\n }\n} | 1 | 0 | ['String'] | 0 |
replace-all-s-to-avoid-consecutive-repeating-characters | Python, less condition approach | python-less-condition-approach-by-maleki-r72o | \nclass Solution:\n def modifyString(self, s: str) -> str:\n s = list("0"+s+"0")\n for i, l in enumerate(s[:-1]):\n if l == "?":\n | maleki_shoja | NORMAL | 2022-08-30T15:38:20.328515+00:00 | 2022-08-30T15:38:20.328556+00:00 | 396 | false | ```\nclass Solution:\n def modifyString(self, s: str) -> str:\n s = list("0"+s+"0")\n for i, l in enumerate(s[:-1]):\n if l == "?":\n for c in "abc":\n if c != s[i-1] and c != s[i+1]:\n s[i] = c\n break\n return "".join(s[1:-1])\n``` | 1 | 0 | ['Python'] | 0 |
replace-all-s-to-avoid-consecutive-repeating-characters | Java Solution | Runtime: 2ms | java-solution-runtime-2ms-by-harsh_kode-quiu | \nclass Solution {\n public String modifyString(String s) {\n char[] ch = s.toCharArray();\n for(int i=0;i<s.length();i++){\n if(ch[ | Harsh_Kode | NORMAL | 2022-08-26T20:18:29.304291+00:00 | 2022-08-26T20:18:29.304361+00:00 | 443 | false | ```\nclass Solution {\n public String modifyString(String s) {\n char[] ch = s.toCharArray();\n for(int i=0;i<s.length();i++){\n if(ch[i]==\'?\')\n for(char j=\'a\';j<=\'c\';j++){ // j = (a-z) is not required.\n if(i>0 && ch[i-1]==j) continue; // skip if previous character is one of a,b,c\n if(i<s.length()-1 && ch[i+1]==j) continue; //skip if next character is one of a,b,c\n ch[i] = j; //set with the current character from a,b,c\n break;\n }\n }\n return String.valueOf(ch);\n }\n}\n``` | 1 | 0 | ['String', 'Java'] | 1 |
replace-all-s-to-avoid-consecutive-repeating-characters | [C++] || Very easy to understand | c-very-easy-to-understand-by-riteshkhan-nfwh | \nclass Solution {\n char update_char(char x, char y){\n char c = \'a\';\n while(c==x || c==y){\n c++;\n }\n return c; | RiteshKhan | NORMAL | 2022-07-25T08:43:33.594589+00:00 | 2022-07-25T08:53:38.819054+00:00 | 215 | false | ```\nclass Solution {\n char update_char(char x, char y){\n char c = \'a\';\n while(c==x || c==y){\n c++;\n }\n return c;\n }\npublic:\n string modifyString(string s) {\n int l = s.length();\n char c;\n if(s[0] == \'?\'){\n c = update_char(s[0],s[1]);\n s[0] = c;\n }\n if(s[l-1] == \'?\'){\n c = update_char(s[l-2],s[l-1]);\n s[l-1] = c;\n }\n for(int i=1; i<l-1; ++i){\n if(s[i] == \'?\'){\n c = update_char(s[i-1],s[i+1]);\n s[i] = c;\n }\n }\n return s;\n }\n};\n``` | 1 | 0 | ['C'] | 0 |
replace-all-s-to-avoid-consecutive-repeating-characters | 🐌 C# - straightforward solution | c-straightforward-solution-by-user7166uf-3gr3 | \npublic class Solution\n{\n public string ModifyString(string s)\n {\n char[] characters = s.ToCharArray();\n\n for (int index = 0; index < | user7166Uf | NORMAL | 2022-06-12T12:39:50.011930+00:00 | 2022-06-12T12:39:50.011970+00:00 | 62 | false | ```\npublic class Solution\n{\n public string ModifyString(string s)\n {\n char[] characters = s.ToCharArray();\n\n for (int index = 0; index < characters.Length; index++)\n {\n if (characters[index] is not \'?\') continue;\n\n char? previousCharacter = index > 0 ? characters[index - 1] : null;\n char? nextCharacter = index + 1 < characters.Length ? characters[index + 1] : null;\n\n if (previousCharacter is not \'a\' && nextCharacter is not \'a\')\n {\n characters[index] = \'a\';\n }\n else if (previousCharacter is not \'b\' && nextCharacter is not \'b\')\n {\n characters[index] = \'b\';\n }\n else\n {\n characters[index] = \'c\';\n }\n }\n\n return new string(characters);\n }\n}\n```\n\nWith switch expression\n```\npublic class Solution\n{\n public string ModifyString(string s)\n {\n char[] characters = s.ToCharArray();\n\n for (int index = 0; index < characters.Length; index++)\n {\n if (characters[index] is not \'?\') continue;\n\n char? previousCharacter = index > 0 ? characters[index - 1] : null;\n char? nextCharacter = index + 1 < characters.Length ? characters[index + 1] : null;\n\n characters[index] = (previousCharacter, nextCharacter) switch\n {\n (not \'a\', not \'a\') => \'a\',\n (not \'b\', not \'b\') => \'b\',\n _ => \'c\'\n };\n }\n\n return new string(characters);\n }\n}\n``` | 1 | 0 | ['C#'] | 0 |
replace-all-s-to-avoid-consecutive-repeating-characters | Tried a lot but only 749/776 passed :( | tried-a-lot-but-only-749776-passed-by-ch-jcpg | \nclass Solution {\npublic:\n string modifyString(string s) {\n string str="";\n for(int i=0;i<s.size();i++){\n if(s[i]==\'?\')\n | charu794 | NORMAL | 2022-05-07T11:01:59.222831+00:00 | 2022-05-07T11:01:59.222868+00:00 | 47 | false | ```\nclass Solution {\npublic:\n string modifyString(string s) {\n string str="";\n for(int i=0;i<s.size();i++){\n if(s[i]==\'?\')\n {\n int x,y;\n if(i==0){\n x= s[i+1];\n if(x<97 || x>122)\n {\n char c= 97;\n str+=c;\n }\n else if(x!=122){\n char c= x+1;\n str+=c;\n }\n else{\n char c= x-1;\n str+=c;\n }\n \n \n }\n else if(i== s.size()-1){\n y=s[i-1];\n if(y<97 || y>122)\n {\n char c= 98;\n str+=c;\n }\n else if(y!=122){\n char c= y+1;\n str+=c;\n }\n else{\n char c= y-1;\n str+=c;\n }\n }\n else{\n x=s[i+1];\n y=s[i-1]; \n if(x==y)\n {\n if(x<97 || x>122)\n {\n char c= 99;\n str+=c;\n }\n else if(x!=122){\n char c= x+1;\n str+=c;\n }\n else{\n char c= x-1;\n str+=c;\n }\n }\n \n else\n {\n if(x<97 || x>122)\n {\n char c= 100;\n str+=c;\n }\n else if(x!=122){\n x++;\n if(x==y ){\n x++;\n }\n char c= x;\n str+=c;\n }\n else{\n x--;\n if(x==y){\n x--;\n }\n char c= x;\n str+=c;\n }\n }\n }\n \n \n }\n else{\n str += s[i];\n \n }\n }\n return str;\n }\n};\n``` | 1 | 0 | [] | 1 |
replace-all-s-to-avoid-consecutive-repeating-characters | C++ solution | c-solution-by-charu794-ia25 | \nclass Solution {\npublic:\n string modifyString(string s) {\n \n for(int i=0;i<s.size();i++)\n {\n if(s[i]==\'?\')\n | charu794 | NORMAL | 2022-05-07T11:00:31.805600+00:00 | 2022-05-07T11:00:31.805626+00:00 | 87 | false | ```\nclass Solution {\npublic:\n string modifyString(string s) {\n \n for(int i=0;i<s.size();i++)\n {\n if(s[i]==\'?\')\n {\n for(char ch=\'a\';ch<=\'z\';ch++)\n {\n if(i==0 && s[i+1]!=ch)\n {\n s[i]=ch;\n break;\n }\n else if(i==s.size()-1 && s[i-1]!=ch)\n {\n s[i]=ch;\n break;\n }\n else if(s[i+1]!=ch && s[i-1]!=ch){\n s[i]=ch;\n break;\n }\n }\n }\n \n }\n return s;\n \n }\n};\n``` | 1 | 0 | [] | 1 |
replace-all-s-to-avoid-consecutive-repeating-characters | java || easy to understand | java-easy-to-understand-by-pratham_18-zos6 | \nclass Solution {\n public String modifyString(String s) {\n char arr[]=new char[s.length()];\n for(int i=0;i<s.length();i++){\n arr | Pratham_18 | NORMAL | 2022-04-19T07:38:21.417311+00:00 | 2022-04-19T07:38:21.417354+00:00 | 171 | false | ```\nclass Solution {\n public String modifyString(String s) {\n char arr[]=new char[s.length()];\n for(int i=0;i<s.length();i++){\n arr[i]=s.charAt(i);\n }\n for(int i=0;i<arr.length;i++){\n if(arr[i]==\'?\'){\n for(char j=\'a\';j<=\'c\';j++){\n if((i==0 || arr[i-1]!=j)&&(i==s.length()-1 || arr[i+1]!=j)){\n arr[i]=j;\n break;\n }\n }\n }\n }\n return String.valueOf(arr);\n }\n}``` | 1 | 0 | ['Java'] | 0 |
replace-all-s-to-avoid-consecutive-repeating-characters | python clear and fast solution | python-clear-and-fast-solution-by-tirtha-xcdx | \n````\nclass Solution:\n def modifyString(self, s: str) -> str:\n a=list(s)+["."]\n for i in range(len(a)-1):\n if a[i]=="?":\n | tirtha_20 | NORMAL | 2022-03-11T12:10:00.096226+00:00 | 2022-03-11T12:10:00.096273+00:00 | 77 | false | \n````\nclass Solution:\n def modifyString(self, s: str) -> str:\n a=list(s)+["."]\n for i in range(len(a)-1):\n if a[i]=="?":\n if a[i+1]!="a" and a[i-1]!="a":\n a[i]="a"\n elif a[i+1]!="b" and a[i-1]!="b":\n a[i]="b"\n elif a[i+1]!="c" and a[i-1]!="c":\n a[i]="c"\n i+=1\n return "".join(a)[:len(s)]\n | 1 | 0 | [] | 1 |
replace-all-s-to-avoid-consecutive-repeating-characters | 2 C++ Solutions || Different Methods each time for replacement of '?' || Most Optimized Approach | 2-c-solutions-different-methods-each-tim-fvbt | Approach 1\nIn this approach when we encounter a \'?\' we try from the first character \'a\', if the character satisfies the condition of the question i.e. not | dee_stroyer | NORMAL | 2021-12-18T12:50:22.873634+00:00 | 2021-12-18T12:50:22.873675+00:00 | 172 | false | **Approach 1**\nIn this approach when we encounter a \'?\' we try from the first character \'a\', if the character satisfies the condition of the question i.e. not similar to the consecutive one, then we replace the ? with the character, otherwise we increment the current char try doing the same as we did with a.\n```\nclass Solution {\npublic:\n string modifyString(string s) {\n \n for(int i = 0; i<s.size(); i++){\n if(s[i] == \'?\'){\n char x = \'a\';\n if(i == 0){\n while(x == s[i+1]){\n x++;\n }\n }\n \n else if(i == s.size()-1){\n while(x == s[i-1]){\n x++;\n }\n }\n \n else{\n while(x==s[i-1] or x == s[i+1]){\n x++;\n }\n }\n \n s[i] = x;\n }\n }\n \n return s;\n\t }\n};\n```\n\n**Approach 2: We need only 3 variables for the replacement of ?**\nLet us assume we have taken three variables as a, b and c.\n\nWhenever there is a ? there are three cases:\n1. Both the left and right of ? consist of a then we can insert b\n2. Both the left and right consist of alphabet other than a then we can insert a\n3. Left side contain a and right side contain b, then we can insert third variable c.\n```\nclass Solution {\npublic:\n string modifyString(string s) {\n \n int n = s.size();\n \n for(int i = 0; i<n; i++){\n \n if(s[i] == \'?\'){\n //for the ? to be present at the first index there is only one case which is that next element must not be the same\n if(i == 0){\n //if the next element is not a then insert a to current position \n if(s[i+1] != \'a\'){\n s[i] = \'a\'; \n }\n \n //if the next element is a then insert b to current position \n else{\n s[i] = \'b\';\n }\n }\n \n //for the ? to be present at the last index there is only one case which is that previous element must not be the same\n else if(i == n-1){\n //if the previous element is not a then put a to current index\n if(s[i-1] != \'a\'){\n s[i] = \'a\';\n }\n //if the previous element is a then put b to current index\n else{\n s[i] = \'b\';\n }\n }\n \n else{\n if(s[i-1] != \'a\' and s[i+1]!=\'a\'){\n s[i] = \'a\';\n }\n \n else if(s[i-1] != \'b\' and s[i+1]!=\'b\'){\n s[i] = \'b\';\n }\n \n else{\n s[i] = \'c\';\n }\n \n }\n }\n }\n \n return s;\n }\n};\n``` | 1 | 0 | ['C', 'C++'] | 0 |
replace-all-s-to-avoid-consecutive-repeating-characters | C++ Simple and Short Straight-Forward Solution | c-simple-and-short-straight-forward-solu-0ier | Idea:\nWe only need three characters for replacement, because there can\'t be more than one character at each side of the ?.\nSo we try each letter and replace. | yehudisk | NORMAL | 2021-11-03T22:30:20.622478+00:00 | 2021-11-03T22:30:20.622509+00:00 | 182 | false | **Idea:**\nWe only need three characters for replacement, because there can\'t be more than one character at each side of the `?`.\nSo we try each letter and replace.\n\n**Time Complexity:** O(n)\n**Space Complexity:** None\n```\nclass Solution {\npublic:\n string modifyString(string s) {\n for (int i = 0; i < s.size(); i++) {\n if (s[i] != \'?\') continue;\n if ((i == 0 || s[i-1] != \'a\') && s[i+1] != \'a\') s[i] = \'a\';\n else if ((i == 0 || s[i-1] != \'b\') && s[i+1] != \'b\') s[i] = \'b\';\n else s[i] = \'c\';\n }\n return s;\n }\n};\n```\n**Like it? please upvote!** | 1 | 0 | ['C'] | 1 |
replace-all-s-to-avoid-consecutive-repeating-characters | Java- Easy Solution | java-easy-solution-by-srajen488-k4s0 | \nclass Solution {\n public String modifyString(String s) {\n char[] str = s.toCharArray();\n for(int i =0; i < str.length; i ++)\n {\n | srajen488 | NORMAL | 2021-09-07T02:32:40.935085+00:00 | 2021-09-07T02:32:40.935122+00:00 | 170 | false | ```\nclass Solution {\n public String modifyString(String s) {\n char[] str = s.toCharArray();\n for(int i =0; i < str.length; i ++)\n {\n if(str[i] == \'?\')\n {\n str[i] = replaceChar(str, i);\n }\n }\n return String.valueOf(str);\n }\n \n private char replaceChar(char[] s, int i)\n {\n for(char ch= \'a\'; ch <= \'z\'; ch++)\n {\n if(i > 0 && ch== s[i -1])\n {\n continue;\n }\n if(i < s.length -1 && ch== s[i + 1])\n {\n continue;\n }\n return ch;\n }\n return 0;\n }\n}\n``` | 1 | 0 | [] | 0 |
replace-all-s-to-avoid-consecutive-repeating-characters | Python Easy Solution - 90+% Faster | python-easy-solution-90-faster-by-gsbc95-yc96 | \nclass Solution:\n def modifyString(self, s: str) -> str:\n prev = \'\'\n for index,value in enumerate(s):\n if value == \'?\':\n | gsbc95 | NORMAL | 2021-08-19T20:36:43.063699+00:00 | 2021-08-19T20:39:15.357553+00:00 | 109 | false | ```\nclass Solution:\n def modifyString(self, s: str) -> str:\n prev = \'\'\n for index,value in enumerate(s):\n if value == \'?\':\n if index+1 < len(s):\n for i in range(97,123):\n if chr(i) !=s[index+1] and chr(i) != prev:\n s = s[:index]+chr(i)+s[index+1:]\n break\n else:\n for i in range(97,123):\n if chr(i) != prev:\n s = s[:index]+chr(i)+s[index+1:]\n break\n prev = s[index]\n return s\n``` | 1 | 0 | [] | 0 |
replace-all-s-to-avoid-consecutive-repeating-characters | Python3, fast and simple | python3-fast-and-simple-by-mihailp-uh9r | \nclass Solution:\n def modifyString(self, s: str) -> str:\n ans = []\n\n for i, ch in enumerate(s):\n if ch != "?":\n | MihailP | NORMAL | 2021-07-29T16:12:15.705703+00:00 | 2021-07-29T16:12:15.705754+00:00 | 303 | false | ```\nclass Solution:\n def modifyString(self, s: str) -> str:\n ans = []\n\n for i, ch in enumerate(s):\n if ch != "?":\n ans.append(ch)\n else:\n prev_ch = ans[-1] if i > 0 else ""\n next_ch = s[i + 1] if i < len(s) - 1 else ""\n if "a" != prev_ch and "a" != next_ch:\n ans.append("a")\n elif "b" != prev_ch and "b" != next_ch:\n ans.append("b")\n else:\n ans.append("c")\n\n return "".join(ans)\n``` | 1 | 1 | ['Python', 'Python3'] | 0 |
replace-all-s-to-avoid-consecutive-repeating-characters | simple C++ | simple-c-by-rap_tors-j1hx | \nstring modifyString(string s) {\n for(int i = 0; i < s.size(); i++) {\n if(s[i] == \'?\') {\n for(char ch = \'a\'; ch <= \'c\ | rap_tors | NORMAL | 2021-07-04T17:22:28.492226+00:00 | 2021-07-04T17:22:28.492268+00:00 | 169 | false | ```\nstring modifyString(string s) {\n for(int i = 0; i < s.size(); i++) {\n if(s[i] == \'?\') {\n for(char ch = \'a\'; ch <= \'c\'; ch++) {\n if(i > 0 and s[i-1] == ch) continue;\n if(i < s.size() - 1 and s[i + 1] == ch) continue;\n s[i] = ch;\n break;\n }\n }\n }\n return s;\n }\n``` | 1 | 0 | ['C', 'C++'] | 0 |
replace-all-s-to-avoid-consecutive-repeating-characters | Easy to understand JavaScript solution (reduce) | easy-to-understand-javascript-solution-r-ayo1 | \tvar modifyString = function(s) {\n\t\tconst getChar = (num, left, right) => {\n\t\t\tconst char = String.fromCharCode(num);\n\t\t\treturn char === left || cha | tzuyi0817 | NORMAL | 2021-06-26T07:10:00.799116+00:00 | 2021-06-26T07:10:00.799148+00:00 | 130 | false | \tvar modifyString = function(s) {\n\t\tconst getChar = (num, left, right) => {\n\t\t\tconst char = String.fromCharCode(num);\n\t\t\treturn char === left || char === right ? getChar(++num, left, right) : char;\n\t\t};\n\n\t\treturn [...s].reduce((acc, curr, index) => {\n\t\t\tif (curr === \'?\') {\n\t\t\t\tconst checkLeft = acc[index - 1];\n\t\t\t\tconst checkRight = acc[index + 1];\n\t\t\t\tconst char = getChar(97, checkLeft, checkRight);\n\t\t\t\tacc = acc.substr(0, index) + char + acc.substr(index + 1);\n\t\t\t}\n\t\t\treturn acc;\n\t\t}, s);\n\t}; | 1 | 0 | ['JavaScript'] | 0 |
replace-all-s-to-avoid-consecutive-repeating-characters | [Java] Straightforward Clean Code | java-straightforward-clean-code-by-mllej-w0tm | \nclass Solution {\n public String modifyString(String s) {\n StringBuilder str = new StringBuilder("a"+s+"z");\n for (int i = 0; i < s.length( | mllejuly | NORMAL | 2021-06-13T08:50:57.540351+00:00 | 2021-09-25T09:26:35.579753+00:00 | 177 | false | ```\nclass Solution {\n public String modifyString(String s) {\n StringBuilder str = new StringBuilder("a"+s+"z");\n for (int i = 0; i < s.length(); i++) { \n if (s.charAt(i) == \'?\') {\n if (Math.min(str.charAt(i),str.charAt(i+2))>\'a\') {\n str.setCharAt(i+1, \'a\');\n } else if (Math.max(str.charAt(i),str.charAt(i+2))<\'z\') {\n str.setCharAt(i+1, \'z\');\n } else {\n str.setCharAt(i+1, \'b\');\n }\n }\n }\n return str.substring(1,s.length()+1).toString();\n }\n}\n``` | 1 | 0 | [] | 0 |
replace-all-s-to-avoid-consecutive-repeating-characters | java easy 1 ms, faster than 100.00% | java-easy-1-ms-faster-than-10000-by-tany-88zq | public String modifyString(String s) {\n StringBuilder sb = new StringBuilder();\n for(int i = 0; i < s.length(); i++){\n if(s.charAt(i | tanyasinghal92 | NORMAL | 2021-06-13T07:29:20.307644+00:00 | 2021-06-13T07:29:20.307681+00:00 | 198 | false | public String modifyString(String s) {\n StringBuilder sb = new StringBuilder();\n for(int i = 0; i < s.length(); i++){\n if(s.charAt(i) == \'?\'){\n char ch = \'a\';\n while((i != 0 && sb.charAt(i-1) == ch) || (i != s.length() - 1 && s.charAt(i+1) == ch))ch++;\n sb.append(ch); \n }\n else\n sb.append(s.charAt(i)); \n } \n return sb.toString();\n } | 1 | 2 | [] | 0 |
replace-all-s-to-avoid-consecutive-repeating-characters | Python simple single pass solution, Time: O(n) | python-simple-single-pass-solution-time-otc4o | \nclass Solution:\n def modifyString(self, s: str) -> str:\n output = ""\n \n for index in range(len(s)):\n if s[index] != \' | kaushal087 | NORMAL | 2021-06-06T13:58:38.673070+00:00 | 2021-06-06T13:58:38.673115+00:00 | 121 | false | ```\nclass Solution:\n def modifyString(self, s: str) -> str:\n output = ""\n \n for index in range(len(s)):\n if s[index] != \'?\':\n output += s[index] \n continue\n\n not_allowed = set()\n \n if index - 1 >= 0:\n not_allowed.add(output[-1])\n \n if index + 1 < len(s) and s[index + 1] != \'?\':\n not_allowed.add(s[index+1])\n \n if \'a\' not in not_allowed:\n output += \'a\'\n elif \'b\' not in not_allowed:\n output += \'b\'\n else:\n output += \'c\'\n return output\n``` | 1 | 0 | [] | 0 |
replace-all-s-to-avoid-consecutive-repeating-characters | Python O(n) time | O(n) Space | python-on-time-on-space-by-wintersoldier-inxt | \n# Time = O(n)\n# Space = O(n)\nclass Solution:\n def modifyString(self, s: str) -> str:\n d = {}\n s = list(s)\n\n for i in range(len( | wintersoldier | NORMAL | 2021-06-05T21:25:42.118683+00:00 | 2021-06-05T21:25:42.118713+00:00 | 92 | false | ```\n# Time = O(n)\n# Space = O(n)\nclass Solution:\n def modifyString(self, s: str) -> str:\n d = {}\n s = list(s)\n\n for i in range(len(s)):\n if s[i] != \'?\':\n d[s[i]] = i \n continue \n \n for j in range(1, 26+1):\n char = chr(96 + j)\n if (i > 0 and s[i-1] == char) or (i < len(s) - 1 and s[i+1] == char):\n continue \n \n s[i] = char \n d[char] = i \n break \n \n return \'\'.join(s)\n\t\t``` | 1 | 0 | [] | 0 |
replace-all-s-to-avoid-consecutive-repeating-characters | c# solution | c-solution-by-obpb4ae4twxy-gbl0 | \npublic class Solution\n{\n public string ModifyString(string s) \n {\n var c = 0;\n \n var result = new StringBuilder();\n f | obpb4ae4twxy | NORMAL | 2021-06-05T16:32:45.359448+00:00 | 2021-06-05T16:34:15.173057+00:00 | 57 | false | ```\npublic class Solution\n{\n public string ModifyString(string s) \n {\n var c = 0;\n \n var result = new StringBuilder();\n for (int i = 0; i < s.Length; i++)\n {\n if (s[i] != \'?\')\n {\n result.Append(s[i]);\n continue;\n }\n \n while (true)\n {\n char cc = (char)(\'a\' + c);\n if (CanUse(s, i, cc))\n {\n result.Append(cc);\n c = (c + 1) % 26;\n break;\n }\n c = (c + 1) % 26;\n }\n }\n \n return result.ToString();\n }\n \n bool CanUse(string s, int i, char c)\n {\n if (i >= 1)\n {\n if (s[i - 1] == c)\n return false;\n }\n \n if (i < s.Length - 1)\n {\n if (s[i + 1] == c)\n return false;\n }\n \n return true;\n }\n}\n``` | 1 | 0 | [] | 0 |
replace-all-s-to-avoid-consecutive-repeating-characters | simple circular C++ 100% faster | simple-circular-c-100-faster-by-dzzhdzzh-4hzw | \nclass Solution {\npublic:\n string modifyString(string s) {\n int j = 0;\n for(int i = 0; i < s.size(); ++i){\n char c = s[i];\n | dzzhdzzh | NORMAL | 2021-06-04T07:04:52.273862+00:00 | 2021-06-04T07:04:52.273909+00:00 | 59 | false | ```\nclass Solution {\npublic:\n string modifyString(string s) {\n int j = 0;\n for(int i = 0; i < s.size(); ++i){\n char c = s[i];\n if(c == \'?\'){\n char l = i > 0 ? s[i-1] : \'*\';\n char r = i < s.size() - 1 ? s[i + 1] : \'*\';\n char tmp = \'a\' + j;\n while(tmp == l || tmp == r){\n j = (j + 1) % 26;\n tmp = \'a\' + j;\n }\n s[i] = tmp;\n }\n }\n return s;\n }\n};\n``` | 1 | 0 | [] | 0 |
replace-all-s-to-avoid-consecutive-repeating-characters | 100 % :: Python | Maintain a pool of characters | 100-python-maintain-a-pool-of-characters-p7hv | \nclass Solution:\n def modifyString(self, s: str) -> str:\n pool = set([chr(i) for i in range(97, 123)])\n s = list(s)\n for i in range | tuhinnn_py | NORMAL | 2021-05-20T16:21:33.702338+00:00 | 2021-05-20T16:21:33.702391+00:00 | 78 | false | ```\nclass Solution:\n def modifyString(self, s: str) -> str:\n pool = set([chr(i) for i in range(97, 123)])\n s = list(s)\n for i in range(len(s)):\n if s[i] == \'?\':\n pool -= set([s[i - 1] if i >= 1 else None, s[i + 1] if i <= len(s) - 2 else None])\n s[i] = pool.pop()\n if i >= 2:\n pool.add(s[i - 2])\n return \'\'.join(s)\n``` | 1 | 0 | [] | 0 |
replace-all-s-to-avoid-consecutive-repeating-characters | [C++] 0ms Concise Solution | 5 lines | Beats 100% Time | c-0ms-concise-solution-5-lines-beats-100-uci6 | \n string modifyString(string s) {\n for(int i = 0; i < s.length(); i++){\n if(s[i] == \'?\'){\n if((i == 0 || s[i - 1] != \ | sherlasd | NORMAL | 2021-05-07T16:22:31.384082+00:00 | 2021-05-07T16:22:31.384114+00:00 | 208 | false | ```\n string modifyString(string s) {\n for(int i = 0; i < s.length(); i++){\n if(s[i] == \'?\'){\n if((i == 0 || s[i - 1] != \'a\') && (i == s.length() - 1 || s[i + 1] != \'a\')) s[i] = \'a\'; \n else if((i == 0 || s[i - 1] != \'b\') && (i == s.length() - 1 || s[i + 1] != \'b\')) s[i] = \'b\';\n else if((i == 0 || s[i - 1] != \'c\') && (i == s.length() - 1 || s[i + 1] != \'c\')) s[i] = \'c\';\n }\n }\n return s;\n }\n``` | 1 | 0 | ['C', 'C++'] | 1 |
replace-all-s-to-avoid-consecutive-repeating-characters | 100% faster || C++ Solution || Clean Code | 100-faster-c-solution-clean-code-by-kann-hao7 | \nclass Solution {\npublic:\n string modifyString(string s) {\n int n = s.size();\n for(int i = 0; i < n; i++)\n if(s[i] == \'?\')\n | kannu_priya | NORMAL | 2021-04-24T09:58:08.409446+00:00 | 2021-04-24T09:58:08.409477+00:00 | 136 | false | ```\nclass Solution {\npublic:\n string modifyString(string s) {\n int n = s.size();\n for(int i = 0; i < n; i++)\n if(s[i] == \'?\')\n for(int c = \'a\'; c <= \'z\'; c++)\n {\n if(i-1 >= 0 && c == s[i-1]) continue;\n if(i+1 < n && c == s[i+1]) continue;\n s[i] = c;\n break;\n } \n return s;\n }\n};\n``` | 1 | 0 | ['C', 'C++'] | 0 |
replace-all-s-to-avoid-consecutive-repeating-characters | Very Simple C++ Solution | 100% Faster 0ms | very-simple-c-solution-100-faster-0ms-by-g9i6 | \nclass Solution {\npublic:\n \n char fun(char cp, char cn)\n {\n for(int i=0; i<26; i++)\n {\n char c = i +\'a\';\n | imanshul | NORMAL | 2021-04-21T09:11:22.842666+00:00 | 2021-04-21T09:11:22.842700+00:00 | 56 | false | ```\nclass Solution {\npublic:\n \n char fun(char cp, char cn)\n {\n for(int i=0; i<26; i++)\n {\n char c = i +\'a\';\n if(cp!=c && cn!=c)\n {\n return c;\n }\n }\n return 0;\n }\n string modifyString(string s) {\n ios:: sync_with_stdio(0); cin.tie(0); cout.tie(0);\n if(s[0] ==\'?\')\n {\n for(int i=0; i<26; i++)\n {\n char c = i+\'a\';\n if(s[1]!=c)\n {\n s[0]=c;\n break;\n }\n }\n }\n if(s[s.size()-1]==\'?\')\n {\n for(int i=0; i<26; i++)\n {\n char c = i+\'a\';\n if(s[s.size()-2]!=c)\n {\n s[s.size()-1]=c;\n break;\n }\n } \n }\n \n for(int i=1; i<s.size()-1; i++)\n {\n if(s[i]==\'?\')\n {\n char cp = s[i-1];\n char cn = s[i+1];\n char ans = fun(cp, cn);\n s[i] = ans;\n }\n }\n return s;\n }\n};\n``` | 1 | 0 | [] | 0 |
replace-all-s-to-avoid-consecutive-repeating-characters | simple C++ solution | simple-c-solution-by-tuhin12-qe3o | \nclass Solution {\npublic:\n string modifyString(string s) \n {\n int len=s.length();\n char ch;\n \n for(int i=0;i<len;i++)\ | Tuhin12 | NORMAL | 2021-04-16T15:26:44.668879+00:00 | 2021-04-16T15:26:44.668917+00:00 | 66 | false | ```\nclass Solution {\npublic:\n string modifyString(string s) \n {\n int len=s.length();\n char ch;\n \n for(int i=0;i<len;i++)\n {\n ch=\'a\';\n if(s[i]==\'?\')\n {\n if(i==0)\n {\n while(ch==s[i+1])\n ch++; \n }\n else if(i==len-1)\n {\n while(ch==s[i-1])\n ch++;\n }\n else\n {\n while(ch==s[i+1]||ch==s[i-1])\n ch++;\n }\n s[i]=ch;\n \n }\n }\n return s;\n }\n};\n``` | 1 | 1 | [] | 0 |
replace-all-s-to-avoid-consecutive-repeating-characters | Javascript using charCode and .slice | javascript-using-charcode-and-slice-by-k-um9a | \nvar modifyString = function(s) {\n for(let i = 0; i < s.length; i++){\n if(s[i] === \'?\'){\n let charCode = 97\n const charCo | kziechmann | NORMAL | 2021-04-14T05:12:03.037124+00:00 | 2021-04-14T05:12:03.037154+00:00 | 47 | false | ```\nvar modifyString = function(s) {\n for(let i = 0; i < s.length; i++){\n if(s[i] === \'?\'){\n let charCode = 97\n const charCodeLeft = s.charCodeAt(i-1)\n const charCodeRight = s.charCodeAt(i+1)\n while(charCodeLeft === charCode || charCodeRight === charCode){\n charCode++\n }\n s = s.slice(0,i) + String.fromCharCode(charCode) + s.slice(i+1)\n } \n }\n return s\n};\n``` | 1 | 0 | [] | 0 |
replace-all-s-to-avoid-consecutive-repeating-characters | Elegant Python Solution | elegant-python-solution-by-true-detectiv-buur | \nclass Solution:\n def modifyString(self, s: str) -> str:\n alphabet = set(string.ascii_lowercase)\n \n res = \'\'\n for i in ra | true-detective | NORMAL | 2021-04-06T14:27:16.042049+00:00 | 2021-04-06T14:28:32.998979+00:00 | 133 | false | ```\nclass Solution:\n def modifyString(self, s: str) -> str:\n alphabet = set(string.ascii_lowercase)\n \n res = \'\'\n for i in range(len(s)):\n if s[i] != \'?\':\n res += s[i]\n continue\n \n last_char = res[-1] if i > 0 else \'\'\n next_char = s[i+1] if i < len(s)-1 else \'\'\n \n to_choose_from = alphabet.difference([last_char, next_char])\n res += to_choose_from.pop()\n \n return res\n``` | 1 | 1 | [] | 0 |
replace-all-s-to-avoid-consecutive-repeating-characters | Java solution faster than 100% | java-solution-faster-than-100-by-bhavyas-wfo4 | We dont want consecutive characters and multiple solutions are possible, so all we need to check is whether the character we replace is not the same as its left | bhavyaspatel | NORMAL | 2021-03-29T19:24:50.359444+00:00 | 2021-03-29T19:24:50.359489+00:00 | 179 | false | We dont want consecutive characters and multiple solutions are possible, so all we need to check is whether the character we replace is not the same as its left/right neighbor.\nWe can simply solve this problem by checking with 3 alphabets : \'a\', \'b\' and \'c\'\nIf left is null, check 2nd character\nelse if right is null, check second last character\nelse just check left and right\n\n```class Solution {\n public String modifyString(String s) {\n \n if(s.length() == 1)\n return (s.equals("?")) ? "a" : s;\n char[] strArr = s.toCharArray();\n \n for(int i = 0; i < strArr.length; i++) {\n char c = strArr[i];\n if(c == \'?\') {\n if(i == 0 ){\n strArr[i] = (strArr[i+1] == \'a\') ? \'b\' : \'a\';\n } else if(i == strArr.length - 1){\n strArr[i] = (strArr[i-1] == \'a\') ? \'b\' : \'a\';\n } else {\n for(char temp = \'a\' ; temp <= \'c\';) {\n if(strArr[i-1] == temp || strArr[i+1] == temp) {\n temp++;\n } else {\n strArr[i] = temp;\n break;\n }\n }\n } \n }\n }\n return String.valueOf(strArr);\n }\n \n \n}``` | 1 | 0 | [] | 0 |
replace-all-s-to-avoid-consecutive-repeating-characters | [C++] Concise Solution Using string::find | c-concise-solution-using-stringfind-by-k-rgvq | Search for every ? in the string, assigning it either a, b, or c depending on the adjacent values.\n\nstring modifyString(string s) {\n\tfor (int i = s.find(\'? | KasraCode | NORMAL | 2021-03-23T18:18:14.373828+00:00 | 2021-03-23T18:18:14.373857+00:00 | 38 | false | Search for every `?` in the string, assigning it either `a`, `b`, or `c` depending on the adjacent values.\n```\nstring modifyString(string s) {\n\tfor (int i = s.find(\'?\'); i != string::npos; i = s.find(\'?\', i + 1)) {\n\t\tchar prev = i > 0 ? s[i - 1] : 0, next = i < s.size() - 1 ? s[i + 1] : 0;\n\t\ts[i] = (prev == \'a\' || next == \'a\') ? ((prev == \'b\' || next == \'b\') ? \'c\' : \'b\') : \'a\';\n\t}\n\treturn s;\n}\n``` | 1 | 0 | [] | 0 |
replace-all-s-to-avoid-consecutive-repeating-characters | Python O(n) | python-on-by-shacoz-l3z0 | \nclass Solution:\n def modifyString(self, s: str) -> str:\n a_to_z = "abcdefghijklmnopqrstuvwxyz"\n ans = ""\n for i in range (0,len(s) | ShacoZ | NORMAL | 2021-03-03T06:40:47.457914+00:00 | 2021-03-03T06:40:47.457950+00:00 | 109 | false | ```\nclass Solution:\n def modifyString(self, s: str) -> str:\n a_to_z = "abcdefghijklmnopqrstuvwxyz"\n ans = ""\n for i in range (0,len(s)):\n if s[i] =="?":\n left, right ="?","?"\n if i-1 >=0 :\n left = ans[i-1]\n if i+1< len(s):\n right = s[i+1]\n for newguess in a_to_z:\n if newguess != left and newguess !=right:\n ans += newguess\n break\n else:\n ans += s[i] \n return ans\n\t\t\n``` | 1 | 0 | [] | 1 |
replace-all-s-to-avoid-consecutive-repeating-characters | Python O(n) solution | python-on-solution-by-stuti2009-k38u | We can simply start with converting string in to list of characters as string is immutable in python. Then iterate over each element in list if it is "?" check | stuti2009 | NORMAL | 2021-03-03T05:02:04.846391+00:00 | 2021-03-03T05:04:37.450648+00:00 | 154 | false | We can simply start with converting string in to list of characters as string is immutable in python. Then iterate over each element in list if it is "?" check previous and next value and replace it with any character apart from neighbours.\n \n `class Solution:\n def modifyString(self, s: str) -> str:\n s = list(s)\n n = len(s)\n i = 0\n while i<n:\n if s[i]=="?":\n choice1 = choice2 = 0\n if i>0:\n choice1 = ord(s[i-1])\n if i<n-1:\n choice2 = ord(s[i+1])\n val = 97\n while val == choice1 or val == choice2:\n val+=1\n s[i] = chr(val)\n i +=1\n return "".join(s)\n \n \n `\n \n \n | 1 | 0 | [] | 0 |
replace-all-s-to-avoid-consecutive-repeating-characters | Python3 solution, beat 97% | python3-solution-beat-97-by-hotrabbit05-f0m8 | Python\nclass Solution:\n def modifyString(self, s: str) -> str:\n chars = \'abcdefghijklmnopqrstuvwxyz\'\n s_list = list(s)\n for i in | hotrabbit05 | NORMAL | 2021-02-15T03:30:37.850335+00:00 | 2021-02-15T03:30:37.850437+00:00 | 119 | false | ```Python\nclass Solution:\n def modifyString(self, s: str) -> str:\n chars = \'abcdefghijklmnopqrstuvwxyz\'\n s_list = list(s)\n for i in range(len(s)):\n if s_list[i] != \'?\':\n continue\n pre, post = \'\', \'\'\n if i - 1 >= 0:\n pre = s_list[i - 1]\n if i + 1 < len(s):\n post = s_list[i + 1]\n print(pre, post)\n for char in chars:\n if char != pre and char != post:\n s_list[i] = char\n break\n res = \'\'.join(s_list)\n return res\n``` | 1 | 0 | [] | 1 |
replace-all-s-to-avoid-consecutive-repeating-characters | Python Solution | python-solution-by-sunnychugh-nsmp | ```\nclass Solution(object):\n def modifyString(self, s):\n """\n :type s: str\n :rtype: str\n """\n \n import rand | sunnychugh | NORMAL | 2021-02-03T00:13:57.711480+00:00 | 2021-02-03T00:13:57.711519+00:00 | 133 | false | ```\nclass Solution(object):\n def modifyString(self, s):\n """\n :type s: str\n :rtype: str\n """\n \n import random\n import string\n def gen_char():\n char = random.choice(string.ascii_lowercase)\n return char\n \n s = list(s) \n rnd = None\n for i, char in enumerate(s):\n if char == "?":\n rnd = gen_char()\n \n if len(s) == 1:\n return rnd \n \n if i == 0:\n while rnd == s[1]:\n rnd = gen_char()\n elif i == len(s)-1:\n while rnd == s[-2]:\n rnd = gen_char()\n else:\n while rnd == s[i-1] or rnd == s[i+1]:\n rnd = gen_char()\n \n s[i] = rnd\n \n return "".join(s) | 1 | 0 | [] | 0 |
replace-all-s-to-avoid-consecutive-repeating-characters | [JAVA] Simple solution, O(n) | java-simple-solution-on-by-yurokusa-u7qc | \nclass Solution {\n public String modifyString(String s) {\n var word = s.toCharArray();\n for (int i = 0; i < word.length; i++) {\n | yurokusa | NORMAL | 2021-02-02T21:40:32.419381+00:00 | 2021-02-02T21:40:46.057249+00:00 | 398 | false | ```\nclass Solution {\n public String modifyString(String s) {\n var word = s.toCharArray();\n for (int i = 0; i < word.length; i++) {\n if (word[i] == \'?\') {\n word[i] = \'a\';\n var left = i == 0 ? \'z\' : word[i - 1];\n var right = i == word.length - 1 ? \'z\': word[i + 1];\n while (word[i] == left || word[i] == right) word[i]++;\n }\n }\n return new String(word);\n }\n}\n``` | 1 | 0 | ['Java'] | 0 |
replace-all-s-to-avoid-consecutive-repeating-characters | easy java random solution | easy-java-random-solution-by-darkrwe-3ntv | \nclass Solution {\n Map<Integer, Character> map = new HashMap();\n Random rand = new Random();\n \n public String modifyString(String s) {\n | darkrwe | NORMAL | 2021-01-06T12:12:55.530037+00:00 | 2021-01-06T12:13:27.258334+00:00 | 222 | false | ```\nclass Solution {\n Map<Integer, Character> map = new HashMap();\n Random rand = new Random();\n \n public String modifyString(String s) {\n char[] ch = s.toCharArray();\n map.put(0, \'a\');\n map.put(1, \'b\');\n map.put(2, \'c\');\n map.put(3, \'d\');\n map.put(4, \'e\');\n map.put(5, \'f\');\n map.put(6, \'g\');\n map.put(7, \'h\');\n map.put(8, \'j\');\n map.put(9, \'i\');\n map.put(10, \'j\');\n map.put(11, \'k\');\n map.put(12, \'l\');\n map.put(13, \'m\');\n map.put(14, \'n\');\n map.put(15, \'o\');\n map.put(16, \'p\');\n map.put(17, \'r\');\n map.put(18, \'s\');\n map.put(19, \'t\');\n map.put(20, \'u\');\n map.put(21, \'v\');\n map.put(22, \'w\');\n map.put(23, \'x\');\n map.put(24, \'y\');\n map.put(25, \'z\');\n\n if(s.length() == 1){\n if( s.charAt(0) == \'?\')\n return String.valueOf(\'a\');\n }\n char cand;\n for(int i = 0 ; i < ch.length; i++) {\n if(ch[i] == \'?\'){\n if(i - 1 >= 0 && i+1 <= ch.length -1){\n do{\n cand = getRandomChar();\n }while(ch[i+1] == cand || ch[i-1] == cand);\n ch[i] = cand;\n }\n else if(i == 0){\n do{\n cand = getRandomChar();\n }while(ch[i+1] == cand);\n ch[i] = cand;\n }\n else if(i == ch.length-1){\n do{\n cand = getRandomChar();\n }while(ch[i-1] == cand);\n ch[i] = cand;\n }\n }\n }\n return new String(ch);\n }\n \n public char getRandomChar(){\n return map.get(rand.nextInt(26));\n }\n}\n``` | 1 | 1 | ['Java'] | 0 |
replace-all-s-to-avoid-consecutive-repeating-characters | [Java] Straightforward solution, beats 100% | java-straightforward-solution-beats-100-y0son | Time Complexity: O(n)\nSpace Complexity: O(n)\n```\nclass Solution {\n public String modifyString(String s) {\n StringBuilder sb = new StringBuilder() | Miss_S | NORMAL | 2020-12-25T17:47:52.175883+00:00 | 2020-12-25T17:49:17.662787+00:00 | 154 | false | Time Complexity: O(n)\nSpace Complexity: O(n)\n```\nclass Solution {\n public String modifyString(String s) {\n StringBuilder sb = new StringBuilder();\n for(int i=0;i<s.length();i++){\n if(s.charAt(i)!=\'?\'){\n sb.append(s.charAt(i));\n }else{\n sb.append(getChar(i>0?sb.charAt(i-1):\'\\0\',i<s.length()-1?s.charAt(i+1):\'\\0\'));\n }\n }\n return sb.toString();\n }\n \n private char getChar(char prev, char next){\n char c=\'a\';\n while(c<=\'z\'){\n if(c==prev || c==next)\n c++;\n else\n break;\n }\n return c;\n }\n} | 1 | 0 | [] | 0 |
replace-all-s-to-avoid-consecutive-repeating-characters | python3 simple solution | python3-simple-solution-by-julianayo-le97 | \nclass Solution:\n def modifyString(self, s: str) -> str:\n if s == \'?\':\n return \'a\'\n s_set = set(s)\n available_chars | JulianaTsv | NORMAL | 2020-12-13T09:26:24.593259+00:00 | 2020-12-13T09:51:57.842396+00:00 | 480 | false | ```\nclass Solution:\n def modifyString(self, s: str) -> str:\n if s == \'?\':\n return \'a\'\n s_set = set(s)\n available_chars = set()\n for char in s:\n if char == \'?\':\n if not available_chars:\n available_chars = set(\'abcdefghijklmnopqrstuvwxyz\').difference(s_set)\n s = s.replace(char, available_chars.pop(), 1)\n return s\n``` | 1 | 0 | ['Python3'] | 0 |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.