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 40f3d8b

Browse files
feat: add solutions to lc problem: No.3403 (doocs#4436)
No.3403.Find the Lexicographically Largest String From the Box I
1 parent 1ebe39f commit 40f3d8b

File tree

7 files changed

+58
-70
lines changed

7 files changed

+58
-70
lines changed

‎solution/3400-3499/3403.Find the Lexicographically Largest String From the Box I/README.md‎

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,13 @@ tags:
7979

8080
<!-- solution:start -->
8181

82-
### 方法一
82+
### 方法一:枚举子串左端点
83+
84+
如果我们固定子字符串的左端点,那么子字符串越长,字典序越大。假设子字符串左端点为 $i,ドル剩余子字符串的最小长度为 $\text{numFriends} - 1,ドル那么子字符串的右端点可以取到 $\min(n, i + n - (\text{numFriends} - 1)),ドル其中 $n$ 为字符串的长度。注意我们说的是左开右闭。
85+
86+
我们枚举所有可能的左端点,取出对应的子字符串,比较字典序,最终得到字典序最大的子字符串。
87+
88+
时间复杂度 $O(n^2),ドル空间复杂度 $O(n)$。其中 $n$ 为字符串的长度。
8389

8490
<!-- tabs:start -->
8591

@@ -91,11 +97,7 @@ class Solution:
9197
if numFriends == 1:
9298
return word
9399
n = len(word)
94-
ans = ""
95-
for i in range(n):
96-
k = min(n - i, n - numFriends + 1)
97-
ans = max(ans, word[i : i + k])
98-
return ans
100+
return max(word[i : i + n - (numFriends - 1)] for i in range(n))
99101
```
100102

101103
#### Java
@@ -109,8 +111,7 @@ class Solution {
109111
int n = word.length();
110112
String ans = "";
111113
for (int i = 0; i < n; ++i) {
112-
int k = Math.min(n - i, n - numFriends + 1);
113-
String t = word.substring(i, i + k);
114+
String t = word.substring(i, Math.min(n, i + n - (numFriends - 1)));
114115
if (ans.compareTo(t) < 0) {
115116
ans = t;
116117
}
@@ -129,12 +130,13 @@ public:
129130
if (numFriends == 1) {
130131
return word;
131132
}
132-
int n = word.size();
133-
string ans;
133+
int n = word.length();
134+
string ans = "";
134135
for (int i = 0; i < n; ++i) {
135-
int k = min(n - i, n - numFriends + 1);
136-
string t = word.substr(i, k);
137-
ans = max(ans, t);
136+
string t = word.substr(i, min(n - i, n - (numFriends - 1)));
137+
if (ans < t) {
138+
ans = t;
139+
}
138140
}
139141
return ans;
140142
}
@@ -149,9 +151,8 @@ func answerString(word string, numFriends int) (ans string) {
149151
return word
150152
}
151153
n := len(word)
152-
for i := range word {
153-
k := min(n-i, n-numFriends+1)
154-
t := word[i : i+k]
154+
for i := 0; i < n; i++ {
155+
t := word[i:min(n, i+n-(numFriends-1))]
155156
ans = max(ans, t)
156157
}
157158
return
@@ -165,14 +166,11 @@ function answerString(word: string, numFriends: number): string {
165166
if (numFriends === 1) {
166167
return word;
167168
}
168-
let ans: string = '';
169169
const n = word.length;
170-
for (let i = 0; i < n; ++i) {
171-
const k = Math.min(n - i, n - numFriends + 1);
172-
const t = word.slice(i, i + k);
173-
if (ans < t) {
174-
ans = t;
175-
}
170+
let ans = '';
171+
for (let i = 0; i < n; i++) {
172+
const t = word.slice(i, Math.min(n, i + n - (numFriends - 1)));
173+
ans = t > ans ? t : ans;
176174
}
177175
return ans;
178176
}

‎solution/3400-3499/3403.Find the Lexicographically Largest String From the Box I/README_EN.md‎

Lines changed: 21 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,13 @@ tags:
7777

7878
<!-- solution:start -->
7979

80-
### Solution 1
80+
### Solution 1: Enumerate Substring Left Endpoints
81+
82+
If we fix the left endpoint of the substring, the longer the substring, the larger its lexicographical order. Suppose the left endpoint of the substring is $i,ドル and the minimum length of the remaining substrings is $\text{numFriends} - 1,ドル then the right endpoint of the substring can be up to $\min(n, i + n - (\text{numFriends} - 1)),ドル where $n$ is the length of the string. Note that we are talking about left-closed, right-open intervals.
83+
84+
We enumerate all possible left endpoints, extract the corresponding substrings, compare their lexicographical order, and finally obtain the lexicographically largest substring.
85+
86+
The time complexity is $O(n^2),ドル and the space complexity is $O(n),ドル where $n$ is the length of the string.
8187

8288
<!-- tabs:start -->
8389

@@ -89,11 +95,7 @@ class Solution:
8995
if numFriends == 1:
9096
return word
9197
n = len(word)
92-
ans = ""
93-
for i in range(n):
94-
k = min(n - i, n - numFriends + 1)
95-
ans = max(ans, word[i : i + k])
96-
return ans
98+
return max(word[i : i + n - (numFriends - 1)] for i in range(n))
9799
```
98100

99101
#### Java
@@ -107,8 +109,7 @@ class Solution {
107109
int n = word.length();
108110
String ans = "";
109111
for (int i = 0; i < n; ++i) {
110-
int k = Math.min(n - i, n - numFriends + 1);
111-
String t = word.substring(i, i + k);
112+
String t = word.substring(i, Math.min(n, i + n - (numFriends - 1)));
112113
if (ans.compareTo(t) < 0) {
113114
ans = t;
114115
}
@@ -127,12 +128,13 @@ public:
127128
if (numFriends == 1) {
128129
return word;
129130
}
130-
int n = word.size();
131-
string ans;
131+
int n = word.length();
132+
string ans = "";
132133
for (int i = 0; i < n; ++i) {
133-
int k = min(n - i, n - numFriends + 1);
134-
string t = word.substr(i, k);
135-
ans = max(ans, t);
134+
string t = word.substr(i, min(n - i, n - (numFriends - 1)));
135+
if (ans < t) {
136+
ans = t;
137+
}
136138
}
137139
return ans;
138140
}
@@ -147,9 +149,8 @@ func answerString(word string, numFriends int) (ans string) {
147149
return word
148150
}
149151
n := len(word)
150-
for i := range word {
151-
k := min(n-i, n-numFriends+1)
152-
t := word[i : i+k]
152+
for i := 0; i < n; i++ {
153+
t := word[i:min(n, i+n-(numFriends-1))]
153154
ans = max(ans, t)
154155
}
155156
return
@@ -163,14 +164,11 @@ function answerString(word: string, numFriends: number): string {
163164
if (numFriends === 1) {
164165
return word;
165166
}
166-
let ans: string = '';
167167
const n = word.length;
168-
for (let i = 0; i < n; ++i) {
169-
const k = Math.min(n - i, n - numFriends + 1);
170-
const t = word.slice(i, i + k);
171-
if (ans < t) {
172-
ans = t;
173-
}
168+
let ans = '';
169+
for (let i = 0; i < n; i++) {
170+
const t = word.slice(i, Math.min(n, i + n - (numFriends - 1)));
171+
ans = t > ans ? t : ans;
174172
}
175173
return ans;
176174
}

‎solution/3400-3499/3403.Find the Lexicographically Largest String From the Box I/Solution.cpp‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@ class Solution {
44
if (numFriends == 1) {
55
return word;
66
}
7-
int n = word.size();
8-
string ans;
7+
int n = word.length();
8+
string ans = "";
99
for (int i = 0; i < n; ++i) {
10-
int k = min(n - i, n - numFriends + 1);
11-
string t = word.substr(i, k);
12-
ans = max(ans, t);
10+
string t = word.substr(i, min(n - i, n - (numFriends - 1)));
11+
if (ans < t) {
12+
ans = t;
13+
}
1314
}
1415
return ans;
1516
}

‎solution/3400-3499/3403.Find the Lexicographically Largest String From the Box I/Solution.go‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,9 @@ func answerString(word string, numFriends int) (ans string) {
33
return word
44
}
55
n := len(word)
6-
for i := range word {
7-
k := min(n-i, n-numFriends+1)
8-
t := word[i : i+k]
6+
for i := 0; i < n; i++ {
7+
t := word[i:min(n, i+n-(numFriends-1))]
98
ans = max(ans, t)
109
}
1110
return
12-
}
11+
}

‎solution/3400-3499/3403.Find the Lexicographically Largest String From the Box I/Solution.java‎

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,11 @@ public String answerString(String word, int numFriends) {
66
int n = word.length();
77
String ans = "";
88
for (int i = 0; i < n; ++i) {
9-
int k = Math.min(n - i, n - numFriends + 1);
10-
String t = word.substring(i, i + k);
9+
String t = word.substring(i, Math.min(n, i + n - (numFriends - 1)));
1110
if (ans.compareTo(t) < 0) {
1211
ans = t;
1312
}
1413
}
1514
return ans;
1615
}
17-
}
16+
}

‎solution/3400-3499/3403.Find the Lexicographically Largest String From the Box I/Solution.py‎

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,4 @@ def answerString(self, word: str, numFriends: int) -> str:
33
if numFriends == 1:
44
return word
55
n = len(word)
6-
ans = ""
7-
for i in range(n):
8-
k = min(n - i, n - numFriends + 1)
9-
ans = max(ans, word[i : i + k])
10-
return ans
6+
return max(word[i : i + n - (numFriends - 1)] for i in range(n))

‎solution/3400-3499/3403.Find the Lexicographically Largest String From the Box I/Solution.ts‎

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,11 @@ function answerString(word: string, numFriends: number): string {
22
if (numFriends === 1) {
33
return word;
44
}
5-
let ans: string = '';
65
const n = word.length;
7-
for (let i = 0; i < n; ++i) {
8-
const k = Math.min(n - i, n - numFriends + 1);
9-
const t = word.slice(i, i + k);
10-
if (ans < t) {
11-
ans = t;
12-
}
6+
let ans = '';
7+
for (let i = 0; i < n; i++) {
8+
const t = word.slice(i, Math.min(n, i + n - (numFriends - 1)));
9+
ans = t > ans ? t : ans;
1310
}
1411
return ans;
1512
}

0 commit comments

Comments
(0)

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