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

题意:

验证变位词

何谓anagram?

思路:

用int[] map = new int[256]代表一个简化版的HashMap

挨个扫字符串s的每个字符,在map里标注++

挨个扫字符串t的每个字符,在map里标注--

根据valid anagram属性, map里最终应该都该为0。

代码:

 class Solution {
public boolean isAnagram(String s, String t) {
if(s.length() != t.length()) return false;
int[] map = new int[256];
for(int i = 0 ; i < s.length(); i++){
map[s.charAt(i)]++;
map[t.charAt(i)]--;
}
for(int i : map){
if(i != 0){
return false;
}
}
return true;
}
}

[leetcode]242. Valid Anagram验证变位词的更多相关文章

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

  2. [LeetCode] Valid Anagram 验证变位词

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

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

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

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

  5. LeetCode 242. Valid Anagram (验证变位词)

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

  6. Leetcode 242. Valid Anagram(有效的变位词)

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

  7. LeetCode 242 Valid Anagram

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

  8. (easy)LeetCode 242.Valid Anagram

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

  9. Java [Leetcode 242]Valid Anagram

    题目描述: Given two strings s and t, write a function to determine if t is an anagram of s. For example, ...

随机推荐

  1. 自己写的jQuery浮动广告插件

    效果图: 文件位置摆放: 插件的js代码: $.extend({ pfAdv:function(options){ var defaults={ count:1, startTop:200, star ...

  2. Ubuntu下Code::Blocks错误

    #error This file requires compiler and library support for the ISO C++ 2011 standard. This support i ...

  3. Python Json序列化与反序列化

    在python中,序列化可以理解为:把python的对象编码转换为json格式的字符串,反序列化可以理解为:把json格式字符串解码为python数据对象.在python的标准库中,专门提供了json ...

  4. cplexJava源码---计算结果

    public static class CplexStatus implements Serializable { static final long serialVersionUID = -7367 ...

  5. 0_Simple__simpleCubemapTexture

    立方体纹理贴图 ▶ 源代码.用纹理方法把元素按原顺序从 CUDA3D 数组中取出来,求个相反数放入全局内存,输出. #include <stdio.h> #include "cu ...

  6. sqoop2的使用测试

    查看现有link sqoop:000> show link+-----------+------------------------+---------+|   Name    |     Co ...

  7. cmd查看电脑是32位还是64位

    代码如下 @echo off if "%PROCESSOR_ARCHITECTURE%" == "AMD64" (   echo OS is 64bit) EL ...

  8. clip-path的任意元素的碎片拼接动效

    看了张大神的这篇文章后自己写的,兼容性不好clip-path要加-webkit- css #test img{position: absolute;} .active .clip{ will-chan ...

  9. Vue2.0中v-for迭代语法变化(key、index)

    语法发生了变化:http://blog.csdn.net/sinat_35512245/article/details/53966788 新数组语法 value in arr (value, inde ...

  10. UI5-文档-4.17-Fragment Callbacks

    现在我们已经集成了对话框,是时候添加一些用户交互了.用户肯定希望在某个时候再次关闭对话框,因此我们添加一个按钮来关闭对话框并分配一个事件处理程序. Preview The dialog now has ...