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 ...
随机推荐
- linux认识第一面
一.领域问题: 在客户端领域,windows始终占据了优势地位.而在服务器领域,全球98%的都是在用linux.因为linux作为服务器的载体,便宜又安全. 二.linux是基于内核的编写工具,在li ...
- bootstrap panel 和table的使用
一.HTML中的页面内容 <div class="col-sm-12"> <!-- <div class="m-b-md" style= ...
- Java——URLEncoder和URLDecoder
import java.net.URLDecoder; import java.net.URLEncoder; //========================================== ...
- ansible加密命令
ansible-vault用于配置文件加密,如编写的playbook配置文件中包含敏感信息,不希望其他人随意查看,ansible-valut可加密/解密这个配置文件,刚试了下也可以加密txt文档,猜想 ...
- nginx重定向配置
# /etc/nginx/nginx.conf #写在server,location核心模块中,if也可以写.$http_host客户端设法要到达主机的主机名 if ($http_host !~ “^ ...
- git在windows命令行下使用
“不是内部或外部命令,也不是可运行的程序”,通常要将程序的exe路径配置环境变量. 将git的bin目录的路径添加到环境变量path中即可.
- 关于centos7的网络配置
1.DNS DNS是域名系统 (Domain Name System) 的缩写,它是由解析器和域名服务器组成的.域名服务器是指保存有该网络中所有主机的域名和对应IP地址,并具有将域名转换为IP地址功能 ...
- 用java实现zip压缩
本来是写到spaces live上的,可是代码的显示效果确实不怎么好看.在javaeye上试了试代码显示的顺眼多了. 今天写了个用java压缩的功能,可以实现对文件和目录的压缩. 由于java.uti ...
- Java实验1-文件IO
目标:掌握Java类的创建,Java I/O操作,Java集合类的使用等 内容: 王老师非常喜欢读书,为了便于查阅,他每次买书回家后就在笔记本上登记每本书的详细信息(书名.作者.出版社.出版日期.价 ...
- SQL Server 2012不支持从SQL Server 2000的备份进行还原
错误: dbbackup failed: Unable to restore database 'ppt'Not valid backupThe database was backed up on a ...