Skip to content

Commit 839befe

Browse files
authored
Merge pull request #1119 from 0xff-dev/1910
Add solution for problem 1910
2 parents c1f4388 + 1b429f6 commit 839befe

File tree

2 files changed

+52
-0
lines changed

2 files changed

+52
-0
lines changed

leetcode/1901-2000/1910.Remove-All-Occurrences-of-a-Substring/Solution.go

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,31 @@ func Solution(s string, part string) string {
1919

2020
return s
2121
}
22+
23+
func Solution1(s string, part string) string {
24+
bs := []byte(s)
25+
index := -1
26+
l := len(part)
27+
lastByte := part[l-1]
28+
for i := 0; i < len(s); i++ {
29+
index++
30+
bs[index] = s[i]
31+
if s[i] != lastByte {
32+
continue
33+
}
34+
35+
if start := index - l + 1; start >= 0 {
36+
if string(bs[start:index+1]) == part {
37+
index = start - 1
38+
}
39+
}
40+
41+
}
42+
if start := index - l + 1; start >= 0 {
43+
if string(bs[start:index+1]) == part {
44+
index = start - 1
45+
}
46+
}
47+
48+
return string(bs[:index+1])
49+
}

leetcode/1901-2000/1910.Remove-All-Occurrences-of-a-Substring/Solution_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,30 @@ func TestSolution(t *testing.T) {
3030
}
3131
}
3232

33+
func TestSolution1(t *testing.T) {
34+
// 测试用例
35+
cases := []struct {
36+
name string
37+
s, p string
38+
expect string
39+
}{
40+
{"TestCase1", "daabcbaabcbc", "abc", "dab"},
41+
{"TestCase2", "axxxxyyyyb", "xy", "ab"},
42+
{"TestCase3", "eemckxmckx", "emckx", ""},
43+
}
44+
45+
// 开始测试
46+
for i, c := range cases {
47+
t.Run(c.name+" "+strconv.Itoa(i), func(t *testing.T) {
48+
got := Solution1(c.s, c.p)
49+
if !reflect.DeepEqual(got, c.expect) {
50+
t.Fatalf("expected: %v, but got: %v, with inputs: %v %v",
51+
c.expect, got, c.s, c.p)
52+
}
53+
})
54+
}
55+
}
56+
3357
// 压力测试
3458
func BenchmarkSolution(b *testing.B) {
3559
}

0 commit comments

Comments
 (0)