From d5c83666523145073b13806878d9abafc5f59778 Mon Sep 17 00:00:00 2001 From: 0xff-dev Date: Mon, 21 Oct 2024 09:06:00 +0800 Subject: [PATCH] Add solution and test-cases for problem 1593 --- .../README.md | 33 +++++++++++-------- .../Solution.go | 22 +++++++++++-- .../Solution_test.go | 14 ++++---- 3 files changed, 47 insertions(+), 22 deletions(-) diff --git a/leetcode/1501-1600/1593.Split-a-String-Into-the-Max-Number-of-Unique-Substrings/README.md b/leetcode/1501-1600/1593.Split-a-String-Into-the-Max-Number-of-Unique-Substrings/README.md index f81157e5a..d81a94b05 100755 --- a/leetcode/1501-1600/1593.Split-a-String-Into-the-Max-Number-of-Unique-Substrings/README.md +++ b/leetcode/1501-1600/1593.Split-a-String-Into-the-Max-Number-of-Unique-Substrings/README.md @@ -1,28 +1,35 @@ # [1593.Split a String Into the Max Number of Unique Substrings][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 +Given a string `s`, return the maximum number of unique substrings that the given string can be split into. + +You can split string `s` into any list of **non-empty substrings**, where the concatenation of the substrings forms the original string. However, you must split the substrings such that all of them are **unique**. + +A **substring** is a contiguous sequence of characters within a string. **Example 1:** ``` -Input: a = "11", b = "1" -Output: "100" +Input: s = "ababccc" +Output: 5 +Explanation: One way to split maximally is ['a', 'b', 'ab', 'c', 'cc']. Splitting like ['a', 'b', 'a', 'b', 'c', 'cc'] is not valid as you have 'a' and 'b' multiple times. ``` -## 题意 -> ... - -## 题解 +**Example 2:** -### 思路1 -> ... -Split a String Into the Max Number of Unique Substrings -```go ``` +Input: s = "aba" +Output: 2 +Explanation: One way to split maximally is ['a', 'ba']. +``` + +**Example 3:** +``` +Input: s = "aa" +Output: 1 +Explanation: It is impossible to split the string any further. +``` ## 结语 diff --git a/leetcode/1501-1600/1593.Split-a-String-Into-the-Max-Number-of-Unique-Substrings/Solution.go b/leetcode/1501-1600/1593.Split-a-String-Into-the-Max-Number-of-Unique-Substrings/Solution.go index d115ccf5e..e6661aae2 100644 --- a/leetcode/1501-1600/1593.Split-a-String-Into-the-Max-Number-of-Unique-Substrings/Solution.go +++ b/leetcode/1501-1600/1593.Split-a-String-Into-the-Max-Number-of-Unique-Substrings/Solution.go @@ -1,5 +1,23 @@ package Solution -func Solution(x bool) bool { - return x +func Solution(s string) int { + used := make(map[string]struct{}) + var dfs func(int) int + dfs = func(index int) int { + if index >= len(s) { + return 0 + } + m := 0 + for next := index + 1; next <= len(s); next++ { + cur := s[index:next] + if _, ok := used[cur]; ok { + continue + } + used[cur] = struct{}{} + m = max(m, dfs(next)+1) + delete(used, cur) + } + return m + } + return dfs(0) } diff --git a/leetcode/1501-1600/1593.Split-a-String-Into-the-Max-Number-of-Unique-Substrings/Solution_test.go b/leetcode/1501-1600/1593.Split-a-String-Into-the-Max-Number-of-Unique-Substrings/Solution_test.go index 14ff50eb4..acaf57614 100644 --- a/leetcode/1501-1600/1593.Split-a-String-Into-the-Max-Number-of-Unique-Substrings/Solution_test.go +++ b/leetcode/1501-1600/1593.Split-a-String-Into-the-Max-Number-of-Unique-Substrings/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", "ababccc", 5}, + {"TestCase2", "aba", 2}, + {"TestCase3", "aa", 1}, } // 开始测试 @@ -30,10 +30,10 @@ func TestSolution(t *testing.T) { } } -// 压力测试 +// 压力测试 func BenchmarkSolution(b *testing.B) { } -// 使用案列 +// 使用案列 func ExampleSolution() { }