Two Strings Are Anagrams
Write a method anagram(s,t) to decide if two strings are anagrams or not.
判断两个字符串里的字符是否相同,也就是是否能够通过改变字母顺序而变成相同的字符串。
如果是返回true,如果不是返回false。
What is Anagram?
- Two strings are anagram if they can be the same after change the order of characters.
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的更多相关文章
- LintCode Two Strings Are Anagrams
1. 把string变为char数组 2. 排序Arrays.sort() public class Solution { /** * @param s: The first string * @pa ...
- [LeetCode] Anagrams 错位词
Given an array of strings, return all groups of strings that are anagrams. Note: All inputs will be ...
- Group Anagrams
Given an array of strings, group anagrams together. For example, given: ["eat", "tea& ...
- LeetCode-Group Anagrams
Given an array of strings, group anagrams together. For example, given: ["eat", "tea& ...
- 【Leetcode】【Medium】Group Anagrams
Given an array of strings, group anagrams together. For example, given: ["eat", "tea& ...
- [LeetCode]题解(python):049-Groups Anagrams
题目来源 https://leetcode.com/problems/anagrams/ Given an array of strings, group anagrams together. For ...
- 49. Group Anagrams
Given an array of strings, group anagrams together. For example, given: ["eat", "tea& ...
- LeetCode49 Group Anagrams
Given an array of strings, group anagrams together. For example, given: ["eat", "tea& ...
- 49. Anagrams
题目: Given an array of strings, return all groups of strings that are anagrams. Note: All inputs will ...
随机推荐
- Interface/接口
1. 类和结构能够实现接口 2. 接口声明包含如下四种类型:属性.方法.事件和索引:这些函数声明不能包含任何实现代码,而在每一个成员的主体后必须使用分号 3. 继承接口的类或结构必须实现接口中的所有成 ...
- 20145212 《Java程序设计》第4周学习总结
20145212 <Java程序设计>第4周学习总结 教材学习内容总结 第六章知识点: 1.继承基本上就是避免多个类间重复定义的行为. 2.子类继承父类,通过继承,我们可以避免类间的重复定 ...
- JavaWeb学习笔记——Ajax
- CSS学习笔记——简述
CSS3学习的教程来自后盾网 div+css网页标准布局 1>div i>DIV全称是division,意为“区块.分割”,DIV标签是一个无意义的容器标签,用于将页面划分出不同的区域 i ...
- 替换所有的cell的右侧箭头
写个UITableViewCell的分类重写这个方法 - (void)didMoveToSuperview { [super didMoveToSuperview]; // 全局替换右侧箭头 if ( ...
- RobotFramework——介绍篇
1.简介Robot Framework是一款python编写的功能自动化测试框架.具备良好的可扩展性,支持关键字驱动,可以同时测试多种类型的客户端或者接口,可以进行分布式测试执行.主要用于轮次很多的验 ...
- Linux基础整理-软件的安装与卸载
redhat/centos/fedora/suse系列: 摘自网址:http://www.runoob.com/linux/linux-yum.html yum( Yellow dog Updater ...
- Bitcask 存储模型
Bitcask 存储模型 Bitcask 是一个日志型.基于hash表结构的key-value存储模型,以Bitcask为存储模型的K-V系统有 Riak和 beansdb新版本. 日志型数据存储 何 ...
- MySQL注入
SQL Injection Tutorial by Marezzi (MySQL) SQL注入教程由Marezzi(MySQL的) In this tutorial i will describe h ...
- yii2 如何用命名空间方式使用第三方类库
原文地址:http://www.yiichina.com/tutorial/395 Yii 2.0最显著的特征之一就是引入了命名空间,因此对于自定义类的引入方式也同之前有所不同.这篇文章讨论一下如何利 ...