From 7516f47677933bfdab6aa944fd84f203569ced14 Mon Sep 17 00:00:00 2001 From: yanglbme Date: Wed, 4 Jun 2025 08:44:14 +0800 Subject: [PATCH] feat: add solutions to lc problem: No.3406 No.3406.Find the Lexicographically Largest String From the Box II --- .../README.md | 142 +++++++++++++++++- .../README_EN.md | 142 +++++++++++++++++- .../Solution.cpp | 30 ++++ .../Solution.go | 27 ++++ .../Solution.java | 30 ++++ .../Solution.py | 21 +++ .../Solution.ts | 27 ++++ 7 files changed, 413 insertions(+), 6 deletions(-) create mode 100644 solution/3400-3499/3406.Find the Lexicographically Largest String From the Box II/Solution.cpp create mode 100644 solution/3400-3499/3406.Find the Lexicographically Largest String From the Box II/Solution.go create mode 100644 solution/3400-3499/3406.Find the Lexicographically Largest String From the Box II/Solution.java create mode 100644 solution/3400-3499/3406.Find the Lexicographically Largest String From the Box II/Solution.py create mode 100644 solution/3400-3499/3406.Find the Lexicographically Largest String From the Box II/Solution.ts diff --git a/solution/3400-3499/3406.Find the Lexicographically Largest String From the Box II/README.md b/solution/3400-3499/3406.Find the Lexicographically Largest String From the Box II/README.md index f791c699f9421..912deb52050fb 100644 --- a/solution/3400-3499/3406.Find the Lexicographically Largest String From the Box II/README.md +++ b/solution/3400-3499/3406.Find the Lexicographically Largest String From the Box II/README.md @@ -88,25 +88,161 @@ tags: #### Python3 ```python - +class Solution: + def answerString(self, word: str, numFriends: int) -> str: + if numFriends == 1: + return word + s = self.lastSubstring(word) + return s[: len(word) - numFriends + 1] + + def lastSubstring(self, s: str) -> str: + i, j, k = 0, 1, 0 + while j + k < len(s): + if s[i + k] == s[j + k]: + k += 1 + elif s[i + k] < s[j + k]: + i += k + 1 + k = 0 + if i >= j: + j = i + 1 + else: + j += k + 1 + k = 0 + return s[i:] ``` #### Java ```java - +class Solution { + public String answerString(String word, int numFriends) { + if (numFriends == 1) { + return word; + } + String s = lastSubstring(word); + return s.substring(0, Math.min(s.length(), word.length() - numFriends + 1)); + } + + public String lastSubstring(String s) { + int n = s.length(); + int i = 0, j = 1, k = 0; + while (j + k < n) { + int d = s.charAt(i + k) - s.charAt(j + k); + if (d == 0) { + ++k; + } else if (d < 0) { + i += k + 1; + k = 0; + if (i >= j) { + j = i + 1; + } + } else { + j += k + 1; + k = 0; + } + } + return s.substring(i); + } +} ``` #### C++ ```cpp - +class Solution { +public: + string answerString(string word, int numFriends) { + if (numFriends == 1) { + return word; + } + string s = lastSubstring(word); + return s.substr(0, min(s.length(), word.length() - numFriends + 1)); + } + + string lastSubstring(string& s) { + int n = s.size(); + int i = 0, j = 1, k = 0; + while (j + k < n) { + if (s[i + k] == s[j + k]) { + ++k; + } else if (s[i + k] < s[j + k]) { + i += k + 1; + k = 0; + if (i >= j) { + j = i + 1; + } + } else { + j += k + 1; + k = 0; + } + } + return s.substr(i); + } +}; ``` #### Go ```go +func answerString(word string, numFriends int) string { + if numFriends == 1 { + return word + } + s := lastSubstring(word) + return s[:min(len(s), len(word)-numFriends+1)] +} + +func lastSubstring(s string) string { + n := len(s) + i, j, k := 0, 1, 0 + for j+k < n { + if s[i+k] == s[j+k] { + k++ + } else if s[i+k] < s[j+k] { + i += k + 1 + k = 0 + if i >= j { + j = i + 1 + } + } else { + j += k + 1 + k = 0 + } + } + return s[i:] +} +``` +#### TypeScript + +```ts +function answerString(word: string, numFriends: number): string { + if (numFriends === 1) { + return word; + } + const s = lastSubstring(word); + return s.slice(0, word.length - numFriends + 1); +} + +function lastSubstring(s: string): string { + const n = s.length; + let i = 0; + for (let j = 1, k = 0; j + k < n; ) { + if (s[i + k] === s[j + k]) { + ++k; + } else if (s[i + k] < s[j + k]) { + i += k + 1; + k = 0; + if (i >= j) { + j = i + 1; + } + } else { + j += k + 1; + k = 0; + } + } + return s.slice(i); +} ``` diff --git a/solution/3400-3499/3406.Find the Lexicographically Largest String From the Box II/README_EN.md b/solution/3400-3499/3406.Find the Lexicographically Largest String From the Box II/README_EN.md index 03d8ecb5b18d7..5e47c4fc344be 100644 --- a/solution/3400-3499/3406.Find the Lexicographically Largest String From the Box II/README_EN.md +++ b/solution/3400-3499/3406.Find the Lexicographically Largest String From the Box II/README_EN.md @@ -84,25 +84,161 @@ If the first min(a.length, b.length) characters do not differ, then #### Python3 ```python - +class Solution: + def answerString(self, word: str, numFriends: int) -> str: + if numFriends == 1: + return word + s = self.lastSubstring(word) + return s[: len(word) - numFriends + 1] + + def lastSubstring(self, s: str) -> str: + i, j, k = 0, 1, 0 + while j + k < len(s): + if s[i + k] == s[j + k]: + k += 1 + elif s[i + k] < s[j + k]: + i += k + 1 + k = 0 + if i >= j: + j = i + 1 + else: + j += k + 1 + k = 0 + return s[i:] ``` #### Java ```java - +class Solution { + public String answerString(String word, int numFriends) { + if (numFriends == 1) { + return word; + } + String s = lastSubstring(word); + return s.substring(0, Math.min(s.length(), word.length() - numFriends + 1)); + } + + public String lastSubstring(String s) { + int n = s.length(); + int i = 0, j = 1, k = 0; + while (j + k < n) { + int d = s.charAt(i + k) - s.charAt(j + k); + if (d == 0) { + ++k; + } else if (d < 0) { + i += k + 1; + k = 0; + if (i >= j) { + j = i + 1; + } + } else { + j += k + 1; + k = 0; + } + } + return s.substring(i); + } +} ``` #### C++ ```cpp - +class Solution { +public: + string answerString(string word, int numFriends) { + if (numFriends == 1) { + return word; + } + string s = lastSubstring(word); + return s.substr(0, min(s.length(), word.length() - numFriends + 1)); + } + + string lastSubstring(string& s) { + int n = s.size(); + int i = 0, j = 1, k = 0; + while (j + k < n) { + if (s[i + k] == s[j + k]) { + ++k; + } else if (s[i + k] < s[j + k]) { + i += k + 1; + k = 0; + if (i >= j) { + j = i + 1; + } + } else { + j += k + 1; + k = 0; + } + } + return s.substr(i); + } +}; ``` #### Go ```go +func answerString(word string, numFriends int) string { + if numFriends == 1 { + return word + } + s := lastSubstring(word) + return s[:min(len(s), len(word)-numFriends+1)] +} + +func lastSubstring(s string) string { + n := len(s) + i, j, k := 0, 1, 0 + for j+k < n { + if s[i+k] == s[j+k] { + k++ + } else if s[i+k] < s[j+k] { + i += k + 1 + k = 0 + if i >= j { + j = i + 1 + } + } else { + j += k + 1 + k = 0 + } + } + return s[i:] +} +``` +#### TypeScript + +```ts +function answerString(word: string, numFriends: number): string { + if (numFriends === 1) { + return word; + } + const s = lastSubstring(word); + return s.slice(0, word.length - numFriends + 1); +} + +function lastSubstring(s: string): string { + const n = s.length; + let i = 0; + for (let j = 1, k = 0; j + k < n; ) { + if (s[i + k] === s[j + k]) { + ++k; + } else if (s[i + k] < s[j + k]) { + i += k + 1; + k = 0; + if (i >= j) { + j = i + 1; + } + } else { + j += k + 1; + k = 0; + } + } + return s.slice(i); +} ``` diff --git a/solution/3400-3499/3406.Find the Lexicographically Largest String From the Box II/Solution.cpp b/solution/3400-3499/3406.Find the Lexicographically Largest String From the Box II/Solution.cpp new file mode 100644 index 0000000000000..d968b6ad1546d --- /dev/null +++ b/solution/3400-3499/3406.Find the Lexicographically Largest String From the Box II/Solution.cpp @@ -0,0 +1,30 @@ +class Solution { +public: + string answerString(string word, int numFriends) { + if (numFriends == 1) { + return word; + } + string s = lastSubstring(word); + return s.substr(0, min(s.length(), word.length() - numFriends + 1)); + } + + string lastSubstring(string& s) { + int n = s.size(); + int i = 0, j = 1, k = 0; + while (j + k < n) { + if (s[i + k] == s[j + k]) { + ++k; + } else if (s[i + k] < s[j + k]) { + i += k + 1; + k = 0; + if (i >= j) { + j = i + 1; + } + } else { + j += k + 1; + k = 0; + } + } + return s.substr(i); + } +}; \ No newline at end of file diff --git a/solution/3400-3499/3406.Find the Lexicographically Largest String From the Box II/Solution.go b/solution/3400-3499/3406.Find the Lexicographically Largest String From the Box II/Solution.go new file mode 100644 index 0000000000000..83a686a4273e3 --- /dev/null +++ b/solution/3400-3499/3406.Find the Lexicographically Largest String From the Box II/Solution.go @@ -0,0 +1,27 @@ +func answerString(word string, numFriends int) string { + if numFriends == 1 { + return word + } + s := lastSubstring(word) + return s[:min(len(s), len(word)-numFriends+1)] +} + +func lastSubstring(s string) string { + n := len(s) + i, j, k := 0, 1, 0 + for j+k < n { + if s[i+k] == s[j+k] { + k++ + } else if s[i+k] < s[j+k] { + i += k + 1 + k = 0 + if i >= j { + j = i + 1 + } + } else { + j += k + 1 + k = 0 + } + } + return s[i:] +} \ No newline at end of file diff --git a/solution/3400-3499/3406.Find the Lexicographically Largest String From the Box II/Solution.java b/solution/3400-3499/3406.Find the Lexicographically Largest String From the Box II/Solution.java new file mode 100644 index 0000000000000..6340947439a44 --- /dev/null +++ b/solution/3400-3499/3406.Find the Lexicographically Largest String From the Box II/Solution.java @@ -0,0 +1,30 @@ +class Solution { + public String answerString(String word, int numFriends) { + if (numFriends == 1) { + return word; + } + String s = lastSubstring(word); + return s.substring(0, Math.min(s.length(), word.length() - numFriends + 1)); + } + + public String lastSubstring(String s) { + int n = s.length(); + int i = 0, j = 1, k = 0; + while (j + k < n) { + int d = s.charAt(i + k) - s.charAt(j + k); + if (d == 0) { + ++k; + } else if (d < 0) { + i += k + 1; + k = 0; + if (i >= j) { + j = i + 1; + } + } else { + j += k + 1; + k = 0; + } + } + return s.substring(i); + } +} \ No newline at end of file diff --git a/solution/3400-3499/3406.Find the Lexicographically Largest String From the Box II/Solution.py b/solution/3400-3499/3406.Find the Lexicographically Largest String From the Box II/Solution.py new file mode 100644 index 0000000000000..45ac9d082b3cd --- /dev/null +++ b/solution/3400-3499/3406.Find the Lexicographically Largest String From the Box II/Solution.py @@ -0,0 +1,21 @@ +class Solution: + def answerString(self, word: str, numFriends: int) -> str: + if numFriends == 1: + return word + s = self.lastSubstring(word) + return s[: len(word) - numFriends + 1] + + def lastSubstring(self, s: str) -> str: + i, j, k = 0, 1, 0 + while j + k < len(s): + if s[i + k] == s[j + k]: + k += 1 + elif s[i + k] < s[j + k]: + i += k + 1 + k = 0 + if i >= j: + j = i + 1 + else: + j += k + 1 + k = 0 + return s[i:] diff --git a/solution/3400-3499/3406.Find the Lexicographically Largest String From the Box II/Solution.ts b/solution/3400-3499/3406.Find the Lexicographically Largest String From the Box II/Solution.ts new file mode 100644 index 0000000000000..e57eeb11c9c1e --- /dev/null +++ b/solution/3400-3499/3406.Find the Lexicographically Largest String From the Box II/Solution.ts @@ -0,0 +1,27 @@ +function answerString(word: string, numFriends: number): string { + if (numFriends === 1) { + return word; + } + const s = lastSubstring(word); + return s.slice(0, word.length - numFriends + 1); +} + +function lastSubstring(s: string): string { + const n = s.length; + let i = 0; + for (let j = 1, k = 0; j + k < n; ) { + if (s[i + k] === s[j + k]) { + ++k; + } else if (s[i + k] < s[j + k]) { + i += k + 1; + k = 0; + if (i >= j) { + j = i + 1; + } + } else { + j += k + 1; + k = 0; + } + } + return s.slice(i); +}