From 3be18188bc4e2c0a463edd083f73f8ff7e4dc108 Mon Sep 17 00:00:00 2001 From: huangge1199 Date: Mon, 7 Jun 2021 11:04:55 +0800 Subject: [PATCH] =?UTF-8?q?567:=E5=AD=97=E7=AC=A6=E4=B8=B2=E7=9A=84?= =?UTF-8?q?=E6=8E=92=E5=88=97?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../editor/cn/PermutationInString.java | 78 +++++++++++++++++++ .../leetcode/editor/cn/PermutationInString.md | 30 +++++++ 2 files changed, 108 insertions(+) create mode 100644 src/main/java/leetcode/editor/cn/PermutationInString.java create mode 100644 src/main/java/leetcode/editor/cn/PermutationInString.md diff --git a/src/main/java/leetcode/editor/cn/PermutationInString.java b/src/main/java/leetcode/editor/cn/PermutationInString.java new file mode 100644 index 0000000..22a547a --- /dev/null +++ b/src/main/java/leetcode/editor/cn/PermutationInString.java @@ -0,0 +1,78 @@ +//给定两个字符串 s1 和 s2,写一个函数来判断 s2 是否包含 s1 的排列。 +// +// 换句话说,第一个字符串的排列之一是第二个字符串的 子串 。 +// +// +// +// 示例 1: +// +// +//输入: s1 = "ab" s2 = "eidbaooo" +//输出: True +//解释: s2 包含 s1 的排列之一 ("ba"). +// +// +// 示例 2: +// +// +//输入: s1= "ab" s2 = "eidboaoo" +//输出: False +// +// +// +// +// 提示: +// +// +// 输入的字符串只包含小写字母 +// 两个字符串的长度都在 [1, 10,000] 之间 +// +// Related Topics 双指针 Sliding Window +// 👍 357 👎 0 + +package leetcode.editor.cn; + +import java.util.HashMap; +import java.util.Map; + +//567:字符串的排列 +public class PermutationInString{ + public static void main(String[] args) { + //测试代码 + Solution solution = new PermutationInString().new Solution(); + } + //力扣代码 + //leetcode submit region begin(Prohibit modification and deletion) +class Solution { + public boolean checkInclusion(String s1, String s2) { + int l1 = s1.length(); + int l2 = s2.length(); + Map m1 = new HashMap<>(l1); + if (l1 > l2) { + return false; + } + for (int i = 0; i < l1; i++) { + String ch = String.valueOf(s1.charAt(i)); + m1.put(ch, m1.get(ch) == null ? 1 : m1.get(ch) + 1); + } + int count = l2 - l1; + for (int i = 0; i <= count; i++) { + String str = s2.substring(i, i + l1); + boolean bl = true; + for (String ch : m1.keySet()) { + String temp = str.replace(ch, ""); + if (l1 - temp.length() != m1.get(ch)) { + bl = false; + break; + } + } + if (bl) { + return true; + } + } + return false; + } +} +//leetcode submit region end(Prohibit modification and deletion) + +} \ No newline at end of file diff --git a/src/main/java/leetcode/editor/cn/PermutationInString.md b/src/main/java/leetcode/editor/cn/PermutationInString.md new file mode 100644 index 0000000..455fe2e --- /dev/null +++ b/src/main/java/leetcode/editor/cn/PermutationInString.md @@ -0,0 +1,30 @@ +

给定两个字符串 s1 和 s2,写一个函数来判断 s2 是否包含 s1 的排列。

+ +

换句话说,第一个字符串的排列之一是第二个字符串的 子串

+ +

 

+ +

示例 1:

+ +
+输入: s1 = "ab" s2 = "eidbaooo"
+输出: True
+解释: s2 包含 s1 的排列之一 ("ba").
+
+ +

示例 2:

+ +
+输入: s1= "ab" s2 = "eidboaoo"
+输出: False
+
+ +

 

+ +

提示:

+ + +
Related Topics
  • 双指针
  • Sliding Window
  • \n
  • 👍 357
  • 👎 0
  • \ No newline at end of file