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. MongoDB-MMS使用总结

    环境:阿里云 系统:ubuntu 12.04 数据库:MongoDB shell version: 2.0.4 登录MMS,注册相应用户 根据文档开始安装:Install the Monitoring ...

  2. SHELL脚本攻略(学习笔记)--2.4 find

    转载请注明出处:http://www.cnblogs.com/f-ck-need-u/p/5916657.html   超级强大的find命令. find搜索是从磁盘搜索,而不是从数据库搜索. 2.4 ...

  3. html a tag's href javascript issue

    a标签的href中写js中遇到的问题(ie中有问题,chrome没问题) <a class="free" href="javascript:{$('#suiteTy ...

  4. jafka的zk数据

    查看topics: ls /brokers/topics [mytopic] 查看topic所在的broker,下面例子,mytopic在broker 0 中管理. ls /brokers/topic ...

  5. android框架整理

    http://blog.csdn.net/ma969070578/article/details/27808043 闲来无事.想搭个框架试试 分析一般应用 将资料整理整理 粗略统计 需要以下资料 1. ...

  6. removeClass() 方法

    删除元素的class类别用removeClass() 方法,与addClass()方法对应.具体使用如下: 如果要删除 p 标记是 cls0 的类别,可以使用如下的代码: $("p" ...

  7. std::back_inserter函数用法

    back_inserter函数:配合copy函数,把[a, b)区间的数据插入到string对象的末尾,如果容量不够,动态扩容. 使用案例: 1.客户端与服务器通信场景:服务器向客户端发送数据,客户端 ...

  8. 内存屏障(Memory barrier)-- 转发

    本文例子均在 Linux(g++)下验证通过,CPU 为 X86-64 处理器架构.所有罗列的 Linux 内核代码也均在(或只在)X86-64 下有效. 本文首先通过范例(以及内核代码)来解释 Me ...

  9. jQuery的动画队列

    动画队列主要用到jQuery的queue.dequeue和clearqueue. 1.queue()函数主要是将一个动画函数数组绑定到一个队列上 2.dequeue()函数主要是执行队列的第一个函数, ...

  10. jquery load 加载改造,只加载body

    背景: 项目中大量用到了jquery和easyui组件,原生load经常出现主页面异常,原因是组件被重复初始化.也考虑过用iframe,但是在实际项目中的效果,最终取消了iframe方案,也尝试了其他 ...