<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                >[danger]求字符串的極值,極值是盡可能長的字符相等的子串的個數,比如aabbc,極值為3,aa,bb,c,然后進行多次修改,每次替換一個字符,求替換過后的字符串極值。 首先,可以使用雙指針的方法來計算字符串的極值。具體而言,我們維護左右兩個指針,從左到右遍歷字符串,如果遇到一個與前面不同的字符,就更新左指針的位置,并將當前的子串作為一個候選的極值,最后取所有候選的極值中長度最大的那個作為字符串的極值。以下是 JavaScript 示例代碼: ```javascript function getMaxEqualSubstringCount(str) { const n = str.length; let left = 0; let res = []; for (let i = 1; i <= n; i++) { if (i === n || str[i] !== str[i - 1]) { const len = i - left; res.push(len); left = i; } } return Math.max(...res); } const str = "aabbc"; const maxCount = getMaxEqualSubstringCount(str); console.log(`字符串 ${str} 的極值為 ${maxCount}`); // 輸出:字符串 aabbc 的極值為 2 ``` 接著,我們需要進行多次修改。對于每次修改,我們可以依次枚舉修改后的新字符,將它替換原來的字符,并重新計算字符串的極值。在計算字符串的極值時,我們可以利用前面的雙指針方法。以下是 JavaScript 示例代碼: ```javascript function getMaxEqualSubstringCount(str) { const n = str.length; let left = 0; let res = []; for (let i = 1; i <= n; i++) { if (i === n || str[i] !== str[i - 1]) { const len = i - left; res.push(len); left = i; } } return Math.max(...res); } function getMaxEqualSubstringCountAfterModify(str, index, newChar) { const arr = str.split(""); arr[index] = newChar; const modifiedStr = arr.join(""); return getMaxEqualSubstringCount(modifiedStr); } const str = "aabbc"; const maxCount = getMaxEqualSubstringCount(str); console.log(`原始字符串 ${str} 的極值為 ${maxCount}`); for (let i = 0; i < str.length; i++) { for (let j = 0; j < 26; j++) { const newChar = String.fromCharCode(97 + j); const newCount = getMaxEqualSubstringCountAfterModify(str, i, newChar); console.log(`將 ${str[i]} 替換成 ${newChar} 后的極值為 ${newCount}`); } } // 輸出: // 原始字符串 aabbc 的極值為 2 // 將 a 替換成 a 后的極值為 2 // 將 a 替換成 b 后的極值為 2 // 將 a 替換成 c 后的極值為 3 // 將 a 替換成 d 后的極值為 2 // ... // 將 c 替換成 s 后的極值為 2 // 將 c 替換成 t 后的極值為 2 // ... // 將 c 替換成 z 后的極值為 2 ``` 以上代碼中,我們首先定義了一個 `getMaxEqualSubstringCountAfterModify` 函數,用于計算將字符串中某個位置的字符替換成新字符后的字符串極值。在該函數中,我們先使用 `split` 函數將字符串轉成字符數組,然后修改指定位置的字符,并使用 `join` 函數將字符數組轉回字符串。最后,再調用 `getMaxEqualSubstringCount` 函數計算修改后的字符串的極值。 接著,在主函數中,我們依次枚舉每個字符以及可以替換的新字符,并使用 `getMaxEqualSubstringCountAfterModify` 函數計算替換后的字符串的極值。最后,輸出所有的極值。 需要注意的是,由于每次修改后都要重新計算字符串的極值,因此如果原始字符串較長或修改次數較多,上述方法可能會比較耗時。在實際應用中,可以考慮使用數據結構或算法優化來提高效率。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看