diff --git a/leetcode/901-1000/0942.DI-String-Match/README.md b/leetcode/901-1000/0942.DI-String-Match/README.md index f9edc5f1d..300df83db 100644 --- a/leetcode/901-1000/0942.DI-String-Match/README.md +++ b/leetcode/901-1000/0942.DI-String-Match/README.md @@ -1,28 +1,33 @@ # [942.DI String Match][title] -> [!WARNING|style:flat] -> This question is temporarily unanswered if you have good ideas. Welcome to [Create Pull Request PR](https://github.com/kylesliu/awesome-golang-algorithm) - ## Description +A permutation `perm` of `n + 1` integers of all the integers in the range `[0, n]` can be represented as a string `s` of length `n` where: + +- `s[i] == 'I'` if `perm[i] < perm[i + 1]`, and +- `s[i] == 'D'` if `perm[i] > perm[i + 1]`. + +Given a string `s`, reconstruct the permutation `perm` and return it. If there are multiple valid permutations perm, return **any of them**. **Example 1:** ``` -Input: a = "11", b = "1" -Output: "100" +Input: s = "IDID" +Output: [0,4,1,3,2] ``` -## 题意 -> ... - -## 题解 +**Example 2:** -### 思路1 -> ... -DI String Match -```go ``` +Input: s = "III" +Output: [0,1,2,3] +``` + +**Example 3:** +``` +Input: s = "DDI" +Output: [3,2,0,1] +``` ## 结语 diff --git a/leetcode/901-1000/0942.DI-String-Match/Solution.go b/leetcode/901-1000/0942.DI-String-Match/Solution.go index d115ccf5e..ae2bfb7c5 100644 --- a/leetcode/901-1000/0942.DI-String-Match/Solution.go +++ b/leetcode/901-1000/0942.DI-String-Match/Solution.go @@ -1,5 +1,28 @@ package Solution -func Solution(x bool) bool { - return x +func Solution(s string) []int { + res := make([]int, len(s)+1) + for i := range res { + res[i] = -1 + } + + index := 0 + for i := 0; i < len(s); i++ { + if s[i] == 'D' { + continue + } + res[i] = index + index++ + for pre := i - 1; pre >= 0 && res[pre] == -1; pre-- { + res[pre] = index + index++ + } + } + res[len(s)] = index + index++ + for pre := len(s) - 1; pre >= 0 && res[pre] == -1; pre-- { + res[pre] = index + index++ + } + return res } diff --git a/leetcode/901-1000/0942.DI-String-Match/Solution_test.go b/leetcode/901-1000/0942.DI-String-Match/Solution_test.go index 14ff50eb4..ec9d3d95e 100644 --- a/leetcode/901-1000/0942.DI-String-Match/Solution_test.go +++ b/leetcode/901-1000/0942.DI-String-Match/Solution_test.go @@ -10,12 +10,12 @@ func TestSolution(t *testing.T) { // 测试用例 cases := []struct { name string - inputs bool - expect bool + inputs string + expect []int }{ - {"TestCase", true, true}, - {"TestCase", true, true}, - {"TestCase", false, false}, + {"TestCase1", "IDID", []int{0, 2, 1, 4, 3}}, + {"TestCase2", "III", []int{0, 1, 2, 3}}, + {"TestCase3", "DDI", []int{2, 1, 0, 3}}, } // 开始测试 @@ -30,10 +30,10 @@ func TestSolution(t *testing.T) { } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }