-
-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add solutions to lc problem: No.0030
No.0030.Substring with Concatenation of All Words
- Loading branch information
Showing
10 changed files
with
364 additions
and
429 deletions.
There are no files selected for viewing
267 changes: 120 additions & 147 deletions
267
solution/0000-0099/0030.Substring with Concatenation of All Words/README.md
Large diffs are not rendered by default.
Oops, something went wrong.
267 changes: 120 additions & 147 deletions
267
solution/0000-0099/0030.Substring with Concatenation of All Words/README_EN.md
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 17 additions & 14 deletions
31
solution/0000-0099/0030.Substring with Concatenation of All Words/Solution.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,31 +1,34 @@ | ||
func findSubstring(s string, words []string) (ans []int) { | ||
cnt := map[string]int{} | ||
cnt := make(map[string]int) | ||
for _, w := range words { | ||
cnt[w]++ | ||
} | ||
m, n, k := len(s), len(words), len(words[0]) | ||
for i := 0; i < k; i++ { | ||
cnt1 := map[string]int{} | ||
l, r, t := i, i, 0 | ||
l, r := i, i | ||
cnt1 := make(map[string]int) | ||
for r+k <= m { | ||
w := s[r : r+k] | ||
t := s[r : r+k] | ||
r += k | ||
if _, ok := cnt[w]; !ok { | ||
l, t = r, 0 | ||
cnt1 = map[string]int{} | ||
|
||
if _, exists := cnt[t]; !exists { | ||
cnt1 = make(map[string]int) | ||
l = r | ||
continue | ||
} | ||
cnt1[w]++ | ||
t++ | ||
for cnt1[w] > cnt[w] { | ||
cnt1[s[l:l+k]]-- | ||
cnt1[t]++ | ||
for cnt1[t] > cnt[t] { | ||
w := s[l : l+k] | ||
cnt1[w]-- | ||
if cnt1[w] == 0 { | ||
delete(cnt1, w) | ||
} | ||
l += k | ||
t-- | ||
} | ||
if t == n { | ||
if r-l == n*k { | ||
ans = append(ans, l) | ||
} | ||
} | ||
} | ||
return | ||
} | ||
} |
29 changes: 13 additions & 16 deletions
29
solution/0000-0099/0030.Substring with Concatenation of All Words/Solution.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,38 +1,35 @@ | ||
class Solution { | ||
public List<Integer> findSubstring(String s, String[] words) { | ||
Map<String, Integer> cnt = new HashMap<>(); | ||
for (String w : words) { | ||
for (var w : words) { | ||
cnt.merge(w, 1, Integer::sum); | ||
} | ||
int m = s.length(), n = words.length; | ||
int k = words[0].length(); | ||
List<Integer> ans = new ArrayList<>(); | ||
int m = s.length(), n = words.length, k = words[0].length(); | ||
for (int i = 0; i < k; ++i) { | ||
Map<String, Integer> cnt1 = new HashMap<>(); | ||
int l = i, r = i; | ||
int t = 0; | ||
Map<String, Integer> cnt1 = new HashMap<>(); | ||
while (r + k <= m) { | ||
String w = s.substring(r, r + k); | ||
var t = s.substring(r, r + k); | ||
r += k; | ||
if (!cnt.containsKey(w)) { | ||
if (!cnt.containsKey(t)) { | ||
cnt1.clear(); | ||
l = r; | ||
t = 0; | ||
continue; | ||
} | ||
cnt1.merge(w, 1, Integer::sum); | ||
++t; | ||
while (cnt1.get(w) > cnt.get(w)) { | ||
String remove = s.substring(l, l + k); | ||
cnt1.merge(t, 1, Integer::sum); | ||
while (cnt1.get(t) > cnt.get(t)) { | ||
String w = s.substring(l, l + k); | ||
if (cnt1.merge(w, -1, Integer::sum) == 0) { | ||
cnt1.remove(w); | ||
} | ||
l += k; | ||
cnt1.merge(remove, -1, Integer::sum); | ||
--t; | ||
} | ||
if (t == n) { | ||
if (r - l == n * k) { | ||
ans.add(l); | ||
} | ||
} | ||
} | ||
return ans; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.