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 ...
随机推荐
- BZOJ树链剖分题目汇总
1036,2157,2243,4034,4196;2325,2908,3083,3159,3531,3626,3999;可以不树剖:1146;2819,2843,4448,4530.
- 9.12 其他样式;JS
Display 显示block和隐藏none,不占位置Visbility 显示visible和隐藏hidden,占位置Overflow 超出范围 hidden隐藏透明圆角 Js脚本语言(JavaScr ...
- WinForm------RepositoryItemCheckEdit属性介绍
//去掉第三种状态 editcheck1.OptionView.NullStyle = UnChecked
- linux 相关快捷键
linux 相关快捷键 http://linux.chinaunix.net/begin/2004-10-05/34.shtml#_Toc41417098 1.使用虚拟控制台登录后按“Alt+F2”键 ...
- Java——复选框:JCheckBox
import java.awt.Container; import java.awt.GridLayout; import java.awt.event.ItemEvent; import java. ...
- JavaWeb学习笔记——开发动态WEB资源(八)cookies和httpsession
会话: cookies: (1)cookies是WEB服务器发送到浏览器的简短文本信息 (2)cookies可以禁用 httpsession: 一次会话是从你打开浏览器开始到你关闭浏览器结束 提供一种 ...
- JSP 属性范围
参考文献:http://www.cnblogs.com/xdp-gacl/p/3781056.html 一.属性范围 所谓的属性范围就是一个属性设置之后,可以经过多少个其他页面后仍然可以访问的保存范围 ...
- hdu 1318 Palindromes
Palindromes Time Limit:3000MS Memory Limit:0KB 64bit ...
- JPG / TGA
JPG http://www.openjpeg.org http://libjpeg.sourceforge.net TGA http://tgalib.sourceforge.net
- php 利用ffmpeg将amr转MP3
原文地址: http://www.jianshu.com/p/895d5568ce70 http://www.cnblogs.com/wanghetao/p/3386311.html http://w ...