LeetCode_242. Valid Anagram
242. Valid Anagram
Given two strings s and t , write a function to determine if t is an anagram of s.
Example 1:
Input: s = "anagram", t = "nagaram"
Output: true
Example 2:
Input: s = "rat", t = "car"
Output: false
Note:
You may assume the string contains only lowercase alphabets.
Follow up:
What if the inputs contain unicode characters? How would you adapt your solution to such case?
package leetcode.easy;
public class ValidAnagram {
public boolean isAnagram1(String s, String t) {
if (s.length() != t.length()) {
return false;
}
char[] str1 = s.toCharArray();
char[] str2 = t.toCharArray();
java.util.Arrays.sort(str1);
java.util.Arrays.sort(str2);
return java.util.Arrays.equals(str1, str2);
}
public boolean isAnagram21(String s, String t) {
if (s.length() != t.length()) {
return false;
}
int[] counter = new int[26];
for (int i = 0; i < s.length(); i++) {
counter[s.charAt(i) - 'a']++;
counter[t.charAt(i) - 'a']--;
}
for (int count : counter) {
if (count != 0) {
return false;
}
}
return true;
}
public boolean isAnagram22(String s, String t) {
if (s.length() != t.length()) {
return false;
}
int[] table = new int[26];
for (int i = 0; i < s.length(); i++) {
table[s.charAt(i) - 'a']++;
}
for (int i = 0; i < t.length(); i++) {
table[t.charAt(i) - 'a']--;
if (table[t.charAt(i) - 'a'] < 0) {
return false;
}
}
return true;
}
@org.junit.Test
public void test() {
String s1 = "anagram";
String t1 = "nagaram";
String s2 = "rat";
String t2 = "car";
System.out.println(isAnagram1(s1, t1));
System.out.println(isAnagram1(s2, t2));
System.out.println(isAnagram21(s1, t1));
System.out.println(isAnagram21(s2, t2));
System.out.println(isAnagram22(s1, t1));
System.out.println(isAnagram22(s2, t2));
}
}
LeetCode_242. Valid Anagram的更多相关文章
- 【09_242】Valid Anagram
Valid Anagram My Submissions Question Total Accepted: 43694 Total Submissions: 111615 Difficulty: Ea ...
- leetcode面试准备:Valid Anagram
leetcode面试准备:Valid Anagram 1 题目 Given two strings s and t, write a function to determine if t is an ...
- 242. Valid Anagram(C++)
242. Valid Anagram Given two strings s and t, write a function to determine if t is an anagram of s. ...
- 22. leetcode 242. Valid Anagram(由颠倒字母顺序而构成的字)
22. 242. Valid Anagram(由颠倒字母顺序而构成的字) Given two strings s and t, write a function to determine if t i ...
- 【LeetCode】242. Valid Anagram (2 solutions)
Valid Anagram Given two strings s and t, write a function to determine if t is an anagram of s. For ...
- [leetcode]242. Valid Anagram验证变位词
Given two strings s and t , write a function to determine if t is an anagram of s. Example 1: Input: ...
- leetcdoe Valid Anagram
题目连接 https://leetcode.com/problems/valid-anagram/ Valid Anagram Description Given two strings s and ...
- LN : leetcode 242 Valid Anagram
lc 242 Valid Anagram 242 Valid Anagram Given two strings s and t, write a function to determine if t ...
- LeetCode 242. 有效的字母异位词(Valid Anagram)
242. 有效的字母异位词 LeetCode242. Valid Anagram 题目描述 给定两个字符串 s 和 t ,编写一个函数来判断 t 是否是 s 的一个字母异位词. 示例 1: 输入: s ...
随机推荐
- 重新打包rpm包
1.安装rpmrebuild 和安装rpmbuild rpmrebuild下载链接: https://sourceforge.net/projects/rpmrebuild/files/rpmrebu ...
- Java获取视频的大小、时长
前端上传视频之后,根据上传的视频文件获取视频的大小和时长 1.获取视频时长 private String ReadVideoTime(File source) { Encoder encoder = ...
- pt
https://www.hdarea.co/torrents.php http://hdhome.org/torrents.php https://ourbits.club/torrents.php ...
- 虚拟环境安装及Hello World
学习文章引自: http://www.pythondoc.com/flask-mega-tutorial/helloworld.html 1.安装项目需要的工具包 pip install flask ...
- 洛谷 P1842 奶牛玩杂技 题解
P1842 奶牛玩杂技 题目背景 Farmer John 养了N(1<=N<=50,000)头牛,她们已经按1~N依次编上了号.FJ所不知道的是,他的所有牛都梦想着从农场逃走,去参加马戏团 ...
- flutter 踩坑小计: amap_base 地图缩放 zoom 设置无效的问题
这种问题估计也就我这种菜鸡能遇到了,因为我问了一些大佬,他们完全没遇到这类的问题. 如果你也遇到了,希望这篇文章能帮到你,倘若还不能解决你的问题,可以留言,我们共同研究. 问题:同样的插件,为什么偏偏 ...
- day25 内置常用模块(四): 模块和包
阅读目录: 模块 import from xxx import xxx 包 import from xxx import xxx from xxx import * __init__.p ...
- jquery.nicescroll.js Unable to preventDefault inside passive event listener due to target being treated as passive.
解决办法就是:https://github.com/bestjhh/Plugin 下载替换. 参考: https://github.com/bestjhh/Plugin https://blog.cs ...
- MYSQL安装报错需要.NET4.0
MySQL安装,提示需要.NET4.0 解决:下载安装.NET4.0即可 链接:https://pan.baidu.com/s/1u0e0lafBbR0QYEcXFsv9sQ 提取码:q6rr 复制这 ...
- Java初级黄金体验 其二
Java初级黄金体验 其二 引言:让我们看一下你的C盘有多少个文件和文件夹 初级 Java IO : 第一个纪念碑 小程序大致功能 让我们看一下E盘有多少个文件 上代码 最近太多的作业 代码可以无限改 ...