isAnagram
/*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.*/
public static boolean isAnagram(String s, String t) {
int[] ch1 = new int[26];
char[] cha1 = s.toCharArray();
char[] cha2 = t.toCharArray();
if (cha1.length != cha2.length)
return false;
for (int i = 0; i < cha1.length; i++) {
int temp = cha1[i] - 97;
ch1[temp]++;
}
for (int i = 0; i < cha2.length; i++) {
int temp = cha2[i] - 97;
ch1[temp]--;
}
for (int i : ch1) {
if (i != 0)
return false;
}
return true;
}
}
isAnagram的更多相关文章
- [LeetCode] Valid Anagram 验证变位词
Given two strings s and t, write a function to determine if t is an anagram of s. For example, s = & ...
- Leetcode分类刷题答案&心得
Array 448.找出数组中所有消失的数 要求:整型数组取值为 1 ≤ a[i] ≤ n,n是数组大小,一些元素重复出现,找出[1,n]中没出现的数,实现时时间复杂度为O(n),并不占额外空间 思路 ...
- 全部leetcode题目解答(不含带锁)
(记忆线:当时一刷完是1-205. 二刷88道.下次更新记得标记不能bug-free的原因.) 88-------------Perfect Squares(完美平方数.给一个整数,求出用平方数来 ...
- Leetcode Valid Anagram
Given two strings s and t, write a function to determine if t is an anagram of s. For example,s = &q ...
- LeetCode 242 Valid Anagram
Problem: Given two strings s and t, write a function to determine if t is an anagram of s. For examp ...
- Continue To DO!
(1)Valid Anagram 解题思路: 使用一个数组,首先遍历S相应位置加1,然后遍历T,判断此时如果相应位置为零返回FALSE,否则就减一.T遍历完毕后返回true. 代码如下: public ...
- 【09_242】Valid Anagram
Valid Anagram My Submissions Question Total Accepted: 43694 Total Submissions: 111615 Difficulty: Ea ...
- Valid Anagram
class Solution { public: bool isAnagram(string s, string t) { if(t=="") return s=="&q ...
- 【leetcode❤python】242. Valid Anagram
class Solution(object): def isAnagram(self, s, t): if sorted(list(s.lower()))==sorted(list ...
随机推荐
- 自己理解的javascript 的对象和类理解
首先需要先理解类和对象的意义,我个人理解如下: 类:对象的抽象化: 对象:类的实体: javascript中没有class关键字和类的用法,只能用伪类来做类的,所以要用function来定义累的名字: ...
- 【性能测试】性能测试总结<四>
性能测试常见指标 性能测试说白了就是通过工具模拟多个用户对被测系统进行访问.然后查看系统对于多个用户发来请求的处理能力. 左边的两个小人表示两个用户,向右边服务器发送请求,然后得到服务器 ...
- 【linux】ps 命令详解
[root@andon lib]# ps aux ###常用格式 USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND root 1 0.0 0 ...
- 理解Javascript参数中的arguments对象
ECMAScript中函数没有标签名的特性,所以ECMAScript函数中没有重载. Javascript中arguments的存在可以弥补javascript中函数没有重载的不足. Javascri ...
- 【转】七个例子帮你更好地理解 CPU 缓存
我的大多数读者都知道缓存是一种快速.小型.存储最近已访问的内存的地方.这个描述相当准确,但是深入处理器缓存如何工作的"枯燥"细节,会对尝试理解程序性能有很大帮助. 在这篇博文中,我 ...
- IntelliJ IDEA通过Spring配置连接MySQL数据库
先从菜单View→Tool Windows→Database打开数据库工具窗口,如下图所示: 点击Database工具窗口左上角添加按钮"+",选择Import from sour ...
- android学习笔记39——使用原始资源
原始资源 android中没有专门提供管理支持的类型文件,都被称为原始资源.例如:声音资源... android原始资源存放位置: 1.res/raw,android SDK会处理该目录下的原始资源, ...
- windows下制作u盘启动的工具
推荐rufus,快捷方便
- Android sqlite管理数据库基本用法
Android操作系统中内置了sqlite数据库(有关sqlite数据库详细介绍见:http://zh.wikipedia.org/wiki/SQLite),而sqllite本身是一个很小型的数据库, ...
- NSString类的相关用法
一.NSString字符串连接NSString* string; // 结果字符串 NSString* string1, string2; //已存在的字符串 1. string = [NSStrin ...