Write a method anagram(s,t) to decide if two strings are anagrams or not.

判断两个字符串里的字符是否相同,也就是是否能够通过改变字母顺序而变成相同的字符串。

如果是返回true,如果不是返回false。

Clarification

What is Anagram?
- Two strings are anagram if they can be the same after change the order of characters.

Example

Given s = "abcd", t = "dcab", return true.
Given s = "ab", t = "ab", return true.
Given s = "ab", t = "ac", return false.

public class Solution {
/**
* @param s: The first string
* @param b: The second string
* @return true or false
*/
public boolean anagram(String s, String t) {
if (s.length() != t.length()) {
return false;
}
int[] count = new int[256]; for(int i = 0; i < s.length(); i++) {
count[(int) s.charAt(i)]++;
} for(int j = 0; j < t.length(); j++) {
count[(int) t.charAt(j)]--;
if (count[(int) t.charAt(j)] < 0){
return false;
}
}
return true;
}
};

Two Strings Are Anagrams的更多相关文章

  1. LintCode Two Strings Are Anagrams

    1. 把string变为char数组 2. 排序Arrays.sort() public class Solution { /** * @param s: The first string * @pa ...

  2. [LeetCode] Anagrams 错位词

    Given an array of strings, return all groups of strings that are anagrams. Note: All inputs will be ...

  3. Group Anagrams

    Given an array of strings, group anagrams together. For example, given: ["eat", "tea& ...

  4. LeetCode-Group Anagrams

    Given an array of strings, group anagrams together. For example, given: ["eat", "tea& ...

  5. 【Leetcode】【Medium】Group Anagrams

    Given an array of strings, group anagrams together. For example, given: ["eat", "tea& ...

  6. [LeetCode]题解(python):049-Groups Anagrams

    题目来源 https://leetcode.com/problems/anagrams/ Given an array of strings, group anagrams together. For ...

  7. 49. Group Anagrams

    Given an array of strings, group anagrams together. For example, given: ["eat", "tea& ...

  8. LeetCode49 Group Anagrams

    Given an array of strings, group anagrams together. For example, given: ["eat", "tea& ...

  9. 49. Anagrams

    题目: Given an array of strings, return all groups of strings that are anagrams. Note: All inputs will ...

随机推荐

  1. vmware下linux系统的安装过程

    虚拟机VMware下CentOS6.6安装教程图文详解 [日期:2016-05-24] 来源:Linux社区  作者:Sungeek [字体:大 中 小]   分享下,虚拟机VMware下CentOS ...

  2. 之前总结的今天给大分享一下iOS

    退回输入键盘 苹果 ios 开发一年的工作笔记 - (BOOL) textFieldShouldReturn:(id)textField{ [textField resignFirstResponde ...

  3. wpf 窗体内容旋转效果 网摘

    <Window x:Class="simplewpf.chuangtixuanzzhuan"        xmlns="http://schemas.micros ...

  4. JS定时器

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  5. Eclipse学习笔记——快捷键

    alt+/ 直接呼出要重写的方法 ctrl+1 快速修复(没导包,没抛出异常等等) ctrl+shift+o 导包 ctrl+shift+F 格式化代码块(自动给代码排版) alt+方向键 向前向后( ...

  6. Win7环境下Eclipse连接Hadoop2.2.0

    准备: 确保hadoop2.2.0集群正常运行 1.eclipse中建立java工程,导入hadoop2.2.0相关jar包 2.在src根目录下拷入log4j.properties,通过log4j查 ...

  7. 解决问题--VS2012中一个Panel覆盖另一个Panel时拖动时容易造成两个控件成父子关系的避免

    在*.Designer.cs中,假如想把panel1覆盖到panel2上,但是VS自动让panel1成为panel2的子控件了,在文件中会有this.panel2.Controls.Add(this. ...

  8. osharp3引入事务后操作结果类别的调整

    /// <summary> /// 表示业务操作结果的枚举, /// 对于业务务操作单元的影响只有二种状态, /// 成功,无变化: 操作将继续,事务将继续 /// 失败:将导致 操作被中 ...

  9. [C#]System.Timers.Timer

    摘要 在.Net中有几种定时器,最喜欢用的是System.Timers命名空间下的定时器,使用起来比较简单,作为定时任务,有Quartz.net,但有时候,一个非常简单的任务,不想引入这个定时任务框架 ...

  10. CSS样式表继承详解

    最近在恶补css样式表的基础知识.上次研究了css样式表之冲突问题详解 .这次是对 css 继承 特性的学习. 什么是css 继承?要想了解css样式表的继承,我们先从文档树(HTML DOM)开始. ...