diff --git a/solution/1300-1399/1345.Jump Game IV/README.md b/solution/1300-1399/1345.Jump Game IV/README.md index 177724adbd22a..920dc3a2b255c 100644 --- a/solution/1300-1399/1345.Jump Game IV/README.md +++ b/solution/1300-1399/1345.Jump Game IV/README.md @@ -85,28 +85,22 @@ tags: ```python class Solution: def minJumps(self, arr: List[int]) -> int: - idx = defaultdict(list) - for i, v in enumerate(arr): - idx[v].append(i) - q = deque([(0, 0)]) + g = defaultdict(list) + for i, x in enumerate(arr): + g[x].append(i) + q = deque([0]) vis = {0} - while q: - i, step = q.popleft() - if i == len(arr) - 1: - return step - v = arr[i] - step += 1 - for j in idx[v]: - if j not in vis: - vis.add(j) - q.append((j, step)) - del idx[v] - if i + 1 < len(arr) and (i + 1) not in vis: - vis.add(i + 1) - q.append((i + 1, step)) - if i - 1 >= 0 and (i - 1) not in vis: - vis.add(i - 1) - q.append((i - 1, step)) + ans = 0 + while 1: + for _ in range(len(q)): + i = q.popleft() + if i == len(arr) - 1: + return ans + for j in (i + 1, i - 1, *g.pop(arr[i], [])): + if 0 <= j < len(arr) and j not in vis: + q.append(j) + vis.add(j) + ans += 1 ``` #### Java @@ -114,40 +108,36 @@ class Solution: ```java class Solution { public int minJumps(int[] arr) { - Map> idx = new HashMap<>(); + Map> g = new HashMap<>(); int n = arr.length; - for (int i = 0; i < n; ++i) { - idx.computeIfAbsent(arr[i], k -> new ArrayList<>()).add(i); + for (int i = 0; i < n; i++) { + g.computeIfAbsent(arr[i], k -> new ArrayList<>()).add(i); } - Deque q = new LinkedList<>(); - Set vis = new HashSet<>(); - vis.add(0); - q.offer(new int[] {0, 0}); - while (!q.isEmpty()) { - int[] e = q.pollFirst(); - int i = e[0], step = e[1]; - if (i == n - 1) { - return step; - } - int v = arr[i]; - ++step; - for (int j : idx.getOrDefault(v, new ArrayList<>())) { - if (!vis.contains(j)) { - vis.add(j); - q.offer(new int[] {j, step}); + boolean[] vis = new boolean[n]; + Deque q = new ArrayDeque<>(); + q.offer(0); + vis[0] = true; + for (int ans = 0;; ++ans) { + for (int k = q.size(); k > 0; --k) { + int i = q.poll(); + if (i == n - 1) { + return ans; + } + for (int j : g.get(arr[i])) { + if (!vis[j]) { + vis[j] = true; + q.offer(j); + } + } + g.get(arr[i]).clear(); + for (int j : new int[] {i - 1, i + 1}) { + if (0 <= j && j < n && !vis[j]) { + vis[j] = true; + q.offer(j); + } } - } - idx.remove(v); - if (i + 1 < n && !vis.contains(i + 1)) { - vis.add(i + 1); - q.offer(new int[] {i + 1, step}); - } - if (i - 1 >= 0 && !vis.contains(i - 1)) { - vis.add(i - 1); - q.offer(new int[] {i - 1, step}); } } - return -1; } } ``` @@ -158,39 +148,36 @@ class Solution { class Solution { public: int minJumps(vector& arr) { - unordered_map> idx; + unordered_map> g; int n = arr.size(); - for (int i = 0; i < n; ++i) idx[arr[i]].push_back(i); - queue> q; - q.emplace(0, 0); - unordered_set vis; - vis.insert(0); - while (!q.empty()) { - auto e = q.front(); - q.pop(); - int i = e.first, step = e.second; - if (i == n - 1) return step; - int v = arr[i]; - ++step; - if (idx.count(v)) { - for (int j : idx[v]) { - if (!vis.count(j)) { - vis.insert(j); - q.emplace(j, step); + for (int i = 0; i < n; ++i) { + g[arr[i]].push_back(i); + } + vector vis(n); + queue q{{0}}; + vis[0] = true; + for (int ans = 0;; ++ans) { + for (int k = q.size(); k; --k) { + int i = q.front(); + q.pop(); + if (i == n - 1) { + return ans; + } + for (int j : g[arr[i]]) { + if (!vis[j]) { + vis[j] = true; + q.push(j); + } + } + g[arr[i]].clear(); + for (int j : {i - 1, i + 1}) { + if (0 <= j && j < n && !vis[j]) { + vis[j] = true; + q.push(j); } } - idx.erase(v); - } - if (i + 1 < n && !vis.count(i + 1)) { - vis.insert(i + 1); - q.emplace(i + 1, step); - } - if (i - 1 >= 0 && !vis.count(i - 1)) { - vis.insert(i - 1); - q.emplace(i - 1, step); } } - return -1; } }; ``` @@ -199,38 +186,76 @@ public: ```go func minJumps(arr []int) int { - idx := map[int][]int{} - for i, v := range arr { - idx[v] = append(idx[v], i) + g := map[int][]int{} + for i, x := range arr { + g[x] = append(g[x], i) } - vis := map[int]bool{0: true} - type pair struct{ idx, step int } - q := []pair{{0, 0}} - for len(q) > 0 { - e := q[0] - q = q[1:] - i, step := e.idx, e.step - if i == len(arr)-1 { - return step - } - step++ - for _, j := range idx[arr[i]] { - if !vis[j] { - vis[j] = true - q = append(q, pair{j, step}) + n := len(arr) + q := []int{0} + vis := make([]bool, n) + vis[0] = true + for ans := 0; ; ans++ { + for k := len(q); k > 0; k-- { + i := q[0] + q = q[1:] + if i == n-1 { + return ans + } + for _, j := range g[arr[i]] { + if !vis[j] { + vis[j] = true + q = append(q, j) + } + } + g[arr[i]] = nil + for _, j := range []int{i - 1, i + 1} { + if 0 <= j && j < n && !vis[j] { + vis[j] = true + q = append(q, j) + } } - } - delete(idx, arr[i]) - if i+1 < len(arr) && !vis[i+1] { - vis[i+1] = true - q = append(q, pair{i + 1, step}) - } - if i-1 >= 0 && !vis[i-1] { - vis[i-1] = true - q = append(q, pair{i - 1, step}) } } - return -1 +} +``` + +#### TypeScript + +```ts +function minJumps(arr: number[]): number { + const g: Map = new Map(); + const n = arr.length; + for (let i = 0; i < n; ++i) { + if (!g.has(arr[i])) { + g.set(arr[i], []); + } + g.get(arr[i])!.push(i); + } + let q: number[] = [0]; + const vis: boolean[] = Array(n).fill(false); + vis[0] = true; + for (let ans = 0; ; ++ans) { + const nq: number[] = []; + for (const i of q) { + if (i === n - 1) { + return ans; + } + for (const j of g.get(arr[i])!) { + if (!vis[j]) { + vis[j] = true; + nq.push(j); + } + } + g.get(arr[i])!.length = 0; + for (const j of [i - 1, i + 1]) { + if (j >= 0 && j < n && !vis[j]) { + vis[j] = true; + nq.push(j); + } + } + } + q = nq; + } } ``` diff --git a/solution/1300-1399/1345.Jump Game IV/README_EN.md b/solution/1300-1399/1345.Jump Game IV/README_EN.md index 30c5fc19c6ae5..0e9b41b44cb92 100644 --- a/solution/1300-1399/1345.Jump Game IV/README_EN.md +++ b/solution/1300-1399/1345.Jump Game IV/README_EN.md @@ -82,28 +82,22 @@ tags: ```python class Solution: def minJumps(self, arr: List[int]) -> int: - idx = defaultdict(list) - for i, v in enumerate(arr): - idx[v].append(i) - q = deque([(0, 0)]) + g = defaultdict(list) + for i, x in enumerate(arr): + g[x].append(i) + q = deque([0]) vis = {0} - while q: - i, step = q.popleft() - if i == len(arr) - 1: - return step - v = arr[i] - step += 1 - for j in idx[v]: - if j not in vis: - vis.add(j) - q.append((j, step)) - del idx[v] - if i + 1 < len(arr) and (i + 1) not in vis: - vis.add(i + 1) - q.append((i + 1, step)) - if i - 1 >= 0 and (i - 1) not in vis: - vis.add(i - 1) - q.append((i - 1, step)) + ans = 0 + while 1: + for _ in range(len(q)): + i = q.popleft() + if i == len(arr) - 1: + return ans + for j in (i + 1, i - 1, *g.pop(arr[i], [])): + if 0 <= j < len(arr) and j not in vis: + q.append(j) + vis.add(j) + ans += 1 ``` #### Java @@ -111,40 +105,36 @@ class Solution: ```java class Solution { public int minJumps(int[] arr) { - Map> idx = new HashMap<>(); + Map> g = new HashMap<>(); int n = arr.length; - for (int i = 0; i < n; ++i) { - idx.computeIfAbsent(arr[i], k -> new ArrayList<>()).add(i); + for (int i = 0; i < n; i++) { + g.computeIfAbsent(arr[i], k -> new ArrayList<>()).add(i); } - Deque q = new LinkedList<>(); - Set vis = new HashSet<>(); - vis.add(0); - q.offer(new int[] {0, 0}); - while (!q.isEmpty()) { - int[] e = q.pollFirst(); - int i = e[0], step = e[1]; - if (i == n - 1) { - return step; - } - int v = arr[i]; - ++step; - for (int j : idx.getOrDefault(v, new ArrayList<>())) { - if (!vis.contains(j)) { - vis.add(j); - q.offer(new int[] {j, step}); + boolean[] vis = new boolean[n]; + Deque q = new ArrayDeque<>(); + q.offer(0); + vis[0] = true; + for (int ans = 0;; ++ans) { + for (int k = q.size(); k > 0; --k) { + int i = q.poll(); + if (i == n - 1) { + return ans; + } + for (int j : g.get(arr[i])) { + if (!vis[j]) { + vis[j] = true; + q.offer(j); + } + } + g.get(arr[i]).clear(); + for (int j : new int[] {i - 1, i + 1}) { + if (0 <= j && j < n && !vis[j]) { + vis[j] = true; + q.offer(j); + } } - } - idx.remove(v); - if (i + 1 < n && !vis.contains(i + 1)) { - vis.add(i + 1); - q.offer(new int[] {i + 1, step}); - } - if (i - 1 >= 0 && !vis.contains(i - 1)) { - vis.add(i - 1); - q.offer(new int[] {i - 1, step}); } } - return -1; } } ``` @@ -155,39 +145,36 @@ class Solution { class Solution { public: int minJumps(vector& arr) { - unordered_map> idx; + unordered_map> g; int n = arr.size(); - for (int i = 0; i < n; ++i) idx[arr[i]].push_back(i); - queue> q; - q.emplace(0, 0); - unordered_set vis; - vis.insert(0); - while (!q.empty()) { - auto e = q.front(); - q.pop(); - int i = e.first, step = e.second; - if (i == n - 1) return step; - int v = arr[i]; - ++step; - if (idx.count(v)) { - for (int j : idx[v]) { - if (!vis.count(j)) { - vis.insert(j); - q.emplace(j, step); + for (int i = 0; i < n; ++i) { + g[arr[i]].push_back(i); + } + vector vis(n); + queue q{{0}}; + vis[0] = true; + for (int ans = 0;; ++ans) { + for (int k = q.size(); k; --k) { + int i = q.front(); + q.pop(); + if (i == n - 1) { + return ans; + } + for (int j : g[arr[i]]) { + if (!vis[j]) { + vis[j] = true; + q.push(j); + } + } + g[arr[i]].clear(); + for (int j : {i - 1, i + 1}) { + if (0 <= j && j < n && !vis[j]) { + vis[j] = true; + q.push(j); } } - idx.erase(v); - } - if (i + 1 < n && !vis.count(i + 1)) { - vis.insert(i + 1); - q.emplace(i + 1, step); - } - if (i - 1 >= 0 && !vis.count(i - 1)) { - vis.insert(i - 1); - q.emplace(i - 1, step); } } - return -1; } }; ``` @@ -196,38 +183,76 @@ public: ```go func minJumps(arr []int) int { - idx := map[int][]int{} - for i, v := range arr { - idx[v] = append(idx[v], i) + g := map[int][]int{} + for i, x := range arr { + g[x] = append(g[x], i) } - vis := map[int]bool{0: true} - type pair struct{ idx, step int } - q := []pair{{0, 0}} - for len(q) > 0 { - e := q[0] - q = q[1:] - i, step := e.idx, e.step - if i == len(arr)-1 { - return step - } - step++ - for _, j := range idx[arr[i]] { - if !vis[j] { - vis[j] = true - q = append(q, pair{j, step}) + n := len(arr) + q := []int{0} + vis := make([]bool, n) + vis[0] = true + for ans := 0; ; ans++ { + for k := len(q); k > 0; k-- { + i := q[0] + q = q[1:] + if i == n-1 { + return ans + } + for _, j := range g[arr[i]] { + if !vis[j] { + vis[j] = true + q = append(q, j) + } + } + g[arr[i]] = nil + for _, j := range []int{i - 1, i + 1} { + if 0 <= j && j < n && !vis[j] { + vis[j] = true + q = append(q, j) + } } - } - delete(idx, arr[i]) - if i+1 < len(arr) && !vis[i+1] { - vis[i+1] = true - q = append(q, pair{i + 1, step}) - } - if i-1 >= 0 && !vis[i-1] { - vis[i-1] = true - q = append(q, pair{i - 1, step}) } } - return -1 +} +``` + +#### TypeScript + +```ts +function minJumps(arr: number[]): number { + const g: Map = new Map(); + const n = arr.length; + for (let i = 0; i < n; ++i) { + if (!g.has(arr[i])) { + g.set(arr[i], []); + } + g.get(arr[i])!.push(i); + } + let q: number[] = [0]; + const vis: boolean[] = Array(n).fill(false); + vis[0] = true; + for (let ans = 0; ; ++ans) { + const nq: number[] = []; + for (const i of q) { + if (i === n - 1) { + return ans; + } + for (const j of g.get(arr[i])!) { + if (!vis[j]) { + vis[j] = true; + nq.push(j); + } + } + g.get(arr[i])!.length = 0; + for (const j of [i - 1, i + 1]) { + if (j >= 0 && j < n && !vis[j]) { + vis[j] = true; + nq.push(j); + } + } + } + q = nq; + } } ``` diff --git a/solution/1300-1399/1345.Jump Game IV/Solution.cpp b/solution/1300-1399/1345.Jump Game IV/Solution.cpp index b1fe17852b940..375c32128f8d1 100644 --- a/solution/1300-1399/1345.Jump Game IV/Solution.cpp +++ b/solution/1300-1399/1345.Jump Game IV/Solution.cpp @@ -1,38 +1,35 @@ class Solution { public: int minJumps(vector& arr) { - unordered_map> idx; + unordered_map> g; int n = arr.size(); - for (int i = 0; i < n; ++i) idx[arr[i]].push_back(i); - queue> q; - q.emplace(0, 0); - unordered_set vis; - vis.insert(0); - while (!q.empty()) { - auto e = q.front(); - q.pop(); - int i = e.first, step = e.second; - if (i == n - 1) return step; - int v = arr[i]; - ++step; - if (idx.count(v)) { - for (int j : idx[v]) { - if (!vis.count(j)) { - vis.insert(j); - q.emplace(j, step); + for (int i = 0; i < n; ++i) { + g[arr[i]].push_back(i); + } + vector vis(n); + queue q{{0}}; + vis[0] = true; + for (int ans = 0;; ++ans) { + for (int k = q.size(); k; --k) { + int i = q.front(); + q.pop(); + if (i == n - 1) { + return ans; + } + for (int j : g[arr[i]]) { + if (!vis[j]) { + vis[j] = true; + q.push(j); + } + } + g[arr[i]].clear(); + for (int j : {i - 1, i + 1}) { + if (0 <= j && j < n && !vis[j]) { + vis[j] = true; + q.push(j); } } - idx.erase(v); - } - if (i + 1 < n && !vis.count(i + 1)) { - vis.insert(i + 1); - q.emplace(i + 1, step); - } - if (i - 1 >= 0 && !vis.count(i - 1)) { - vis.insert(i - 1); - q.emplace(i - 1, step); } } - return -1; } -}; \ No newline at end of file +}; diff --git a/solution/1300-1399/1345.Jump Game IV/Solution.go b/solution/1300-1399/1345.Jump Game IV/Solution.go index 61cfcc8be1b7b..44a7d1cc78446 100644 --- a/solution/1300-1399/1345.Jump Game IV/Solution.go +++ b/solution/1300-1399/1345.Jump Game IV/Solution.go @@ -1,34 +1,32 @@ func minJumps(arr []int) int { - idx := map[int][]int{} - for i, v := range arr { - idx[v] = append(idx[v], i) + g := map[int][]int{} + for i, x := range arr { + g[x] = append(g[x], i) } - vis := map[int]bool{0: true} - type pair struct{ idx, step int } - q := []pair{{0, 0}} - for len(q) > 0 { - e := q[0] - q = q[1:] - i, step := e.idx, e.step - if i == len(arr)-1 { - return step - } - step++ - for _, j := range idx[arr[i]] { - if !vis[j] { - vis[j] = true - q = append(q, pair{j, step}) + n := len(arr) + q := []int{0} + vis := make([]bool, n) + vis[0] = true + for ans := 0; ; ans++ { + for k := len(q); k > 0; k-- { + i := q[0] + q = q[1:] + if i == n-1 { + return ans + } + for _, j := range g[arr[i]] { + if !vis[j] { + vis[j] = true + q = append(q, j) + } + } + g[arr[i]] = nil + for _, j := range []int{i - 1, i + 1} { + if 0 <= j && j < n && !vis[j] { + vis[j] = true + q = append(q, j) + } } - } - delete(idx, arr[i]) - if i+1 < len(arr) && !vis[i+1] { - vis[i+1] = true - q = append(q, pair{i + 1, step}) - } - if i-1 >= 0 && !vis[i-1] { - vis[i-1] = true - q = append(q, pair{i - 1, step}) } } - return -1 -} \ No newline at end of file +} diff --git a/solution/1300-1399/1345.Jump Game IV/Solution.java b/solution/1300-1399/1345.Jump Game IV/Solution.java index 60d2120503408..31a7bf16e8752 100644 --- a/solution/1300-1399/1345.Jump Game IV/Solution.java +++ b/solution/1300-1399/1345.Jump Game IV/Solution.java @@ -1,38 +1,34 @@ class Solution { public int minJumps(int[] arr) { - Map> idx = new HashMap<>(); + Map> g = new HashMap<>(); int n = arr.length; - for (int i = 0; i < n; ++i) { - idx.computeIfAbsent(arr[i], k -> new ArrayList<>()).add(i); + for (int i = 0; i < n; i++) { + g.computeIfAbsent(arr[i], k -> new ArrayList<>()).add(i); } - Deque q = new LinkedList<>(); - Set vis = new HashSet<>(); - vis.add(0); - q.offer(new int[] {0, 0}); - while (!q.isEmpty()) { - int[] e = q.pollFirst(); - int i = e[0], step = e[1]; - if (i == n - 1) { - return step; - } - int v = arr[i]; - ++step; - for (int j : idx.getOrDefault(v, new ArrayList<>())) { - if (!vis.contains(j)) { - vis.add(j); - q.offer(new int[] {j, step}); + boolean[] vis = new boolean[n]; + Deque q = new ArrayDeque<>(); + q.offer(0); + vis[0] = true; + for (int ans = 0;; ++ans) { + for (int k = q.size(); k > 0; --k) { + int i = q.poll(); + if (i == n - 1) { + return ans; + } + for (int j : g.get(arr[i])) { + if (!vis[j]) { + vis[j] = true; + q.offer(j); + } + } + g.get(arr[i]).clear(); + for (int j : new int[] {i - 1, i + 1}) { + if (0 <= j && j < n && !vis[j]) { + vis[j] = true; + q.offer(j); + } } - } - idx.remove(v); - if (i + 1 < n && !vis.contains(i + 1)) { - vis.add(i + 1); - q.offer(new int[] {i + 1, step}); - } - if (i - 1 >= 0 && !vis.contains(i - 1)) { - vis.add(i - 1); - q.offer(new int[] {i - 1, step}); } } - return -1; } -} \ No newline at end of file +} diff --git a/solution/1300-1399/1345.Jump Game IV/Solution.py b/solution/1300-1399/1345.Jump Game IV/Solution.py index fe2a3e1869611..678affdfdb79b 100644 --- a/solution/1300-1399/1345.Jump Game IV/Solution.py +++ b/solution/1300-1399/1345.Jump Game IV/Solution.py @@ -1,24 +1,18 @@ class Solution: def minJumps(self, arr: List[int]) -> int: - idx = defaultdict(list) - for i, v in enumerate(arr): - idx[v].append(i) - q = deque([(0, 0)]) + g = defaultdict(list) + for i, x in enumerate(arr): + g[x].append(i) + q = deque([0]) vis = {0} - while q: - i, step = q.popleft() - if i == len(arr) - 1: - return step - v = arr[i] - step += 1 - for j in idx[v]: - if j not in vis: - vis.add(j) - q.append((j, step)) - del idx[v] - if i + 1 < len(arr) and (i + 1) not in vis: - vis.add(i + 1) - q.append((i + 1, step)) - if i - 1 >= 0 and (i - 1) not in vis: - vis.add(i - 1) - q.append((i - 1, step)) + ans = 0 + while 1: + for _ in range(len(q)): + i = q.popleft() + if i == len(arr) - 1: + return ans + for j in (i + 1, i - 1, *g.pop(arr[i], [])): + if 0 <= j < len(arr) and j not in vis: + q.append(j) + vis.add(j) + ans += 1 diff --git a/solution/1300-1399/1345.Jump Game IV/Solution.ts b/solution/1300-1399/1345.Jump Game IV/Solution.ts new file mode 100644 index 0000000000000..f06c3cfe0da83 --- /dev/null +++ b/solution/1300-1399/1345.Jump Game IV/Solution.ts @@ -0,0 +1,35 @@ +function minJumps(arr: number[]): number { + const g: Map = new Map(); + const n = arr.length; + for (let i = 0; i < n; ++i) { + if (!g.has(arr[i])) { + g.set(arr[i], []); + } + g.get(arr[i])!.push(i); + } + let q: number[] = [0]; + const vis: boolean[] = Array(n).fill(false); + vis[0] = true; + for (let ans = 0; ; ++ans) { + const nq: number[] = []; + for (const i of q) { + if (i === n - 1) { + return ans; + } + for (const j of g.get(arr[i])!) { + if (!vis[j]) { + vis[j] = true; + nq.push(j); + } + } + g.get(arr[i])!.length = 0; + for (const j of [i - 1, i + 1]) { + if (j >= 0 && j < n && !vis[j]) { + vis[j] = true; + nq.push(j); + } + } + } + q = nq; + } +} diff --git a/solution/1300-1399/1347.Minimum Number of Steps to Make Two Strings Anagram/README.md b/solution/1300-1399/1347.Minimum Number of Steps to Make Two Strings Anagram/README.md index 71f97d27326b4..ba29eeec484bd 100644 --- a/solution/1300-1399/1347.Minimum Number of Steps to Make Two Strings Anagram/README.md +++ b/solution/1300-1399/1347.Minimum Number of Steps to Make Two Strings Anagram/README.md @@ -46,7 +46,7 @@ tags:
输出:s = "anagram", t = "mangaar"
 输出:0
-提示:"anagram" 和 "mangaar" 本身就是一组字母异位词。 
+提示:"anagram" 和 "mangaar" 本身就是一组字母异位词。
 

示例 4:

@@ -77,13 +77,13 @@ tags: -### 方法一:数组或哈希表 +### 方法一:计数 -我们可以使用数组或哈希表 `cnt` 统计字符串 $s$ 中每个字符出现的次数,然后遍历字符串 $t$,对于 $t$ 中的每个字符,如果 $cnt$ 中对应的字符出现的次数大于 $0$,则将 $cnt$ 中对应的字符出现的次数减 $1$,否则将答案加 $1$。 +我们可以使用一个哈希表或者数组 $\textit{cnt}$ 来统计字符串 $\textit{s}$ 中每个字符出现的次数,然后遍历字符串 $\textit{t}$,对于每个字符,我们在 $\textit{cnt}$ 中将其出现的次数减一,如果减一后的值小于 $0$,说明这个字符在字符串 $\textit{t}$ 中出现的次数比在字符串 $\textit{s}$ 中多,我们需要将这个字符替换掉,将答案加一。 -时间复杂度 $O(n)$,空间复杂度 $O(C)$。其中 $n$ 为字符串 $s$ 的长度,而 $C$ 为字符集的大小。本题中 $C=26$。 +遍历结束后,返回答案即可。 - +时间复杂度 $O(m + n)$,空间复杂度 $O(|\Sigma|)$,其中 $m$ 和 $n$ 分别是字符串 $\textit{s}$ 和 $\textit{t}$ 的长度,而 $|\Sigma|$ 是字符集的大小,本题中字符集为小写字母,因此 $|\Sigma| = 26$。 #### Python3 @@ -93,10 +93,8 @@ class Solution: cnt = Counter(s) ans = 0 for c in t: - if cnt[c] > 0: - cnt[c] -= 1 - else: - ans += 1 + cnt[c] -= 1 + ans += cnt[c] < 0 return ans ``` @@ -106,13 +104,13 @@ class Solution: class Solution { public int minSteps(String s, String t) { int[] cnt = new int[26]; - for (int i = 0; i < s.length(); ++i) { - ++cnt[s.charAt(i) - 'a']; + for (char c : s.toCharArray()) { + cnt[c - 'a']++; } int ans = 0; - for (int i = 0; i < t.length(); ++i) { - if (--cnt[t.charAt(i) - 'a'] < 0) { - ++ans; + for (char c : t.toCharArray()) { + if (--cnt[c - 'a'] < 0) { + ans++; } } return ans; @@ -127,10 +125,14 @@ class Solution { public: int minSteps(string s, string t) { int cnt[26]{}; - for (char& c : s) ++cnt[c - 'a']; + for (char c : s) { + ++cnt[c - 'a']; + } int ans = 0; - for (char& c : t) { - ans += --cnt[c - 'a'] < 0; + for (char c : t) { + if (--cnt[c - 'a'] < 0) { + ++ans; + } } return ans; } @@ -155,6 +157,24 @@ func minSteps(s string, t string) (ans int) { } ``` +#### TypeScript + +```ts +function minSteps(s: string, t: string): number { + const cnt: number[] = Array(26).fill(0); + for (const c of s) { + ++cnt[c.charCodeAt(0) - 97]; + } + let ans = 0; + for (const c of t) { + if (--cnt[c.charCodeAt(0) - 97] < 0) { + ++ans; + } + } + return ans; +} +``` + #### JavaScript ```js @@ -164,15 +184,15 @@ func minSteps(s string, t string) (ans int) { * @return {number} */ var minSteps = function (s, t) { - const cnt = new Array(26).fill(0); + const cnt = Array(26).fill(0); for (const c of s) { - const i = c.charCodeAt(0) - 'a'.charCodeAt(0); - ++cnt[i]; + ++cnt[c.charCodeAt(0) - 97]; } let ans = 0; for (const c of t) { - const i = c.charCodeAt(0) - 'a'.charCodeAt(0); - ans += --cnt[i] < 0; + if (--cnt[c.charCodeAt(0) - 97] < 0) { + ++ans; + } } return ans; }; diff --git a/solution/1300-1399/1347.Minimum Number of Steps to Make Two Strings Anagram/README_EN.md b/solution/1300-1399/1347.Minimum Number of Steps to Make Two Strings Anagram/README_EN.md index d773d266cf07b..cb66547740838 100644 --- a/solution/1300-1399/1347.Minimum Number of Steps to Make Two Strings Anagram/README_EN.md +++ b/solution/1300-1399/1347.Minimum Number of Steps to Make Two Strings Anagram/README_EN.md @@ -48,7 +48,7 @@ tags:
 Input: s = "anagram", t = "mangaar"
 Output: 0
-Explanation: "anagram" and "mangaar" are anagrams. 
+Explanation: "anagram" and "mangaar" are anagrams.
 

 

@@ -66,7 +66,13 @@ tags: -### Solution 1 +### Solution 1: Counting + +We can use a hash table or an array $\textit{cnt}$ to count the occurrences of each character in the string $\textit{s}$. Then, we traverse the string $\textit{t}$. For each character, we decrement its count in $\textit{cnt}$. If the decremented value is less than $0$, it means that this character appears more times in the string $\textit{t}$ than in the string $\textit{s}$. In this case, we need to replace this character and increment the answer by one. + +After the traversal, we return the answer. + +The time complexity is $O(m + n)$, and the space complexity is $O(|\Sigma|)$, where $m$ and $n$ are the lengths of the strings $\textit{s}$ and $\textit{t}$, respectively, and $|\Sigma|$ is the size of the character set. In this problem, the character set consists of lowercase letters, so $|\Sigma| = 26$. @@ -78,10 +84,8 @@ class Solution: cnt = Counter(s) ans = 0 for c in t: - if cnt[c] > 0: - cnt[c] -= 1 - else: - ans += 1 + cnt[c] -= 1 + ans += cnt[c] < 0 return ans ``` @@ -91,13 +95,13 @@ class Solution: class Solution { public int minSteps(String s, String t) { int[] cnt = new int[26]; - for (int i = 0; i < s.length(); ++i) { - ++cnt[s.charAt(i) - 'a']; + for (char c : s.toCharArray()) { + cnt[c - 'a']++; } int ans = 0; - for (int i = 0; i < t.length(); ++i) { - if (--cnt[t.charAt(i) - 'a'] < 0) { - ++ans; + for (char c : t.toCharArray()) { + if (--cnt[c - 'a'] < 0) { + ans++; } } return ans; @@ -112,10 +116,14 @@ class Solution { public: int minSteps(string s, string t) { int cnt[26]{}; - for (char& c : s) ++cnt[c - 'a']; + for (char c : s) { + ++cnt[c - 'a']; + } int ans = 0; - for (char& c : t) { - ans += --cnt[c - 'a'] < 0; + for (char c : t) { + if (--cnt[c - 'a'] < 0) { + ++ans; + } } return ans; } @@ -140,6 +148,24 @@ func minSteps(s string, t string) (ans int) { } ``` +#### TypeScript + +```ts +function minSteps(s: string, t: string): number { + const cnt: number[] = Array(26).fill(0); + for (const c of s) { + ++cnt[c.charCodeAt(0) - 97]; + } + let ans = 0; + for (const c of t) { + if (--cnt[c.charCodeAt(0) - 97] < 0) { + ++ans; + } + } + return ans; +} +``` + #### JavaScript ```js @@ -149,15 +175,15 @@ func minSteps(s string, t string) (ans int) { * @return {number} */ var minSteps = function (s, t) { - const cnt = new Array(26).fill(0); + const cnt = Array(26).fill(0); for (const c of s) { - const i = c.charCodeAt(0) - 'a'.charCodeAt(0); - ++cnt[i]; + ++cnt[c.charCodeAt(0) - 97]; } let ans = 0; for (const c of t) { - const i = c.charCodeAt(0) - 'a'.charCodeAt(0); - ans += --cnt[i] < 0; + if (--cnt[c.charCodeAt(0) - 97] < 0) { + ++ans; + } } return ans; }; diff --git a/solution/1300-1399/1347.Minimum Number of Steps to Make Two Strings Anagram/Solution.cpp b/solution/1300-1399/1347.Minimum Number of Steps to Make Two Strings Anagram/Solution.cpp index 813ce96a5c938..47db9c31a3d25 100644 --- a/solution/1300-1399/1347.Minimum Number of Steps to Make Two Strings Anagram/Solution.cpp +++ b/solution/1300-1399/1347.Minimum Number of Steps to Make Two Strings Anagram/Solution.cpp @@ -2,11 +2,15 @@ class Solution { public: int minSteps(string s, string t) { int cnt[26]{}; - for (char& c : s) ++cnt[c - 'a']; + for (char c : s) { + ++cnt[c - 'a']; + } int ans = 0; - for (char& c : t) { - ans += --cnt[c - 'a'] < 0; + for (char c : t) { + if (--cnt[c - 'a'] < 0) { + ++ans; + } } return ans; } -}; \ No newline at end of file +}; diff --git a/solution/1300-1399/1347.Minimum Number of Steps to Make Two Strings Anagram/Solution.go b/solution/1300-1399/1347.Minimum Number of Steps to Make Two Strings Anagram/Solution.go index 6fc8697b6249f..3a2336c2babc9 100644 --- a/solution/1300-1399/1347.Minimum Number of Steps to Make Two Strings Anagram/Solution.go +++ b/solution/1300-1399/1347.Minimum Number of Steps to Make Two Strings Anagram/Solution.go @@ -10,4 +10,4 @@ func minSteps(s string, t string) (ans int) { } } return -} \ No newline at end of file +} diff --git a/solution/1300-1399/1347.Minimum Number of Steps to Make Two Strings Anagram/Solution.java b/solution/1300-1399/1347.Minimum Number of Steps to Make Two Strings Anagram/Solution.java index 8d958830efc3a..b01783cdd76b9 100644 --- a/solution/1300-1399/1347.Minimum Number of Steps to Make Two Strings Anagram/Solution.java +++ b/solution/1300-1399/1347.Minimum Number of Steps to Make Two Strings Anagram/Solution.java @@ -1,15 +1,15 @@ class Solution { public int minSteps(String s, String t) { int[] cnt = new int[26]; - for (int i = 0; i < s.length(); ++i) { - ++cnt[s.charAt(i) - 'a']; + for (char c : s.toCharArray()) { + cnt[c - 'a']++; } int ans = 0; - for (int i = 0; i < t.length(); ++i) { - if (--cnt[t.charAt(i) - 'a'] < 0) { - ++ans; + for (char c : t.toCharArray()) { + if (--cnt[c - 'a'] < 0) { + ans++; } } return ans; } -} \ No newline at end of file +} diff --git a/solution/1300-1399/1347.Minimum Number of Steps to Make Two Strings Anagram/Solution.js b/solution/1300-1399/1347.Minimum Number of Steps to Make Two Strings Anagram/Solution.js index f6b5a190569d9..7dec05313fe27 100644 --- a/solution/1300-1399/1347.Minimum Number of Steps to Make Two Strings Anagram/Solution.js +++ b/solution/1300-1399/1347.Minimum Number of Steps to Make Two Strings Anagram/Solution.js @@ -4,15 +4,15 @@ * @return {number} */ var minSteps = function (s, t) { - const cnt = new Array(26).fill(0); + const cnt = Array(26).fill(0); for (const c of s) { - const i = c.charCodeAt(0) - 'a'.charCodeAt(0); - ++cnt[i]; + ++cnt[c.charCodeAt(0) - 97]; } let ans = 0; for (const c of t) { - const i = c.charCodeAt(0) - 'a'.charCodeAt(0); - ans += --cnt[i] < 0; + if (--cnt[c.charCodeAt(0) - 97] < 0) { + ++ans; + } } return ans; }; diff --git a/solution/1300-1399/1347.Minimum Number of Steps to Make Two Strings Anagram/Solution.py b/solution/1300-1399/1347.Minimum Number of Steps to Make Two Strings Anagram/Solution.py index f0cebfb9f8065..ea83b675ba7ce 100644 --- a/solution/1300-1399/1347.Minimum Number of Steps to Make Two Strings Anagram/Solution.py +++ b/solution/1300-1399/1347.Minimum Number of Steps to Make Two Strings Anagram/Solution.py @@ -3,8 +3,6 @@ def minSteps(self, s: str, t: str) -> int: cnt = Counter(s) ans = 0 for c in t: - if cnt[c] > 0: - cnt[c] -= 1 - else: - ans += 1 + cnt[c] -= 1 + ans += cnt[c] < 0 return ans diff --git a/solution/1300-1399/1347.Minimum Number of Steps to Make Two Strings Anagram/Solution.ts b/solution/1300-1399/1347.Minimum Number of Steps to Make Two Strings Anagram/Solution.ts new file mode 100644 index 0000000000000..91098a497b518 --- /dev/null +++ b/solution/1300-1399/1347.Minimum Number of Steps to Make Two Strings Anagram/Solution.ts @@ -0,0 +1,13 @@ +function minSteps(s: string, t: string): number { + const cnt: number[] = Array(26).fill(0); + for (const c of s) { + ++cnt[c.charCodeAt(0) - 97]; + } + let ans = 0; + for (const c of t) { + if (--cnt[c.charCodeAt(0) - 97] < 0) { + ++ans; + } + } + return ans; +}