Skip to content

Commit c314961

Browse files
committed
added problem 2000
1 parent a0fbcb6 commit c314961

File tree

3 files changed

+57
-0
lines changed

3 files changed

+57
-0
lines changed

Diff for: problems/problem2000/reverse_prefix_of_word.go

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package problem2000
2+
3+
import "strings"
4+
5+
/*
6+
Given a 0-indexed string word and a character ch,
7+
reverse the segment of word that starts at index 0 and ends at the index of the first occurrence of ch (inclusive).
8+
If the character ch does not exist in word, do nothing.
9+
For example, if word = "abcdefd" and ch = "d", then you should reverse the segment that starts at 0 and ends at 3 (inclusive).
10+
The resulting string will be "dcbaefd".
11+
Return the resulting string.
12+
*/
13+
14+
func reversePrefix(word string, ch byte) string {
15+
var end int = strings.IndexByte(word, ch)
16+
var data []byte
17+
if end == -1 {
18+
return word
19+
}
20+
data = []byte(word)
21+
for i := 0; end > i; i++ {
22+
data[i], data[end] = data[end], data[i]
23+
end--
24+
}
25+
return string(data)
26+
}

Diff for: problems/problem2000/reverse_prefix_of_word_test.go

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package problem2000
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/stretchr/testify/assert"
8+
)
9+
10+
type Result struct {
11+
Input string
12+
Char byte
13+
Expected string
14+
}
15+
16+
var Results = []Result{
17+
{"abcdefd", 'd', "dcbaefd"},
18+
{"xyxzxe", 'z', "zxyxxe"},
19+
{"abcd", 'z', "abcd"},
20+
}
21+
22+
func TestReversePrefix(t *testing.T) {
23+
assert := assert.New(t)
24+
25+
for _, res := range Results {
26+
want := res.Expected
27+
got := reversePrefix(res.Input, res.Char)
28+
assert.Equal(want, got, fmt.Sprintf("%+v", res))
29+
}
30+
}

Diff for: readme.md

+1
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,7 @@ Each problem is in it's own directory, with test files. There are helper package
270270
| 1844 | [Replace All Digits with Characters](https://leetcode.com/problems/replace-all-digits-with-characters) | [My Solution](./problems/problem1844) ||
271271
| 1926 | [Nearest Exit from Entrance in Maze](https://leetcode.com/problems/nearest-exit-from-entrance-in-maze) | [My Solution](./problems/problem1926) ||
272272
| 1996 | [The Number of Weak Characters in the Game](https://leetcode.com/problems/the-number-of-weak-characters-in-the-game) | [My Solution](./problems/problem1996) ||
273+
| 2000 | [Reverse Prefix of Word](https://leetcode.com/problems/reverse-prefix-of-word/) | [My Solution](./problems/problem2000) ||
273274
| 2007 | [Find Original Array From Doubled Array](https://leetcode.com/problems/find-original-array-from-doubled-array) | [My Solution](./problems/problem2007) ||
274275
| 2095 | [Delete the Middle Node of a Linked List](https://leetcode.com/problems/delete-the-middle-node-of-a-linked-list) | [My Solution](./problems/problem2095) ||
275276
| 2131 | [Longest Palindrome by Concatenating Two Letter Words](https://leetcode.com/problems/longest-palindrome-by-concatenating-two-letter-words) | [My Solution](./problems/problem2131) ||

0 commit comments

Comments
 (0)