From 0caeb2e5b5db06a4caf547c2ea3144a37b578e5d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=BD=A9=E8=BE=95=E9=BE=99=E5=84=BF?= Date: Thu, 2 Mar 2023 14:48:07 +0800 Subject: [PATCH] =?UTF-8?q?809:=E6=83=85=E6=84=9F=E4=B8=B0=E5=AF=8C?= =?UTF-8?q?=E7=9A=84=E6=96=87=E5=AD=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../leetcode/editor/cn/ExpressiveWords.java | 85 +++++++++++++++++++ .../editor/cn/doc/content/ExpressiveWords.md | 33 +++++++ 2 files changed, 118 insertions(+) create mode 100644 src/main/java/leetcode/editor/cn/ExpressiveWords.java create mode 100644 src/main/java/leetcode/editor/cn/doc/content/ExpressiveWords.md diff --git a/src/main/java/leetcode/editor/cn/ExpressiveWords.java b/src/main/java/leetcode/editor/cn/ExpressiveWords.java new file mode 100644 index 0000000..0850c7e --- /dev/null +++ b/src/main/java/leetcode/editor/cn/ExpressiveWords.java @@ -0,0 +1,85 @@ +//

有时候人们会用重复写一些字母来表示额外的感受,比如 "hello" -> "heeellooo", "hi" -> "hiii"。我们将相邻字母都相同的一串字符定义为相同字母组,例如:"h", "eee", "ll", "ooo"。

+// +//

对于一个给定的字符串 S ,如果另一个单词能够通过将一些字母组扩张从而使其和 S 相同,我们将这个单词定义为可扩张的(stretchy)。扩张操作定义如下:选择一个字母组(包含字母 c ),然后往其中添加相同的字母 c 使其长度达到 3 或以上。

+// +//

例如,以 "hello" 为例,我们可以对字母组 "o" 扩张得到 "hellooo",但是无法以同样的方法得到 "helloo" 因为字母组 "oo" 长度小于 3。此外,我们可以进行另一种扩张 "ll" -> "lllll" 以获得 "helllllooo"。如果 s = "helllllooo",那么查询词 "hello" 是可扩张的,因为可以对它执行这两种扩张操作使得 query = "hello" -> "hellooo" -> "helllllooo" = s

+// +//

输入一组查询单词,输出其中可扩张的单词数量。

+// +//

 

+// +//

示例:

+// +//
+//输入: 
+//s = "heeellooo"
+//words = ["hello", "hi", "helo"]
+//输出:1
+//解释:
+//我们能通过扩张 "hello" 的 "e" 和 "o" 来得到 "heeellooo"。
+//我们不能通过扩张 "helo" 来得到 "heeellooo" 因为 "ll" 的长度小于 3 。
+//
+// +//

 

+// +//

提示:

+// +// +// +//
Related Topics
  • 数组
  • 双指针
  • 字符串

  • 👍 122
  • 👎 0
  • +package leetcode.editor.cn; + +import java.util.Arrays; +import java.util.LinkedList; +import java.util.Queue; + +// 809:情感丰富的文字 +public class ExpressiveWords { + public static void main(String[] args) { + Solution solution = new ExpressiveWords().new Solution(); + // TO TEST + solution.expressiveWords("abcd", new String[]{"abc"}); + } + + //leetcode submit region begin(Prohibit modification and deletion) + class Solution { + public int expressiveWords(String s, String[] words) { + Queue queue = new LinkedList<>(); + queue.addAll(Arrays.asList(words)); + int cnt = 0; + while (!queue.isEmpty() && s.length() > 0) { + int size = queue.size(); + char ch = s.charAt(0); + s = s.substring(1); + int num = 1; + while (s.startsWith("" + ch + ch)) { + while (s.startsWith("" + ch)) { + s = s.substring(1); + num++; + } + } + for (int i = 0; i < size; i++) { + String str = queue.poll(); + int num1 = num; + while (str.startsWith("" + ch) && num1 > 0) { + str = str.substring(1); + num1--; + } + if (num1 < num && str.length() > 0) { + queue.add(str); + } + if (s.equals("") && str.equals("")) { + cnt++; + } + } + } + return cnt; + } + } +//leetcode submit region end(Prohibit modification and deletion) + +} diff --git a/src/main/java/leetcode/editor/cn/doc/content/ExpressiveWords.md b/src/main/java/leetcode/editor/cn/doc/content/ExpressiveWords.md new file mode 100644 index 0000000..2b94759 --- /dev/null +++ b/src/main/java/leetcode/editor/cn/doc/content/ExpressiveWords.md @@ -0,0 +1,33 @@ +

    有时候人们会用重复写一些字母来表示额外的感受,比如 "hello" -> "heeellooo", "hi" -> "hiii"。我们将相邻字母都相同的一串字符定义为相同字母组,例如:"h", "eee", "ll", "ooo"。

    + +

    对于一个给定的字符串 S ,如果另一个单词能够通过将一些字母组扩张从而使其和 S 相同,我们将这个单词定义为可扩张的(stretchy)。扩张操作定义如下:选择一个字母组(包含字母 c ),然后往其中添加相同的字母 c 使其长度达到 3 或以上。

    + +

    例如,以 "hello" 为例,我们可以对字母组 "o" 扩张得到 "hellooo",但是无法以同样的方法得到 "helloo" 因为字母组 "oo" 长度小于 3。此外,我们可以进行另一种扩张 "ll" -> "lllll" 以获得 "helllllooo"。如果 s = "helllllooo",那么查询词 "hello" 是可扩张的,因为可以对它执行这两种扩张操作使得 query = "hello" -> "hellooo" -> "helllllooo" = s

    + +

    输入一组查询单词,输出其中可扩张的单词数量。

    + +

     

    + +

    示例:

    + +
    +输入: 
    +s = "heeellooo"
    +words = ["hello", "hi", "helo"]
    +输出:1
    +解释:
    +我们能通过扩张 "hello" 的 "e" 和 "o" 来得到 "heeellooo"。
    +我们不能通过扩张 "helo" 来得到 "heeellooo" 因为 "ll" 的长度小于 3 。
    +
    + +

     

    + +

    提示:

    + + + +
    Related Topics
  • 数组
  • 双指针
  • 字符串

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