Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 3001ee4

Browse files
feat: solve No.1347
1 parent 838b398 commit 3001ee4

File tree

2 files changed

+89
-0
lines changed

2 files changed

+89
-0
lines changed
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
# 1347. Minimum Number of Steps to Make Two Strings Anagram
2+
3+
- Difficulty: Medium.
4+
- Related Topics: Hash Table, String, Counting.
5+
- Similar Questions: Determine if Two Strings Are Close, Minimum Number of Steps to Make Two Strings Anagram II.
6+
7+
## Problem
8+
9+
You are given two strings of the same length `s` and `t`. In one step you can choose **any character** of `t` and replace it with **another character**.
10+
11+
Return **the minimum number of steps** to make `t` an anagram of `s`.
12+
13+
An **Anagram** of a string is a string that contains the same characters with a different (or the same) ordering.
14+
15+
16+
Example 1:
17+
18+
```
19+
Input: s = "bab", t = "aba"
20+
Output: 1
21+
Explanation: Replace the first 'a' in t with b, t = "bba" which is anagram of s.
22+
```
23+
24+
Example 2:
25+
26+
```
27+
Input: s = "leetcode", t = "practice"
28+
Output: 5
29+
Explanation: Replace 'p', 'r', 'a', 'i' and 'c' from t with proper characters to make t anagram of s.
30+
```
31+
32+
Example 3:
33+
34+
```
35+
Input: s = "anagram", t = "mangaar"
36+
Output: 0
37+
Explanation: "anagram" and "mangaar" are anagrams.
38+
```
39+
40+
41+
**Constraints:**
42+
43+
44+
45+
- `1 <= s.length <= 5 * 104`
46+
47+
- `s.length == t.length`
48+
49+
- `s` and `t` consist of lowercase English letters only.
50+
51+
52+
53+
## Solution
54+
55+
```javascript
56+
/**
57+
* @param {string} s
58+
* @param {string} t
59+
* @return {number}
60+
*/
61+
var minSteps = function(s, t) {
62+
var map = Array(26).fill(0);
63+
var a = 'a'.charCodeAt(0);
64+
for (var i = 0; i < t.length; i++) {
65+
map[t.charCodeAt(i) - a]++;
66+
}
67+
for (var j = 0; j < s.length; j++) {
68+
map[s.charCodeAt(j) - a]--;
69+
}
70+
var res = 0;
71+
for (var k = 0; k < 26; k++) {
72+
res += Math.abs(map[k]);
73+
}
74+
return res / 2;
75+
}
76+
```
77+
78+
**Explain:**
79+
80+
nope.
81+
82+
**Complexity:**
83+
84+
* Time complexity : O(n).
85+
* Space complexity : O(1).

‎create/index.js‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,10 @@ const queryAndCreate = name => {
9595
const url = `https://leetcode.com/graphql?query=query%20getQuestionDetail($titleSlug:%20String!)%20%7B%0A%20%20question(titleSlug:%20$titleSlug)%20%7B%0A%20%20%20%20questionId%0A%20%20%20%20questionFrontendId%0A%20%20%20%20questionTitle%0A%20%20%20%20content%0A%20%20%20%20difficulty%0A%20%20%20%20stats%0A%20%20%20%20similarQuestions%0A%20%20%20%20topicTags%20%7B%0A%20%20%20%20%20%20name%0A%20%20%20%20%7D%0A%20%20%7D%0A%7D%0A&operationName=getQuestionDetail&variables=%7B%22titleSlug%22:%22${name}%22%7D`;
9696

9797
axios.get(url).then(res => {
98+
if (!res.data?.data?.question) {
99+
console.error('fetch question info error, probably wrong problem url');
100+
return;
101+
}
98102
create(res.data.data.question);
99103
}).catch(err => {
100104
console.error(err);

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /