Leetcode Everyday: 242. Valid Anagram
2016-05-12 00:13
405 查看
https://leetcode.com/problems/valid-anagram/
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?
public class Solution {
public boolean isAnagram(String s, String t) {
if(s.length() != t.length()) return false;
int[] occ = new int[26];
for(int i=0;i<s.length();i++){
occ[s.charAt(i)-'a']++;
}
for(int i=0;i<t.length();i++){
occ[t.charAt(i)-'a']--;
if(occ[t.charAt(i)-'a']<0) return false;
}
return true;
}
}
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?
public class Solution {
public boolean isAnagram(String s, String t) {
if(s.length() != t.length()) return false;
int[] occ = new int[26];
for(int i=0;i<s.length();i++){
occ[s.charAt(i)-'a']++;
}
for(int i=0;i<t.length();i++){
occ[t.charAt(i)-'a']--;
if(occ[t.charAt(i)-'a']<0) return false;
}
return true;
}
}
相关文章推荐
- Wireshark抓包示范:TCP三次握手建立连接和四次握手断开连接
- 记录第一次纯手打爬虫经历
- 记录第一次纯手打爬虫经历
- Retrofit2.0使用总结及注意事项
- 探索《How Tomcat Works》心得(二)
- (三)映射对象标识符(OID)
- 小酌重构系列[10]——分离职责
- 里氏替换原则(Liskov Substitution Principle) LSP
- 小酌重构系列[10]——分离职责
- [Java] 基础命令
- Centos 7 学习之静态IP设置
- netfilter/iptables全攻略
- Install Sentry Server(Sentry Server的搭建)[for_wind]
- C 标准库 —— stdio.h
- CentOS7 下linux不能上网解决方法,centos7 eth0 没有ip,IP突然丢失
- nefu阶乘定理
- C/C++中结构体与类的区别
- 一个例子深入理解ClassLoader
- CocoaPods相关知识点
- dede taglist模板中调用自定义字段