原文算法说明如下:

Given two strings s and t, write a function to determine if t is an anagram of s.

For example,

s = "anagram", t = "nagaram", return true.

s = "rat", t = "car", return false.

翻译:判断给定的两个字符串是否为打乱了顺序的同一个字符串

我的java实现:

import java.util.HashMap;
public class Solution {
public boolean isAnagram(String s, String t) {
if(s.length() != t.length()){
return false;
}
char[] c1 = s.toCharArray();
char[] c2 = t.toCharArray();
HashMap<Character,Integer> map1 = new HashMap<Character,Integer>();
HashMap<Character,Integer> map2 = new HashMap<Character,Integer>();
for(int i=0;i < s.length();i++){
if(!map1.containsKey(c1[i])){
map1.put(c1[i], 1);
}else{
map1.put(c1[i], map1.get(c1[i])+1);
}
if(!map2.containsKey(c2[i])){
map2.put(c2[i], 1);
}else{
map2.put(c2[i], map2.get(c2[i])+1);
}
}
if(map1.entrySet().containsAll(map2.entrySet()) && map2.entrySet().containsAll(map1.entrySet())){
return true;
}
return false;
}
}





leetcode算法之Valid Anagram的更多相关文章

  1. LeetCode算法题-Valid Anagram(Java实现)

    这是悦乐书的第198次更新,第205篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第61题(顺位题号是242).给定两个字符串s和t,写一个函数来确定t是否是s的anag ...

  2. leetcode面试准备:Valid Anagram

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

  3. LeetCode算法题-Valid Palindrome II(Java实现)

    这是悦乐书的第287次更新,第304篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第155题(顺位题号是680).给定非空字符串s,最多可以删除一个字符. 判断它是否是回 ...

  4. LeetCode算法题-Valid Perfect Square(Java实现-四种解法)

    这是悦乐书的第209次更新,第221篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第77题(顺位题号是367).给定正整数num,写一个函数,如果num是一个完美的正方形 ...

  5. LeetCode算法题-Valid Palindrome(Java实现)

    这是悦乐书的第174次更新,第176篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第33题(顺位题号是125).给定一个字符串,确定它是否是回文,只考虑字母数字字符并忽略 ...

  6. 【算法】LeetCode算法题-Valid Parentheses

    这是悦乐书的第147次更新,第149篇原创 01 看题和准备 今天介绍的是LeetCode算法题中Easy级别的第6题(顺位题号是20),给定一个只包含字符'(',')','{','}','['和'] ...

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

  8. 【LeetCode】242. Valid Anagram 解题报告(Java & Python)

    作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 字典统计词频 排序 日期 [LeetCode] 题目地址:ht ...

  9. 【一天一道LeetCode】#242. Valid Anagram

    一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given t ...

  10. LeetCode OJ:Valid Anagram(有效字谜问题)

    Given two strings s and t, write a function to determine if t is an anagram of s. For example,s = &q ...

随机推荐

  1. 实战视频所需要的IDE和工具软件的下载链接

    以下是视频实战所需要的IDE和工具软件的下载链接: Visual Studio Code(适用于Windows.Mac和Linux):https://code.visualstudio.com/dow ...

  2. [Docker] Dockerfile常用保留字

    FROM 基础镜像,当前新镜像是基于哪个镜像的,指定一个已经存在的镜像作为模板.第一条必须是from MAINTAINER 镜像维护者的姓名和邮箱地址 RUN 容器构建时需要运行的命令,也就是在 do ...

  3. NC16615 [NOIP2008]传纸条

    题目链接 题目 题目描述 小渊和小轩是好朋友也是同班同学,他们在一起总有谈不完的话题.一次素质拓展活动中,班上同学安排做成一个m行n列的矩阵,而小渊和小轩被安排在矩阵对角线的两端,因此,他们就无法直接 ...

  4. Reactive 简介

    1. 概念 Reactive 非常适合低延迟.高吞吐量的工作负载. Reactive Processing 是一种范式(规范),它使开发人员能够构建非阻塞的.异步的应用程序,这些应用程序能够处理背压( ...

  5. Gerrit 笔记

    Gerrit 通过git push后增加一个中间状态, 来完成代码审批环节, 因此在git commit的时候增加了一个change id, 并且push到定制的target, 在push之后, 需要 ...

  6. layui切换select选项事件

    说明 我们经常遇到表单上面选择不同的下拉选项需要触发函数去完成一些业务逻辑,比如我这个地方根据所选商品查询它底下明细的数量,并展示. 效果演示 代码 <!--选择商品--> <div ...

  7. 利用nssm将jar包安装为windows服务

    1.介绍 假设我们有一个spring boot程序打包成jar文件放到windows服务器运行,第一种方式jar -jar xx方式在cmd运行.这样有个缺点,被别人误关闭了咋办?服务器重启了咋办? ...

  8. Java使用正则表达式判断字符串中是否包含某子字符串

    需求: 给定一个字符串s,判断当s中包含"tree fiddy"或"3.50"或"three thirty"子字符串返回true,否则返回f ...

  9. Java I/O 教程(二) 介绍OutputStream 和 InputStream

    OutputStream vs InputStream 我们来看一下两者的工作图: OutputStream 输出流 Java应用程序使用输出流将数据写入到某个目的地,可以是一个文件,数组,外围设备或 ...

  10. 3D环饼图

    // <div class="AnalysisAccCom">         <first-title title="分析对象统计"> ...