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 ...
随机推荐
- pt100、pt1000,和热电偶
最近温度测试这块,已使用过pt100.pt1000,和热电偶,这里进行大致的总结.1.pt系列pt100和pt1000的精度相比较,1000更加精细,毕竟他的电阻值更加大.两个电阻都是有分度表的,pt ...
- python为什么不需要重载函数
https://www.cnblogs.com/erbaodabao0611/p/7490439.html
- 什么是ARP协议?
ARP协议,全称“Address Resolution Protocol”,中文名是地址解析协议, 使用ARP协议可实现通过IP地址获得对应主机的物理地址(MAC地址). 在TCP/IP的网络环境下, ...
- Javaweb学习笔记(一)
一.javaweb学习是所需要的细节 1.发送响应头相关的方法 1).addHeader()与setHeader()都是设置HTTP协议的响应头字段,区别是addHeader()方法可以增加同名的响应 ...
- read()和write(),读和写的优化。
读和写的优化在输入数据后输出数据十分多的情况下是十分有用的,比scanf和printf也要快. 读: int read(){ ; ; char c=getchar(); '){ if(c=='-') ...
- java使用freemark生成word/pdf
目录 一. 背景 二.实现的技术选型以及遇到的坑 三.最终的效果 2.1 .doc word效果展示 2.1 .docx word效果展示 2.2 docx word转pdf效果展示 三.准备工作及代 ...
- zabbix监控线
echo mntr | nc 127.0.0.1 2181获取mntr的信息 换成conf将获得conf信息,从中找出需要监控项 conf: clientPort:客户端端口号 dataDir:数据文 ...
- java使用递归遍历文件,使用内部类过滤文件,使用匿名内部类过滤文件
public class TestFile { public static void main(String [] args) { //遍历文件夹中文件名称,若文件夹中还存有文件夹,递归读取文件夹名称 ...
- select,poll,epoll最简单的解释
从事服务端开发,少不了要接触网络编程.epoll 作为 Linux 下高性能网络服务器的必备技术至关重要,nginx.Redis.Skynet 和大部分游戏服务器都使用到这一多路复用技术. epoll ...
- angr工具的安装
一.安装 python2 或 python3: pip install angr 目前官方不再维护python2版本,所以python2装的版本偏低,且很可能没有后续更新. 如果你选择python2版 ...