Valid Anagram

My Submissions

Question
Total Accepted: 43694 Total Submissions: 111615 Difficulty: Easy

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.

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?

这个方法可以把包括unicode characters在内的都进行对比。

对string的内部进行排序,这个方法要记住!

这个方法的头函数是#include <algorithm>

C++写法:

 class Solution {
public:
bool isAnagram(string s, string t) {
sort(s.begin(), s.end());
sort(t.begin(), t.end());
if (s == t)
return true;
else
return false;
}
};

我的解法时间太长,下面是Discuss里面的几种简单解法:

  1. The idea is simple. It creates a size 26 int arrays as buckets for each letter in alphabet. It increments the bucket value with String s and decrement with string t. So if they are anagrams, all buckets should remain with initial value which is zero. So just checking that and return
 public class Solution {
public boolean isAnagram(String s, String t) {
int[] alphabet = new int[26];
for (int i = 0; i < s.length(); i++) alphabet[s.charAt(i) - 'a']++;
for (int i = 0; i < t.length(); i++) alphabet[t.charAt(i) - 'a']--;
for (int i : alphabet) if (i != 0) return false;
return true;
}
}

  

看了别的解答发现,核心思想都一样,但是语句表达上各有千秋,有的很简单比如上面的要学习。

【09_242】Valid Anagram的更多相关文章

  1. 【转】Valid signing identity not found解决办法(原有IDP私钥丢失)及Certificate、App ID、Devices、Provisioning Profiles之间区别--不错

    原文网址:http://blog.csdn.net/mad1989/article/details/8699147 前言: 刚刚把mini换成了macbookair,之前一直在mini上进行开发,到换 ...

  2. 【leetcode】Valid Sudoku

    题目简述: Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board cou ...

  3. 【leetcode】Valid Palindrome

    题目简述: Given a string, determine if it is a palindrome, considering only alphanumeric characters and ...

  4. 【leetcode】Valid Parentheses

    题目简述: Given a string containing just the characters '(', ')', '{', '}', '[' and ']', determine if th ...

  5. 【leetcode】Valid Number

    Valid Number Validate if a given string is numeric. Some examples:"0" => true" 0.1 ...

  6. 【题解】【字符串】【Leetcode】Valid Palindrome

    Given a string, determine if it is a palindrome, considering only alphanumeric characters and ignori ...

  7. 【LeetCode】- Valid Palindrome(右回文)

    [ 问题: ] Given a string, determine if it is a palindrome, considering only alphanumeric characters an ...

  8. 【LeetCode】 Valid Sudoku

    Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could be ...

  9. 【Leetcode】【Easy】Valid Sudoku

    Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules. The Sudoku board could be ...

随机推荐

  1. Sass浅谈

    对于一名前端开发来说,CSS并不陌生,几乎每天都在和CSS打交道.相处久了就会觉得CSS有些许的机械化,有些许的无趣:就会觉得写CSS很多时候都是在做一些复制粘贴性的工作,布局排版,颜色设置,边框属性 ...

  2. Oracle 表分区

    从以下几个方面来整理关于分区表的概念及操作: 表空间及分区表的概念 表分区的具体作用 表分区的优缺点 表分区的几种类型及操作方法 对表分区的维护性操作 1.表空间及分区表的概念 表空间: 是一个或多个 ...

  3. php与ascii码

    首先 简单说一下历史,ascii码最开始是美国人搞出来的,用来干什么呢?我们知道,计算机只知道0和1,如果我们要计算机识别除了01之外的字符,例如 'a',我们要先告诉计算机‘1100001’就是'a ...

  4. js中RegExp类型

    ECMAScript通过RegExp类型来支持正则表达式. var expression = / pattern / flag ; pattern可以是任意的正则表达式.每个正则都带有标志,用以正则表 ...

  5. ThinkPHP 学习记录

    index.php //入口文件 define('APP_DEBUG',True); //开启调试模式 define('APP_PATH','./Application/'); //定义应用目录 re ...

  6. 1-11 ICMP协议

    ICMP协议 IP不提供可靠的传输服务,也不提供端到端或点到点的确认,如果出错可以通过ICMP报告来看,它在IP模块中实现.TCP/IP协议设计了ICMP就是为了弥补IP协议的不足. 它是TCP/IP ...

  7. Mysql常用命令行大全——转载

    转载地址:http://www.blogjava.net/supperchen/archive/2012/10/11/389340.html 第一招.mysql服务的启动和停止 net stop my ...

  8. java与微信企业号交互

    微信企业号接收消息(使用SpringMVC): http://blog.csdn.net/omsvip/article/details/39480577 微信企业号api: http://qydev. ...

  9. smarty基本语法

    smarty基本语法: 1.注释:<{* this is a comment *}>,注意左右分隔符的写法,要和自己定义的一致. <{* I am a Smarty comment, ...

  10. 获取DataGridView中的的选中行

    1. 获取DataGridView中的的选中行:http://blog.csdn.net/yiqijinbu/article/details/7734593 2.winform datagridvie ...