File tree Expand file tree Collapse file tree 3 files changed +115
-2
lines changed
647palindromic-substrings
76minimum-window-substring Expand file tree Collapse file tree 3 files changed +115
-2
lines changed Original file line number Diff line number Diff line change 1+ package main
2+
3+ func main () {
4+
5+ }
Original file line number Diff line number Diff line change 1+ package main
2+
3+ import "fmt"
4+
5+ func countSubstrings (s string ) int {
6+ var res int
7+ for i := 0 ; i < len (s ); i ++ {
8+ for j := i + 1 ; j <= len (s ); j ++ {
9+ if isPalindrome (s [i :j ]) {
10+ res ++
11+ }
12+ }
13+ }
14+ return res
15+ }
16+
17+ func countSubstrings1 (s string ) int {
18+
19+ var res int
20+ flag := make ([][]bool ,len (s ))
21+ for i := range flag {
22+ flag [i ]= make ([]bool ,len (s ))
23+ }
24+ for j := 0 ; j < len (s ); j ++ {
25+ for i := j ; i >= 0 ; i -- {
26+ if s [i ] == s [j ] && (j - i < 2 || flag [i + 1 ][j - 1 ]) {
27+ flag [i ][j ] = true
28+ res ++
29+ }
30+ }
31+ }
32+ return res
33+ }
34+
35+ func isPalindrome (s string ) bool {
36+ fmt .Println (s )
37+ l := 0
38+ r := len (s ) - 1
39+ for l < r {
40+ if s [l ] != s [r ] {
41+ return false
42+ }
43+ l ++
44+ r --
45+ }
46+ return true
47+ }
48+
49+ func main () {
50+ fmt .Println (countSubstrings1 ("abba" ))
51+ //fmt.Println(countSubstrings1("aaa"))
52+ }
Original file line number Diff line number Diff line change 11package main
22
3- import "fmt"
3+ import (
4+ "fmt"
5+ )
6+
7+ func minWindow1 (s string , t string ) string {
8+ need := make (map [byte ]int )
9+ for i := range t {
10+ need [t [i ]]++
11+ }
12+
13+ window := make (map [byte ]int )
14+ var left ,right ,match int
15+ var minLen = 0
16+ var resL int //窗口最小左边索引
17+ var resR int //窗口最小右边索引
18+ for right < len (s ){
19+ if _ ,ok := need [s [right ]];ok {
20+ window [s [right ]]++
21+ if window [s [right ]]== need [s [right ]] {
22+ match ++
23+ }
24+ }
25+ for match == len (need ){
26+ l := right - left + 1
27+ //fmt.Println(s[left:right+1])
28+ if minLen == 0 || l < minLen {
29+ resL = left
30+ resR = right
31+ minLen = l
32+ }
33+ if v ,ok := need [s [left ]];ok {
34+ window [s [left ]]--
35+ if window [s [left ]]< v {
36+ match --
37+ }
38+ }
39+ left ++
40+ }
41+ right ++
42+ }
43+ if minLen == 0 {
44+ return ""
45+ }
46+ return s [resL :resR + 1 ]
47+ }
48+
49+ func isContainer (strMap map [byte ]int ,m map [byte ]int )bool {
50+ for k ,v := range m {
51+ if strMap [k ]< v {
52+ return false
53+ }
54+ }
55+ return true
56+ }
57+
58+
459
560func minWindow (s string , t string ) string {
661 need := make (map [byte ]int )
@@ -39,5 +94,6 @@ func minWindow(s string, t string) string {
3994 return s [resL :resR + 1 ]
4095}
4196func main () {
42- fmt .Println (minWindow ("ADOBECODEBANC" ,"ABC" ))
97+ //fmt.Println(minWindow1("BANC","ABC"))
98+ fmt .Println (minWindow1 ("ADOBECODEBANC" ,"ABC" ))
4399}
You can’t perform that action at this time.
0 commit comments