Skip to content

Commit

Permalink
feat: update solutions to lc problems: No.1044,1392
Browse files Browse the repository at this point in the history
* No.1044.Longest Duplicate Substring
* No.1392.Longest Happy Prefix
  • Loading branch information
yanglbme committed Dec 23, 2021
1 parent f00f880 commit 770b85e
Show file tree
Hide file tree
Showing 10 changed files with 111 additions and 126 deletions.
15 changes: 8 additions & 7 deletions solution/1000-1099/1044.Longest Duplicate Substring/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,9 @@ class Solution {
while (left < right) {
int mid = (left + right + 1) >> 1;
String t = check(s, mid);
ans = t.length() > ans.length() ? t : ans;
if (t.length() > 0) {
left = mid;
ans = t;
} else {
right = mid - 1;
}
Expand Down Expand Up @@ -147,9 +147,12 @@ public:
{
int mid = (left + right + 1) >> 1;
string t = check(s, mid);
if (t.size() > ans.size()) ans = t;
if (t.size() > 0) left = mid;
else right = mid - 1;
if (t.empty()) right = mid - 1;
else
{
left = mid;
ans = t;
}
}
return ans;
}
Expand Down Expand Up @@ -198,11 +201,9 @@ func longestDupSubstring(s string) string {
for left < right {
mid := (left + right + 1) >> 1
t := check(mid)
if len(t) > len(ans) {
ans = t
}
if len(t) > 0 {
left = mid
ans = t
} else {
right = mid - 1
}
Expand Down
15 changes: 8 additions & 7 deletions solution/1000-1099/1044.Longest Duplicate Substring/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,9 @@ class Solution {
while (left < right) {
int mid = (left + right + 1) >> 1;
String t = check(s, mid);
ans = t.length() > ans.length() ? t : ans;
if (t.length() > 0) {
left = mid;
ans = t;
} else {
right = mid - 1;
}
Expand Down Expand Up @@ -128,9 +128,12 @@ public:
{
int mid = (left + right + 1) >> 1;
string t = check(s, mid);
if (t.size() > ans.size()) ans = t;
if (t.size() > 0) left = mid;
else right = mid - 1;
if (t.empty()) right = mid - 1;
else
{
left = mid;
ans = t;
}
}
return ans;
}
Expand Down Expand Up @@ -179,11 +182,9 @@ func longestDupSubstring(s string) string {
for left < right {
mid := (left + right + 1) >> 1
t := check(mid)
if len(t) > len(ans) {
ans = t
}
if len(t) > 0 {
left = mid
ans = t
} else {
right = mid - 1
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,12 @@ class Solution {
{
int mid = (left + right + 1) >> 1;
string t = check(s, mid);
if (t.size() > ans.size()) ans = t;
if (t.size() > 0) left = mid;
else right = mid - 1;
if (t.empty()) right = mid - 1;
else
{
left = mid;
ans = t;
}
}
return ans;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,9 @@ func longestDupSubstring(s string) string {
for left < right {
mid := (left + right + 1) >> 1
t := check(mid)
if len(t) > len(ans) {
ans = t
}
if len(t) > 0 {
left = mid
ans = t
} else {
right = mid - 1
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ public String longestDupSubstring(String s) {
while (left < right) {
int mid = (left + right + 1) >> 1;
String t = check(s, mid);
ans = t.length() > ans.length() ? t : ans;
if (t.length() > 0) {
left = mid;
ans = t;
} else {
right = mid - 1;
}
Expand Down
64 changes: 29 additions & 35 deletions solution/1300-1399/1392.Longest Happy Prefix/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,18 +81,17 @@ class Solution {

public String longestPrefix(String s) {
int base = 131;
int n = 100010;
p = new long[n];
h = new long[n];
int n = s.length();
p = new long[n + 10];
h = new long[n + 10];
p[0] = 1;
for (int i = 1; i <= s.length(); ++i) {
p[i] = p[i - 1] * base;
h[i] = h[i - 1] * base + s.charAt(i - 1);
for (int i = 0; i < n; ++i) {
p[i + 1] = p[i] * base;
h[i + 1] = h[i] * base + s.charAt(i);
}
int l = s.length();
for (int i = l - 1; i > 0; --i) {
if (get(1, i) == get(l - i + 1, l)) {
return s.substring(0, i);
for (int l = n - 1; l > 0; --l) {
if (get(1, l) == get(n - l + 1, n)) {
return s.substring(0, l);
}
}
return "";
Expand All @@ -108,29 +107,26 @@ class Solution {

```cpp
typedef unsigned long long ULL;

class Solution {
public:
string longestPrefix(string s) {
int base = 131;
int n = 100010;
ULL p[100010];
int n = s.size();
ULL p[n + 10];
ULL h[n + 10];
p[0] = 1;
ULL h[100010];
h[0] = 0;
for (int i = 1; i <= s.size(); i++)
for (int i = 0; i < n; ++i)
{
p[i] = p[i - 1] * base;
h[i] = h[i - 1] * base + s[i - 1];
p[i + 1] = p[i] * base;
h[i + 1] = h[i] * base + s[i];
}
int l = s.size();
for (int i = l - 1; i >= 1; i--)
for (int l = n - 1; l > 0; --l)
{
ULL prefix = h[i];
ULL suffix = h[l] - h[l - i] * p[i];
if (prefix == suffix)
{
return s.substr(0, i);
}
ULL prefix = h[l];
ULL suffix = h[n] - h[n - l] * p[l];
if (prefix == suffix) return s.substr(0, l);
}
return "";
}
Expand All @@ -142,20 +138,18 @@ public:
```go
func longestPrefix(s string) string {
base := 131
n := 100010
p := make([]int, n)
h := make([]int, n)
n := len(s)
p := make([]int, n+10)
h := make([]int, n+10)
p[0] = 1
for i := 1; i <= len(s); i++ {
p[i] = p[i-1] * base
h[i] = h[i-1]*base + int(s[i-1])
for i, c := range s {
p[i+1] = p[i] * base
h[i+1] = h[i]*base + int(c)
}
l := len(s)
for i := l - 1; i > 0; i-- {
prefix := h[i]
suffix := h[l] - h[l-i]*p[i]
for l := n - 1; l > 0; l-- {
prefix, suffix := h[l], h[n]-h[n-l]*p[l]
if prefix == suffix {
return s[:i]
return s[:l]
}
}
return ""
Expand Down
64 changes: 29 additions & 35 deletions solution/1300-1399/1392.Longest Happy Prefix/README_EN.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,18 +73,17 @@ class Solution {

public String longestPrefix(String s) {
int base = 131;
int n = 100010;
p = new long[n];
h = new long[n];
int n = s.length();
p = new long[n + 10];
h = new long[n + 10];
p[0] = 1;
for (int i = 1; i <= s.length(); ++i) {
p[i] = p[i - 1] * base;
h[i] = h[i - 1] * base + s.charAt(i - 1);
for (int i = 0; i < n; ++i) {
p[i + 1] = p[i] * base;
h[i + 1] = h[i] * base + s.charAt(i);
}
int l = s.length();
for (int i = l - 1; i > 0; --i) {
if (get(1, i) == get(l - i + 1, l)) {
return s.substring(0, i);
for (int l = n - 1; l > 0; --l) {
if (get(1, l) == get(n - l + 1, n)) {
return s.substring(0, l);
}
}
return "";
Expand All @@ -100,29 +99,26 @@ class Solution {

```cpp
typedef unsigned long long ULL;

class Solution {
public:
string longestPrefix(string s) {
int base = 131;
int n = 100010;
ULL p[100010];
int n = s.size();
ULL p[n + 10];
ULL h[n + 10];
p[0] = 1;
ULL h[100010];
h[0] = 0;
for (int i = 1; i <= s.size(); i++)
for (int i = 0; i < n; ++i)
{
p[i] = p[i - 1] * base;
h[i] = h[i - 1] * base + s[i - 1];
p[i + 1] = p[i] * base;
h[i + 1] = h[i] * base + s[i];
}
int l = s.size();
for (int i = l - 1; i >= 1; i--)
for (int l = n - 1; l > 0; --l)
{
ULL prefix = h[i];
ULL suffix = h[l] - h[l - i] * p[i];
if (prefix == suffix)
{
return s.substr(0, i);
}
ULL prefix = h[l];
ULL suffix = h[n] - h[n - l] * p[l];
if (prefix == suffix) return s.substr(0, l);
}
return "";
}
Expand All @@ -134,20 +130,18 @@ public:
```go
func longestPrefix(s string) string {
base := 131
n := 100010
p := make([]int, n)
h := make([]int, n)
n := len(s)
p := make([]int, n+10)
h := make([]int, n+10)
p[0] = 1
for i := 1; i <= len(s); i++ {
p[i] = p[i-1] * base
h[i] = h[i-1]*base + int(s[i-1])
for i, c := range s {
p[i+1] = p[i] * base
h[i+1] = h[i]*base + int(c)
}
l := len(s)
for i := l - 1; i > 0; i-- {
prefix := h[i]
suffix := h[l] - h[l-i]*p[i]
for l := n - 1; l > 0; l-- {
prefix, suffix := h[l], h[n]-h[n-l]*p[l]
if prefix == suffix {
return s[:i]
return s[:l]
}
}
return ""
Expand Down
25 changes: 11 additions & 14 deletions solution/1300-1399/1392.Longest Happy Prefix/Solution.cpp
Original file line number Diff line number Diff line change
@@ -1,27 +1,24 @@
typedef unsigned long long ULL;

class Solution {
public:
string longestPrefix(string s) {
int base = 131;
int n = 100010;
ULL p[100010];
int n = s.size();
ULL p[n + 10];
ULL h[n + 10];
p[0] = 1;
ULL h[100010];
h[0] = 0;
for (int i = 1; i <= s.size(); i++)
for (int i = 0; i < n; ++i)
{
p[i] = p[i - 1] * base;
h[i] = h[i - 1] * base + s[i - 1];
p[i + 1] = p[i] * base;
h[i + 1] = h[i] * base + s[i];
}
int l = s.size();
for (int i = l - 1; i >= 1; i--)
for (int l = n - 1; l > 0; --l)
{
ULL prefix = h[i];
ULL suffix = h[l] - h[l - i] * p[i];
if (prefix == suffix)
{
return s.substr(0, i);
}
ULL prefix = h[l];
ULL suffix = h[n] - h[n - l] * p[l];
if (prefix == suffix) return s.substr(0, l);
}
return "";
}
Expand Down
20 changes: 9 additions & 11 deletions solution/1300-1399/1392.Longest Happy Prefix/Solution.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
func longestPrefix(s string) string {
base := 131
n := 100010
p := make([]int, n)
h := make([]int, n)
n := len(s)
p := make([]int, n+10)
h := make([]int, n+10)
p[0] = 1
for i := 1; i <= len(s); i++ {
p[i] = p[i-1] * base
h[i] = h[i-1]*base + int(s[i-1])
for i, c := range s {
p[i+1] = p[i] * base
h[i+1] = h[i]*base + int(c)
}
l := len(s)
for i := l - 1; i > 0; i-- {
prefix := h[i]
suffix := h[l] - h[l-i]*p[i]
for l := n - 1; l > 0; l-- {
prefix, suffix := h[l], h[n]-h[n-l]*p[l]
if prefix == suffix {
return s[:i]
return s[:l]
}
}
return ""
Expand Down
Loading

0 comments on commit 770b85e

Please sign in to comment.