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. s3express截图安装教程

    1.安装s3express_setup.exe 2.设置s3express 设置服务器地址setopt -endpoint:s3.cn-north-1.amazonaws.com.cn 设置协议set ...

  2. [UE4]C++取得蓝图控件实例

    .h /*确认密码输入框*/ UPROPERTY() UEditableTextBox* EditableTextBoxRePassword; .cpp EditableTextBoxPassword ...

  3. file_get_contents是打工文件或URL获取内容的方法,比其稳定的还有curl_get_contents

    相信使用过file_get_contents函数的朋友都知道,当获取的$url访问不了时,会导致页面漫长的等待,甚至还能导致PHP进程占用CPU达100%,因此这个函数就诞生了 分享一个实际在用的函数 ...

  4. Linux命令详解-ftp服务器配置

    1.ftp服务器配置 1.ftp安装: rpm –qa | grep ftp 2.查看安装内容: rpm-ql |more 3.启动ftp服务: service vsftpd start 4.配置文件 ...

  5. python中获取当前路径并添加到系统路径

    import os import sys sys.path.append(os.getcwd())

  6. 深入理解yield(三):yield与基于Tornado的异步回调

    转自:http://beginman.cn/python/2015/04/06/yield-via-Tornado/ 作者:BeginMan 版权声明:本文版权归作者所有,欢迎转载,但未经作者同意必须 ...

  7. 向Nexus仓库推送/使用各种组件

    1.Nuget仓库 使用NuGetPackageExplorer打包制作自己的nupkg https://github.com/NuGetPackageExplorer/NuGetPackageExp ...

  8. XML,XSD,XSLT应用场景

    XML:数据交换的标准  1.数据通信: 其实HTTP就是标准的报文格式,早开发中,设计报文的格式是可以看出这个系统的好坏  2.配置文件:设计一个良好的配置文件比写代码要难,比如Spring的配置文 ...

  9. django中视图处理请求方式(FBV、CBV)

    FBV FBV(function base views) 就是在视图里使用函数处理请求. 在之前django的学习中,我们一直使用的是这种方式,所以不再赘述. CBV CBV(class base v ...

  10. Python之部分基础知识点汇总

    1.三元运算(又称三目运算) 三元运算(又称三目运算),简单条件语句的简写    if a<b: A    else: B等价于:A if a<b else B 2.