From ad18c284482568cdab22b3940cbb44c8ba7bb9e4 Mon Sep 17 00:00:00 2001 From: "huangge1199@hotmail.com" Date: Sun, 11 Jul 2021 23:27:42 +0800 Subject: [PATCH] =?UTF-8?q?242:=E6=9C=89=E6=95=88=E7=9A=84=E5=AD=97?= =?UTF-8?q?=E6=AF=8D=E5=BC=82=E4=BD=8D=E8=AF=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/leetcode/editor/cn/ValidAnagram.java | 62 +++++++++++++++++++ .../java/leetcode/editor/cn/ValidAnagram.md | 30 +++++++++ 2 files changed, 92 insertions(+) create mode 100644 src/main/java/leetcode/editor/cn/ValidAnagram.java create mode 100644 src/main/java/leetcode/editor/cn/ValidAnagram.md diff --git a/src/main/java/leetcode/editor/cn/ValidAnagram.java b/src/main/java/leetcode/editor/cn/ValidAnagram.java new file mode 100644 index 0000000..e5112fb --- /dev/null +++ b/src/main/java/leetcode/editor/cn/ValidAnagram.java @@ -0,0 +1,62 @@ +//给定两个字符串 s 和 t ,编写一个函数来判断 t 是否是 s 的字母异位词。 +// +// +// +// 示例 1: +// +// +//输入: s = "anagram", t = "nagaram" +//输出: true +// +// +// 示例 2: +// +// +//输入: s = "rat", t = "car" +//输出: false +// +// +// +// 提示: +// +// +// 1 <= s.length, t.length <= 5 * 104 +// s 和 t 仅包含小写字母 +// +// +// +// +// 进阶: 如果输入字符串包含 unicode 字符怎么办?你能否调整你的解法来应对这种情况? +// Related Topics 哈希表 字符串 排序 +// 👍 401 👎 0 + +package leetcode.editor.cn; +//242:有效的字母异位词 +class ValidAnagram{ + public static void main(String[] args) { + //测试代码 + Solution solution = new ValidAnagram().new Solution(); + } + //力扣代码 + //leetcode submit region begin(Prohibit modification and deletion) +class Solution { + public boolean isAnagram(String s, String t) { + if(s.length()!=t.length()){ + return false; + } + int[] arr = new int[26]; + for (char ch:s.toCharArray()){ + arr[ch-'a']++; + } + for (char ch:t.toCharArray()){ + if(arr[ch-'a']<1){ + return false; + } + arr[ch-'a']--; + } + return true; + } +} +//leetcode submit region end(Prohibit modification and deletion) + +} \ No newline at end of file diff --git a/src/main/java/leetcode/editor/cn/ValidAnagram.md b/src/main/java/leetcode/editor/cn/ValidAnagram.md new file mode 100644 index 0000000..1f5e07e --- /dev/null +++ b/src/main/java/leetcode/editor/cn/ValidAnagram.md @@ -0,0 +1,30 @@ +

给定两个字符串 st ,编写一个函数来判断 t 是否是 s 的字母异位词。

+ +

 

+ +

示例 1:

+ +
+输入: s = "anagram", t = "nagaram"
+输出: true
+
+ +

示例 2:

+ +
+输入: s = "rat", t = "car"
+输出: false
+ +

 

+ +

提示:

+ + + +

 

+ +

进阶: 如果输入字符串包含 unicode 字符怎么办?你能否调整你的解法来应对这种情况?

+
Related Topics
  • 哈希表
  • 字符串
  • 排序
  • \n
  • 👍 401
  • 👎 0
  • \ No newline at end of file