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 ...
随机推荐
- Mysql数据库设置定时任务
最近手头在做一个拍卖的电商项目. 中间需要将到点的拍卖会状态设置为进行中. 我们的解决方案是Mysql的定时器任务,这里进行一个简单的总结. 1.使用范围 不是所有的MySQL版本都支持,Mysql ...
- wifi-mac
//18:a6:f7:12:0b:8b //18:a6:f7:1e:a9:57 //18:a6:f7:1f:8e:69 //18:a6:f7:12:0b:9c //18:a6:f7:1f:cd:d4 ...
- js切换实现背景颜色
<script type="text/javascript"> obj=document.getElementsByTagName('h1'); ;i<obj.l ...
- easyUI中tree的简单使用
一.在JS中的代码 $('#tt').tree({ url: baseCtx + 'lib/easyui-1.4/demo/tree/tree_data1.json',//tree数据的来源,json ...
- vs2013 支持C#6.0 Install-Package Microsoft.Net.Compilers
vs2013 支持C#6.0 Install-Package Microsoft.Net.Compilers
- Mac添加bash alias
1. vim ~/.bash_profile (无则新建) 添加 if [ -f ~/.bashrc ]; then source ~/.bashrcfi 2.vim ~/.bashrc (无则新建) ...
- realloc,malloc,calloc函数的区别
from:http://www.cnblogs.com/BlueTzar/articles/1136549.html realloc,malloc,calloc的区别 三个函数的申明分别是: void ...
- Linux命令之dos2unix
Linux命令之dos2unix (2011-09-22 11:24:06) 转载▼ 标签: 杂谈 Linux命令之dos2unix - 将DOS格式文本文件转换成UNIX格式 用途说明 dos2 ...
- CString::Mid成员函数
CString Mid( int nFirst, int nCount ) const; 此成员函数从此CString对象中提取一个长度为nCount个字符的子串,从nFirst(从零开始的索引)指定 ...
- HTML5+学习笔记1-------边看代码边研究中
document.addEventListener('touchstart',function(){ return false; },true); touchstart当手指触摸屏幕时候触发,即使已经 ...