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..76786576123b8 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,33 @@ func modifyString(s string) string { } ``` +### **TypeScript** + +```ts +function modifyString(s: string): string { + const strArr = ["a", "b", "c"]; + const n = s.length; + + const result = []; + for (let i = 0; i < n; i++) { + let str = s[i]; + if (str === "?") { + const before = result[i - 1]; + const after = s[i + 1]; + + if (after !== "a" && before !== "a") { + str = strArr[0]; + } else if (after !== "b" && before !== "b") { + str = strArr[1]; + } else { + str = strArr[2]; + } + } + + result.push(str); + } + return result.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..97a21742b09b6 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,33 @@ func modifyString(s string) string { } ``` +### **TypeScript** + +```ts +function modifyString(s: string): string { + const strArr = ["a", "b", "c"]; + const n = s.length; + + const result = []; + for (let i = 0; i < n; i++) { + let str = s[i]; + if (str === "?") { + const before = result[i - 1]; + const after = s[i + 1]; + + if (after !== "a" && before !== "a") { + str = strArr[0]; + } else if (after !== "b" && before !== "b") { + str = strArr[1]; + } else { + str = strArr[2]; + } + } + + result.push(str); + } + return result.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..d19d98198c6b8 --- /dev/null +++ b/solution/1500-1599/1576.Replace All 's to Avoid Consecutive Repeating Characters/Solution.ts @@ -0,0 +1,24 @@ +function modifyString(s: string): string { + const strArr = ["a", "b", "c"]; + const n = s.length; + + const result = []; + for (let i = 0; i < n; i++) { + let str = s[i]; + if (str === "?") { + const before = result[i - 1]; + const after = s[i + 1]; + + if (after !== "a" && before !== "a") { + str = strArr[0]; + } else if (after !== "b" && before !== "b") { + str = strArr[1]; + } else { + str = strArr[2]; + } + } + + result.push(str); + } + return result.join(""); +}