返回介绍

solution / 1500-1599 / 1593.Split a String Into the Max Number of Unique Substrings / README_EN

发布于 2024-06-17 01:03:16 字数 4133 浏览 0 评论 0 收藏 0

1593. Split a String Into the Max Number of Unique Substrings

中文文档

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: 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:

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.

 

Constraints:

  • 1 <= s.length <= 16

  • s contains only lower case English letters.

Solutions

Solution 1

class Solution:
  def maxUniqueSplit(self, s: str) -> int:
    def dfs(i, t):
      if i >= len(s):
        nonlocal ans
        ans = max(ans, t)
        return
      for j in range(i + 1, len(s) + 1):
        if s[i:j] not in vis:
          vis.add(s[i:j])
          dfs(j, t + 1)
          vis.remove(s[i:j])

    vis = set()
    ans = 1
    dfs(0, 0)
    return ans
class Solution {
  private Set<String> vis = new HashSet<>();
  private int ans = 1;
  private String s;

  public int maxUniqueSplit(String s) {
    this.s = s;
    dfs(0, 0);
    return ans;
  }

  private void dfs(int i, int t) {
    if (i >= s.length()) {
      ans = Math.max(ans, t);
      return;
    }
    for (int j = i + 1; j <= s.length(); ++j) {
      String x = s.substring(i, j);
      if (vis.add(x)) {
        dfs(j, t + 1);
        vis.remove(x);
      }
    }
  }
}
class Solution {
public:
  unordered_set<string> vis;
  string s;
  int ans = 1;

  int maxUniqueSplit(string s) {
    this->s = s;
    dfs(0, 0);
    return ans;
  }

  void dfs(int i, int t) {
    if (i >= s.size()) {
      ans = max(ans, t);
      return;
    }
    for (int j = i + 1; j <= s.size(); ++j) {
      string x = s.substr(i, j - i);
      if (!vis.count(x)) {
        vis.insert(x);
        dfs(j, t + 1);
        vis.erase(x);
      }
    }
  }
};
func maxUniqueSplit(s string) int {
  ans := 1
  vis := map[string]bool{}

  var dfs func(i, t int)
  dfs = func(i, t int) {
    if i >= len(s) {
      ans = max(ans, t)
      return
    }
    for j := i + 1; j <= len(s); j++ {
      x := s[i:j]
      if !vis[x] {
        vis[x] = true
        dfs(j, t+1)
        vis[x] = false
      }
    }
  }
  dfs(0, 0)
  return ans
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文