242. Valid Anagram

Easy

Given two strings s and , 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的更多相关文章

  1. 【09_242】Valid Anagram

    Valid Anagram My Submissions Question Total Accepted: 43694 Total Submissions: 111615 Difficulty: Ea ...

  2. leetcode面试准备:Valid Anagram

    leetcode面试准备:Valid Anagram 1 题目 Given two strings s and t, write a function to determine if t is an ...

  3. 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. ...

  4. 22. leetcode 242. Valid Anagram(由颠倒字母顺序而构成的字)

    22. 242. Valid Anagram(由颠倒字母顺序而构成的字) Given two strings s and t, write a function to determine if t i ...

  5. 【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 ...

  6. [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: ...

  7. leetcdoe Valid Anagram

    题目连接 https://leetcode.com/problems/valid-anagram/ Valid Anagram Description Given two strings s and ...

  8. 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 ...

  9. LeetCode 242. 有效的字母异位词(Valid Anagram)

    242. 有效的字母异位词 LeetCode242. Valid Anagram 题目描述 给定两个字符串 s 和 t ,编写一个函数来判断 t 是否是 s 的一个字母异位词. 示例 1: 输入: s ...

随机推荐

  1. LightOJ - 1102 - Problem Makes Problem(组合数)

    链接: https://vjudge.net/problem/LightOJ-1102 题意: As I am fond of making easier problems, I discovered ...

  2. 高级接口--OAuth2.0网页授权

    官方文档 Auth是一个开放协议,允许用户让第三方应用以安全且标准的方式获取该用户在某以网站,移动或桌面应用上存储的司名的资源(如用户个人信息,照片,视频,联系人列表),而无需将用户名和密码提供给第三 ...

  3. C# 遍历 enum 枚举

    foreach (Suit suit in (Suit[]) Enum.GetValues(typeof(Suit))) { } 注意:强制转换(Suit[])不是必需的,但确实使代码快0.5 ns.

  4. HiveQL Index 索引

    Hive只有有限的索引功能.Hive中没有普通关系型数据库中键的概念,但是还是可以对一些字段建立索引来加速某些操作.一张表的索引数据存储在另外一张表中. 通过explain命令可以查看某个查询语句是否 ...

  5. Intel 8086 CPU

    一.8086概述 Intel8086拥有四个16位的通用寄存器,也能够当作八个8位寄存器来存取,以及四个16位索引寄存器(包含了堆栈指标).资料寄存器通常由指令隐含地使用,针对暂存值需要复杂的寄存器配 ...

  6. uic

    uic user interface complieruic mainwindow.ui >> ui_mainwidow.h

  7. 《挑战30天C++入门极限》C/C++中结构体(struct)知识点强化

        C/C++中结构体(struct)知识点强化 在上一个教程中我们已经简单的阐述了什么是结构体了,为了进一部的学习结构体这一重要的知识点,我们今天来学习一下链表结构. 结构体可以看做是一种自定义 ...

  8. Jmeter 5.1实现图片上传接口测试

    背景: 项目过程中需要抓取接口进行图片上传的接口测试,所有上传功能大同小异,无非就是参数内容不同,此处记录一下,为其他上传做一些参考 1.通过fiddler抓取到的参数如下: Content-Disp ...

  9. UOJ#468. 【ZJOI2019】Minimax搜索 动态DP

    原文链接www.cnblogs.com/zhouzhendong/p/UOJ468.html 前言 毒瘤题 题解 首先,将问题稍加转化,将"等于k"转化为"小于等于k&q ...

  10. Linux下的find命令详解

    0x01 简介 find命令用来在指定目录下查找文件.任何位于参数之前的字符串都将被视为欲查找的目录名.如果使用该命令时,不设置任何参数,则find命令将在当前目录下查找子目录与文件.并且将查找到的子 ...