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. Vue 实例成员

    Vue 一. 什么是Vue 可以独立完成前后端分离时 Web项目的JavaScript框架 二.为什么学Vue 前端三大主流框架:Angular React Vue Vue结合了其他框架优点.轻量级. ...

  2. 【贪心】Communication System POJ 1018

    题目链接:http://poj.org/problem?id=1018 题目大意:有n种通讯设备,每种有mi个制造商,bi.pi分别是带宽和价格.在每种设备中选一个制造商让最小带宽B与总价格P的比值B ...

  3. Phoenix 简单介绍

    转载自:https://blog.csdn.net/carolzhang8406/article/details/79455684 1. Phoenix定义 Phoenix最早是saleforce的一 ...

  4. IP地址与Mac地址绑定错误

    有个application,有时候可以正常访问,有时候又返回404错误,百思不得其解.刚开始以为是文件夹权限问题,折腾了好久. 后来没在服务器上monitor到包,所以猜想是到了错误的mac地址,用a ...

  5. php+ tinymce粘贴word

    最近公司做项目需要实现一个功能,在网页富文本编辑器中实现粘贴Word图文的功能. 我们在网站中使用的Web编辑器比较多,都是根据用户需求来选择的.目前还没有固定哪一个编辑器 有时候用的是UEditor ...

  6. LIO -SCSI target

    2010年底,LIO 项目获选成为新的内核态的 SCSI target,取代原有的用户态的 STGT 项目.当时有两个主要的竞争项目(LIO和SCST),都在努力将代码并入主线内核.本文将比较着两个项 ...

  7. 原创:C++实现的可排序的双向链表

    学习C++有一周了,今天用C++设计了一个双向链表,这个链表有排序功能,默认按升序排列,接受的参数可以是数字,也可以是字符串.现在把自己写的代码,分享出来.如果链表中接受的对象为Lexeme,可以用于 ...

  8. gdb tui设置默认窗口高度

    gdb -p 12999 -tui 先显示win信息(输入:info win) 显示如下: SRC (35 lines) <has focus> CMD (17 lines) 我们要改的是 ...

  9. PHP chmod() 函数

    chmod() 函数改变文件模式. 如果成功则返回 TRUE,否则返回 FALSE. 例子 <?php // 所有者可读写,其他人没有任何权限 chmod(); // 所有者可读写,其他人可读 ...

  10. JDBC 操作

    简单的 JDBC 操作主要有: JdbcTemplate query queryForObject queryForList update execute 简单使用如下所示. 初始化数据库 sprin ...