Skip to content

feat: add solutions to lc problem: No.3403 #4436

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 26, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,13 @@ tags:

<!-- solution:start -->

### 方法一
### 方法一:枚举子串左端点

如果我们固定子字符串的左端点,那么子字符串越长,字典序越大。假设子字符串左端点为 $i$,剩余子字符串的最小长度为 $\text{numFriends} - 1$,那么子字符串的右端点可以取到 $\min(n, i + n - (\text{numFriends} - 1))$,其中 $n$ 为字符串的长度。注意我们说的是左开右闭。

我们枚举所有可能的左端点,取出对应的子字符串,比较字典序,最终得到字典序最大的子字符串。

时间复杂度 $O(n^2)$,空间复杂度 $O(n)$。其中 $n$ 为字符串的长度。

<!-- tabs:start -->

Expand All @@ -91,11 +97,7 @@ class Solution:
if numFriends == 1:
return word
n = len(word)
ans = ""
for i in range(n):
k = min(n - i, n - numFriends + 1)
ans = max(ans, word[i : i + k])
return ans
return max(word[i : i + n - (numFriends - 1)] for i in range(n))
```

#### Java
Expand All @@ -109,8 +111,7 @@ class Solution {
int n = word.length();
String ans = "";
for (int i = 0; i < n; ++i) {
int k = Math.min(n - i, n - numFriends + 1);
String t = word.substring(i, i + k);
String t = word.substring(i, Math.min(n, i + n - (numFriends - 1)));
if (ans.compareTo(t) < 0) {
ans = t;
}
Expand All @@ -129,12 +130,13 @@ public:
if (numFriends == 1) {
return word;
}
int n = word.size();
string ans;
int n = word.length();
string ans = "";
for (int i = 0; i < n; ++i) {
int k = min(n - i, n - numFriends + 1);
string t = word.substr(i, k);
ans = max(ans, t);
string t = word.substr(i, min(n - i, n - (numFriends - 1)));
if (ans < t) {
ans = t;
}
}
return ans;
}
Expand All @@ -149,9 +151,8 @@ func answerString(word string, numFriends int) (ans string) {
return word
}
n := len(word)
for i := range word {
k := min(n-i, n-numFriends+1)
t := word[i : i+k]
for i := 0; i < n; i++ {
t := word[i:min(n, i+n-(numFriends-1))]
ans = max(ans, t)
}
return
Expand All @@ -165,14 +166,11 @@ function answerString(word: string, numFriends: number): string {
if (numFriends === 1) {
return word;
}
let ans: string = '';
const n = word.length;
for (let i = 0; i < n; ++i) {
const k = Math.min(n - i, n - numFriends + 1);
const t = word.slice(i, i + k);
if (ans < t) {
ans = t;
}
let ans = '';
for (let i = 0; i < n; i++) {
const t = word.slice(i, Math.min(n, i + n - (numFriends - 1)));
ans = t > ans ? t : ans;
}
return ans;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,13 @@ tags:

<!-- solution:start -->

### Solution 1
### Solution 1: Enumerate Substring Left Endpoints

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.

We enumerate all possible left endpoints, extract the corresponding substrings, compare their lexicographical order, and finally obtain the lexicographically largest substring.

The time complexity is $O(n^2)$, and the space complexity is $O(n)$, where $n$ is the length of the string.

<!-- tabs:start -->

Expand All @@ -89,11 +95,7 @@ class Solution:
if numFriends == 1:
return word
n = len(word)
ans = ""
for i in range(n):
k = min(n - i, n - numFriends + 1)
ans = max(ans, word[i : i + k])
return ans
return max(word[i : i + n - (numFriends - 1)] for i in range(n))
```

#### Java
Expand All @@ -107,8 +109,7 @@ class Solution {
int n = word.length();
String ans = "";
for (int i = 0; i < n; ++i) {
int k = Math.min(n - i, n - numFriends + 1);
String t = word.substring(i, i + k);
String t = word.substring(i, Math.min(n, i + n - (numFriends - 1)));
if (ans.compareTo(t) < 0) {
ans = t;
}
Expand All @@ -127,12 +128,13 @@ public:
if (numFriends == 1) {
return word;
}
int n = word.size();
string ans;
int n = word.length();
string ans = "";
for (int i = 0; i < n; ++i) {
int k = min(n - i, n - numFriends + 1);
string t = word.substr(i, k);
ans = max(ans, t);
string t = word.substr(i, min(n - i, n - (numFriends - 1)));
if (ans < t) {
ans = t;
}
}
return ans;
}
Expand All @@ -147,9 +149,8 @@ func answerString(word string, numFriends int) (ans string) {
return word
}
n := len(word)
for i := range word {
k := min(n-i, n-numFriends+1)
t := word[i : i+k]
for i := 0; i < n; i++ {
t := word[i:min(n, i+n-(numFriends-1))]
ans = max(ans, t)
}
return
Expand All @@ -163,14 +164,11 @@ function answerString(word: string, numFriends: number): string {
if (numFriends === 1) {
return word;
}
let ans: string = '';
const n = word.length;
for (let i = 0; i < n; ++i) {
const k = Math.min(n - i, n - numFriends + 1);
const t = word.slice(i, i + k);
if (ans < t) {
ans = t;
}
let ans = '';
for (let i = 0; i < n; i++) {
const t = word.slice(i, Math.min(n, i + n - (numFriends - 1)));
ans = t > ans ? t : ans;
}
return ans;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,13 @@ class Solution {
if (numFriends == 1) {
return word;
}
int n = word.size();
string ans;
int n = word.length();
string ans = "";
for (int i = 0; i < n; ++i) {
int k = min(n - i, n - numFriends + 1);
string t = word.substr(i, k);
ans = max(ans, t);
string t = word.substr(i, min(n - i, n - (numFriends - 1)));
if (ans < t) {
ans = t;
}
}
return ans;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@ func answerString(word string, numFriends int) (ans string) {
return word
}
n := len(word)
for i := range word {
k := min(n-i, n-numFriends+1)
t := word[i : i+k]
for i := 0; i < n; i++ {
t := word[i:min(n, i+n-(numFriends-1))]
ans = max(ans, t)
}
return
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,11 @@ public String answerString(String word, int numFriends) {
int n = word.length();
String ans = "";
for (int i = 0; i < n; ++i) {
int k = Math.min(n - i, n - numFriends + 1);
String t = word.substring(i, i + k);
String t = word.substring(i, Math.min(n, i + n - (numFriends - 1)));
if (ans.compareTo(t) < 0) {
ans = t;
}
}
return ans;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,4 @@ def answerString(self, word: str, numFriends: int) -> str:
if numFriends == 1:
return word
n = len(word)
ans = ""
for i in range(n):
k = min(n - i, n - numFriends + 1)
ans = max(ans, word[i : i + k])
return ans
return max(word[i : i + n - (numFriends - 1)] for i in range(n))
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,11 @@ function answerString(word: string, numFriends: number): string {
if (numFriends === 1) {
return word;
}
let ans: string = '';
const n = word.length;
for (let i = 0; i < n; ++i) {
const k = Math.min(n - i, n - numFriends + 1);
const t = word.slice(i, i + k);
if (ans < t) {
ans = t;
}
let ans = '';
for (let i = 0; i < n; i++) {
const t = word.slice(i, Math.min(n, i + n - (numFriends - 1)));
ans = t > ans ? t : ans;
}
return ans;
}