From b643809495f9f57671b0bb9b2dd4c7708565cb18 Mon Sep 17 00:00:00 2001 From: zhaocchen Date: Tue, 4 Jan 2022 16:33:06 +0800 Subject: [PATCH] feat: add typescript solution to lc problem: No.1737 No.1737.Change Minimum Characters to Satisfy One of Three Conditions --- .../README.md | 31 ++++++++++++++++++ .../README_EN.md | 32 +++++++++++++++++++ .../Solution.ts | 25 +++++++++++++++ 3 files changed, 88 insertions(+) create mode 100644 solution/1700-1799/1737.Change Minimum Characters to Satisfy One of Three Conditions/Solution.ts diff --git a/solution/1700-1799/1737.Change Minimum Characters to Satisfy One of Three Conditions/README.md b/solution/1700-1799/1737.Change Minimum Characters to Satisfy One of Three Conditions/README.md index 762fb5851a153..bf0325e20d422 100644 --- a/solution/1700-1799/1737.Change Minimum Characters to Satisfy One of Three Conditions/README.md +++ b/solution/1700-1799/1737.Change Minimum Characters to Satisfy One of Three Conditions/README.md @@ -50,6 +50,7 @@ ## 解法 +前缀和 @@ -69,6 +70,36 @@ ``` +### **TypeScript** + +```ts +function minCharacters(a: string, b: string): number { + const m = a.length, n = b.length; + let count1 = new Array(26).fill(0); + let count2 = new Array(26).fill(0); + const base = 'a'.charCodeAt(0); + + for (let char of a) { + count1[char.charCodeAt(0) - base]++; + } + for (let char of b) { + count2[char.charCodeAt(0) - base]++; + } + + let pre1 = 0, pre2 = 0; + let ans = m + n; + for (let i = 0; i < 25; i++) { + pre1 += count1[i]; + pre2 += count2[i]; + // case1, case2, case3 + ans = Math.min(ans, m - pre1 + pre2, pre1 + n - pre2, m + n - count1[i] - count2[i]); + } + ans = Math.min(ans, m + n - count1[25] - count2[25]); + + return ans; +}; +``` + ### **...** ``` diff --git a/solution/1700-1799/1737.Change Minimum Characters to Satisfy One of Three Conditions/README_EN.md b/solution/1700-1799/1737.Change Minimum Characters to Satisfy One of Three Conditions/README_EN.md index 9b6dc36823951..828ae38726a60 100644 --- a/solution/1700-1799/1737.Change Minimum Characters to Satisfy One of Three Conditions/README_EN.md +++ b/solution/1700-1799/1737.Change Minimum Characters to Satisfy One of Three Conditions/README_EN.md @@ -47,6 +47,8 @@ The best way was done in 2 operations (either condition 1 or condition 3). ## Solutions +Prefix Sum + ### **Python3** @@ -61,6 +63,36 @@ The best way was done in 2 operations (either condition 1 or condition 3). ``` +### **TypeScript** + +```ts +function minCharacters(a: string, b: string): number { + const m = a.length, n = b.length; + let count1 = new Array(26).fill(0); + let count2 = new Array(26).fill(0); + const base = 'a'.charCodeAt(0); + + for (let char of a) { + count1[char.charCodeAt(0) - base]++; + } + for (let char of b) { + count2[char.charCodeAt(0) - base]++; + } + + let pre1 = 0, pre2 = 0; + let ans = m + n; + for (let i = 0; i < 25; i++) { + pre1 += count1[i]; + pre2 += count2[i]; + // case1, case2, case3 + ans = Math.min(ans, m - pre1 + pre2, pre1 + n - pre2, m + n - count1[i] - count2[i]); + } + ans = Math.min(ans, m + n - count1[25] - count2[25]); + + return ans; +}; +``` + ### **...** ``` diff --git a/solution/1700-1799/1737.Change Minimum Characters to Satisfy One of Three Conditions/Solution.ts b/solution/1700-1799/1737.Change Minimum Characters to Satisfy One of Three Conditions/Solution.ts new file mode 100644 index 0000000000000..54b8ac340dde9 --- /dev/null +++ b/solution/1700-1799/1737.Change Minimum Characters to Satisfy One of Three Conditions/Solution.ts @@ -0,0 +1,25 @@ +function minCharacters(a: string, b: string): number { + const m = a.length, n = b.length; + let count1 = new Array(26).fill(0); + let count2 = new Array(26).fill(0); + const base = 'a'.charCodeAt(0); + + for (let char of a) { + count1[char.charCodeAt(0) - base]++; + } + for (let char of b) { + count2[char.charCodeAt(0) - base]++; + } + + let pre1 = 0, pre2 = 0; + let ans = m + n; + for (let i = 0; i < 25; i++) { + pre1 += count1[i]; + pre2 += count2[i]; + // case1, case2, case3 + ans = Math.min(ans, m - pre1 + pre2, pre1 + n - pre2, m + n - count1[i] - count2[i]); + } + ans = Math.min(ans, m + n - count1[25] - count2[25]); + + return ans; +}; \ No newline at end of file