diff --git a/solution/1500-1599/1576.Replace All 's to Avoid Consecutive Repeating Characters/README.md b/solution/1500-1599/1576.Replace All 's to Avoid Consecutive Repeating Characters/README.md index a1253a7208935..97a27ac24012e 100644 --- a/solution/1500-1599/1576.Replace All 's to Avoid Consecutive Repeating Characters/README.md +++ b/solution/1500-1599/1576.Replace All 's to Avoid Consecutive Repeating Characters/README.md @@ -155,4 +155,28 @@ func modifyString(s string) string { } ``` +### **TypeScript** + +```ts +function modifyString(s: string): string { + const strArr = s.split(""); + const n = s.length; + for (let i = 0; i < n; i++) { + if (strArr[i] === "?") { + const before = strArr[i - 1]; + const after = strArr[i + 1]; + + if (after !== "a" && before !== "a") { + strArr[i] = "a"; + } else if (after !== "b" && before !== "b") { + strArr[i] = "b"; + } else { + strArr[i] = "c"; + } + } + } + return strArr.join(""); +} +``` + diff --git a/solution/1500-1599/1576.Replace All 's to Avoid Consecutive Repeating Characters/README_EN.md b/solution/1500-1599/1576.Replace All 's to Avoid Consecutive Repeating Characters/README_EN.md index 593fe2d3ae2e9..6816aa0d3c1cf 100644 --- a/solution/1500-1599/1576.Replace All 's to Avoid Consecutive Repeating Characters/README_EN.md +++ b/solution/1500-1599/1576.Replace All 's to Avoid Consecutive Repeating Characters/README_EN.md @@ -143,4 +143,28 @@ func modifyString(s string) string { } ``` +### **TypeScript** + +```ts +function modifyString(s: string): string { + const strArr = s.split(""); + const n = s.length; + for (let i = 0; i < n; i++) { + if (strArr[i] === "?") { + const before = strArr[i - 1]; + const after = strArr[i + 1]; + + if (after !== "a" && before !== "a") { + strArr[i] = "a"; + } else if (after !== "b" && before !== "b") { + strArr[i] = "b"; + } else { + strArr[i] = "c"; + } + } + } + return strArr.join(""); +} +``` + diff --git a/solution/1500-1599/1576.Replace All 's to Avoid Consecutive Repeating Characters/Solution.ts b/solution/1500-1599/1576.Replace All 's to Avoid Consecutive Repeating Characters/Solution.ts new file mode 100644 index 0000000000000..9557d7e9c2914 --- /dev/null +++ b/solution/1500-1599/1576.Replace All 's to Avoid Consecutive Repeating Characters/Solution.ts @@ -0,0 +1,19 @@ +function modifyString(s: string): string { + const strArr = s.split(""); + const n = s.length; + for (let i = 0; i < n; i++) { + if (strArr[i] === "?") { + const before = strArr[i - 1]; + const after = strArr[i + 1]; + + if (after !== "a" && before !== "a") { + strArr[i] = "a"; + } else if (after !== "b" && before !== "b") { + strArr[i] = "b"; + } else { + strArr[i] = "c"; + } + } + } + return strArr.join(""); +}