From 450095cdddc5e84572acafc810c06ed27950b318 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=BD=A9=E8=BE=95=E9=BE=99=E5=84=BF?= Date: Sat, 8 Apr 2023 22:20:05 +0800 Subject: [PATCH] =?UTF-8?q?1239:=E4=B8=B2=E8=81=94=E5=AD=97=E7=AC=A6?= =?UTF-8?q?=E4=B8=B2=E7=9A=84=E6=9C=80=E5=A4=A7=E9=95=BF=E5=BA=A6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...oncatenatedStringWithUniqueCharacters.java | 94 +++++++++++++++++++ ...AConcatenatedStringWithUniqueCharacters.md | 49 ++++++++++ 2 files changed, 143 insertions(+) create mode 100644 src/main/java/leetcode/editor/cn/MaximumLengthOfAConcatenatedStringWithUniqueCharacters.java create mode 100644 src/main/java/leetcode/editor/cn/doc/content/MaximumLengthOfAConcatenatedStringWithUniqueCharacters.md diff --git a/src/main/java/leetcode/editor/cn/MaximumLengthOfAConcatenatedStringWithUniqueCharacters.java b/src/main/java/leetcode/editor/cn/MaximumLengthOfAConcatenatedStringWithUniqueCharacters.java new file mode 100644 index 0000000..dc16152 --- /dev/null +++ b/src/main/java/leetcode/editor/cn/MaximumLengthOfAConcatenatedStringWithUniqueCharacters.java @@ -0,0 +1,94 @@ +////给定一个字符串数组 arr,字符串 s 是将 arr 的含有 不同字母 的 子序列 字符串 连接 所得的字符串。 +// +// 请返回所有可行解 s 中最长长度。 +// +// 子序列 是一种可以从另一个数组派生而来的数组,通过删除某些元素或不删除元素而不改变其余元素的顺序。 +// +// +// +// 示例 1: +// +// +//输入:arr = ["un","iq","ue"] +//输出:4 +//解释:所有可能的串联组合是: +//- "" +//- "un" +//- "iq" +//- "ue" +//- "uniq" ("un" + "iq") +//- "ique" ("iq" + "ue") +//最大长度为 4。 +// +// +// 示例 2: +// +// +//输入:arr = ["cha","r","act","ers"] +//输出:6 +//解释:可能的解答有 "chaers" 和 "acters"。 +// +// +// 示例 3: +// +// +//输入:arr = ["abcdefghijklmnopqrstuvwxyz"] +//输出:26 +// +// +// +// +// 提示: +// +// +// 1 <= arr.length <= 16 +// 1 <= arr[i].length <= 26 +// arr[i] 中只含有小写英文字母 +// +// +// Related Topics 位运算 数组 字符串 回溯 👍 217 👎 0 + + +package leetcode.editor.cn; + +import java.util.HashSet; +import java.util.List; +import java.util.Set; + +// 1239:串联字符串的最大长度 +public class MaximumLengthOfAConcatenatedStringWithUniqueCharacters { + public static void main(String[] args) { + Solution solution = new MaximumLengthOfAConcatenatedStringWithUniqueCharacters().new Solution(); + } + + //leetcode submit region begin(Prohibit modification and deletion) + class Solution { + StringBuilder sb = new StringBuilder(); + int res = 0; + + public int maxLength(List arr) { + dfs(arr, 0); + return res; + } + + public void dfs(List arr, int start) { + char[] ch = sb.toString().toCharArray(); + Set set = new HashSet<>(); + for (char c : ch) { + if (set.contains(c)) { + return; + } else { + set.add(c); + } + } + res = Math.max(res, sb.length()); + for (int i = start; i < arr.size(); i++) { + sb.append(arr.get(i)); + dfs(arr, i + 1); + sb.delete(sb.length() - arr.get(i).length(), sb.length()); + } + } + } +//leetcode submit region end(Prohibit modification and deletion) + +} \ No newline at end of file diff --git a/src/main/java/leetcode/editor/cn/doc/content/MaximumLengthOfAConcatenatedStringWithUniqueCharacters.md b/src/main/java/leetcode/editor/cn/doc/content/MaximumLengthOfAConcatenatedStringWithUniqueCharacters.md new file mode 100644 index 0000000..e793861 --- /dev/null +++ b/src/main/java/leetcode/editor/cn/doc/content/MaximumLengthOfAConcatenatedStringWithUniqueCharacters.md @@ -0,0 +1,49 @@ +

给定一个字符串数组 arr,字符串 s 是将 arr 的含有 不同字母 的 子序列 字符串 连接 所得的字符串。

+ +

请返回所有可行解 s 中最长长度。

+ +

子序列 是一种可以从另一个数组派生而来的数组,通过删除某些元素或不删除元素而不改变其余元素的顺序。

+ +

 

+ +

示例 1:

+ +
+输入:arr = ["un","iq","ue"]
+输出:4
+解释:所有可能的串联组合是:
+- ""
+- "un"
+- "iq"
+- "ue"
+- "uniq" ("un" + "iq")
+- "ique" ("iq" + "ue")
+最大长度为 4。
+
+ +

示例 2:

+ +
+输入:arr = ["cha","r","act","ers"]
+输出:6
+解释:可能的解答有 "chaers" 和 "acters"。
+
+ +

示例 3:

+ +
+输入:arr = ["abcdefghijklmnopqrstuvwxyz"]
+输出:26
+
+ +

 

+ +

提示:

+ +
    +
  • 1 <= arr.length <= 16
  • +
  • 1 <= arr[i].length <= 26
  • +
  • arr[i] 中只含有小写英文字母
  • +
+ +
Related Topics
  • 位运算
  • 数组
  • 字符串
  • 回溯

  • 👍 217
  • 👎 0
  • \ No newline at end of file