Skip to content

Commit f93b0d1

Browse files
authored
feat: add solutions to lc problem: No.2744~2746 (doocs#2207)
* No.2744.Find Maximum Number of String Pairs * No.2745.Construct the Longest New String * No.2746.Decremental String Concatenation
1 parent 051a58b commit f93b0d1

File tree

3 files changed

+30
-0
lines changed

3 files changed

+30
-0
lines changed

solution/2700-2799/2744.Find Maximum Number of String Pairs/README_EN.md

+10
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,16 @@ It can be proven that 1 is the maximum number of pairs that can be formed.
5858

5959
## Solutions
6060

61+
**Solution 1: Hash Table**
62+
63+
We can use a hash table $cnt$ to store the number of occurrences of each string's reversed string in the array $words$.
64+
65+
Traverse the array $words$, for each string $w$, we directly add the value of $cnt[w]$ to the answer, then increase the occurrence count of $w$'s reversed string by $1$.
66+
67+
After the traversal ends, we can get the maximum number of matches.
68+
69+
The time complexity is $O(L)$, and the space complexity is $O(L)$, where $L$ is the sum of the lengths of the strings in the array $words$.
70+
6171
<!-- tabs:start -->
6272

6373
### **Python3**

solution/2700-2799/2745.Construct the Longest New String/README_EN.md

+10
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,16 @@ That string has length 14, and we can show that it is impossible to construct a
4040

4141
## Solutions
4242

43+
**Solution 1: Case Discussion**
44+
45+
We observe that the string 'AA' can only be followed by 'BB', and the string 'AB' can be placed at the beginning or end of the string. Therefore:
46+
47+
- If $x < y$, we can first alternately place 'BBAABBAA..BB', placing a total of $x$ 'AA' and $x+1$ 'BB', then place the remaining $z$ 'AB', with a total length of $(x \times 2 + z + 1) \times 2$;
48+
- If $x > y$, we can first alternately place 'AABBAABB..AA', placing a total of $y$ 'BB' and $y+1$ 'AA', then place the remaining $z$ 'AB', with a total length of $(y \times 2 + z + 1) \times 2$;
49+
- If $x = y$, we only need to alternately place 'AABB', placing a total of $x$ 'AA' and $y$ 'BB', then place the remaining $z$ 'AB', with a total length of $(x + y + z) \times 2$.
50+
51+
The time complexity is $O(1)$, and the space complexity is $O(1)$.
52+
4353
<!-- tabs:start -->
4454

4555
### **Python3**

solution/2700-2799/2746.Decremental String Concatenation/README_EN.md

+10
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,16 @@ It can be shown that the minimum possible length of str<sub>2</sub> is 6.
6868

6969
## Solutions
7070

71+
**Solution 1: Case Discussion**
72+
73+
We observe that the string 'AA' can only be followed by 'BB', and the string 'AB' can be placed at the beginning or end of the string. Therefore:
74+
75+
- If $x < y$, we can first alternately place 'BBAABBAA..BB', placing a total of $x$ 'AA' and $x+1$ 'BB', then place the remaining $z$ 'AB', with a total length of $(x \times 2 + z + 1) \times 2$;
76+
- If $x > y$, we can first alternately place 'AABBAABB..AA', placing a total of $y$ 'BB' and $y+1$ 'AA', then place the remaining $z$ 'AB', with a total length of $(y \times 2 + z + 1) \times 2$;
77+
- If $x = y$, we only need to alternately place 'AABB', placing a total of $x$ 'AA' and $y$ 'BB', then place the remaining $z$ 'AB', with a total length of $(x + y + z) \times 2$.
78+
79+
The time complexity is $O(1)$, and the space complexity is $O(1)$.
80+
7181
<!-- tabs:start -->
7282

7383
### **Python3**

0 commit comments

Comments
 (0)