-
Notifications
You must be signed in to change notification settings - Fork 186
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #658 from 0xff-dev/1358
Add solution and test-cases for problem 1358
- Loading branch information
Showing
3 changed files
with
106 additions
and
22 deletions.
There are no files selected for viewing
30 changes: 17 additions & 13 deletions
30
...e/1301-1400/1358.Number-of-Substrings-Containing-All-Three-Characters/README.md
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
79 changes: 77 additions & 2 deletions
79
leetcode/1301-1400/1358.Number-of-Substrings-Containing-All-Three-Characters/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,5 +1,80 @@ | ||
package Solution | ||
|
||
func Solution(x bool) bool { | ||
return x | ||
import "sort" | ||
|
||
func Solution(s string) int { | ||
need := [3][]int{} | ||
for i := 0; i < 3; i++ { | ||
need[i] = make([]int, 0) | ||
} | ||
ans := 0 | ||
for i := len(s) - 1; i >= 0; i-- { | ||
need[s[i]-'a'] = append(need[s[i]-'a'], i) | ||
} | ||
for end := len(s) - 1; end > 1; end-- { | ||
if s[end] == 'a' { | ||
i1 := sort.Search(len(need[1]), func(i int) bool { | ||
return need[1][i] < end | ||
}) | ||
i2 := sort.Search(len(need[2]), func(i int) bool { | ||
return need[2][i] < end | ||
}) | ||
if i1 != len(need[1]) && i2 != len(need[2]) { | ||
x := need[1][i1] | ||
if need[2][i2] < x { | ||
x = need[2][i2] | ||
} | ||
ans += x + 1 | ||
} | ||
} | ||
if s[end] == 'b' { | ||
i1 := sort.Search(len(need[0]), func(i int) bool { | ||
return need[0][i] < end | ||
}) | ||
i2 := sort.Search(len(need[2]), func(i int) bool { | ||
return need[2][i] < end | ||
}) | ||
if i1 != len(need[0]) && i2 != len(need[2]) { | ||
x := need[0][i1] | ||
if need[2][i2] < x { | ||
x = need[2][i2] | ||
} | ||
ans += x + 1 | ||
} | ||
|
||
} | ||
if s[end] == 'c' { | ||
i1 := sort.Search(len(need[0]), func(i int) bool { | ||
return need[0][i] < end | ||
}) | ||
i2 := sort.Search(len(need[1]), func(i int) bool { | ||
return need[1][i] < end | ||
}) | ||
if i1 != len(need[0]) && i2 != len(need[1]) { | ||
x := need[0][i1] | ||
if need[1][i2] < x { | ||
x = need[1][i2] | ||
} | ||
ans += x + 1 | ||
} | ||
} | ||
} | ||
return ans | ||
} | ||
|
||
func Solution1(s string) int { | ||
ans := 0 | ||
|
||
chars := [3]int{} | ||
start := 0 | ||
x := len(s) | ||
for i := 0; i < x; i++ { | ||
chars[s[i]-'a']++ | ||
for chars[0] > 0 && chars[1] > 0 && chars[2] > 0 { | ||
ans += x - i | ||
chars[s[start]-'a']-- | ||
start++ | ||
} | ||
} | ||
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