package com.fishercoder.solutions;

public class _1576 {
    public static class Solution1 {
        public String modifyString(String s) {
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < s.length(); i++) {
                char c = s.charAt(i);
                if (c == '?') {
                    char replacement = findReplacement(sb, i, s);
                    sb.append(replacement);
                } else {
                    sb.append(c);
                }
            }
            return sb.toString();
        }

        private char findReplacement(StringBuilder sb, int index, String s) {
            char replacement = 'a';
            if (index > 0) {
                int count = 1;
                while (replacement == sb.charAt(index - 1)) {
                    replacement += count % 26;
                }
            }
            if (index + 1 < s.length()) {
                int count = 1;
                while (replacement == s.charAt(index + 1) || (index > 0 && replacement == sb.charAt(index - 1))) {
                    replacement += count % 26;
                }
            }
            return replacement;
        }
    }
}

@@ -86,16 +86,177 @@
 tion1 %7B%0A
+        /**%0A         * Each char could have at most two neighbors, so we only need to toggle between three character candidates to avoid repetition.%0A         */%0A
         
@@ -310,44 +310,34 @@
     
-StringBuilder sb = new StringBuilder
+char%5B%5D arr = s.toCharArray
 ();%0A
@@ -368,26 +368,26 @@
  0; i %3C 
-s
+arr
 .length
-()
 ; i++) %7B
@@ -407,248 +407,140 @@
     
-char c = s.charAt(i);%0A                if (c == '?') %7B%0A                    char replacement = findReplacement(sb, i, s);%0A                    sb.append(replacement);%0A                %7D else %7B%0A                    sb.append(c);%0A                %7D
+if (arr%5Bi%5D == '?') %7B%0A                    for (int j = 0; j %3C 3; j++) %7B%0A                        if (i %3E 0 && arr%5Bi - 1%5D == 'a' + j) %7B
 %0A   
@@ -540,34 +540,35 @@
 ) %7B%0A            
-%7D%0A
+   
             retu
@@ -567,28 +567,17 @@
     
-return sb.toString()
+ continue
 ;%0A  
@@ -586,19 +586,16 @@
     
-%7D%0A%0A
         
 priv
@@ -594,282 +594,144 @@
     
-private char findReplacement(StringBuilder sb, int index, String s) %7B%0A            char replacement = 'a';%0A            if (index %3E 0) %7B%0A                int count = 1;%0A                while (replacement == sb.charAt(index - 1)) %7B%0A                    replacement += count %25 26;
+        %7D else if (i %3C arr.length - 1 && arr%5Bi + 1%5D == 'a' + j) %7B%0A                            continue;%0A                        %7D else %7B
 %0A   
@@ -735,34 +735,32 @@
                 
-%7D%0A
             %7D%0A  
@@ -759,244 +759,117 @@
     
-%7D%0A            if (index + 1 %3C s.length()) %7B%0A                int count = 1;%0A                while (replacement == s.charAt(index + 1) %7C%7C (index %3E 0 && replacement == sb.charAt(index - 1))) %7B%0A                    replacement += count %25 26;
+arr%5Bi%5D = (char) ('a' + j);%0A                            break;%0A                        %7D%0A                    %7D
 %0A   
@@ -920,19 +920,27 @@
 urn 
-replacement
+String.valueOf(arr)
 ;%0A  
@@ -943,20 +943,21 @@
 ;%0A        %7D%0A
+%0A
     %7D%0A%7D%0A

refactor 1576