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 ...
随机推荐
- select跳转
<select onchange="window.open(this.options[this.selectedIndex].value)"><option> ...
- Subversion与TortoiseSVN的安装
首先介绍一下Subversion与TortoiseSVN两者之间的关系: Subversion是一种集中分享信息的系统,它的核心是版本库,储存所有的数据.版本库按照文件树形式储存数据-包括文件和目录. ...
- html中间块居中宽度自适应
说来,这个其实不是个多难的事情,但是,若没有经验或者没有了解过html原数在浏览器中显示的顺序,可能还真是个问题,不知如何调整. 先说明下,在确定了左右两边显示的块的宽度后,再让中间块的宽度自适应,这 ...
- 【python】sys.argv[]的用法
在学python的过程中,一直弄不明白sys.argv[]的意思,虽知道是表示命令行参数,但还是有些稀里糊涂的感觉. 今天又好好学习了一把,总算是大彻大悟了. Sys.argv[]是用来获取命令行参数 ...
- 剑指offer系列62---两个链表的公共结点
[题目]输入两个链表,找出它们的第一个公共结点. * [思路]1 获取两链表的长度: * 2 让长的链表先走n步后此时走到短链表起始位置: * 3 两链表同时遍历,直至相同,这时返回第一个公共结点. ...
- 【FreeMaker】FreeMaker学习-基础
转载请标明出处:http://www.cnblogs.com/ssslinppp 阅读目录 -04-08 08:08:08 Pacific Daylight Time Tue, Apr 8, '03 ...
- 【转】Java集合框架综述
文章目录 1. 集合框架(collections framework) 2. 设计理念 3. 两大基类Collection与Map 3.1. Collection 3.2. Map 4. 集合的实现( ...
- bzoj1760 [Baltic2009]Triangulation
给定一个多边形的三角剖分(n<=1e5),且每个三角形有其颜色,问最多可以把这个三角剖分分成几个联通的部分,使任何一种颜色不出现在多个连通块中 建出三角剖分对应的树,同种颜色的点之间的路径是不能 ...
- (C++) Interview in English. - Constructors/Destructors
Constructors/Destructors. 我们都知道,在C++中建立一个类,这个类中肯定会包括构造函数.析构函数.复制构造函数和重载赋值操作:即使在你没有明确定义的情况下,编译器也会给你生成 ...
- shell中大小写转换
有两种方式: 1.用tr 例如:UPPERCASE=$(echo $VARIABLE | tr '[a-z]' '[A-Z]') (把VARIABLE的小写转换成大写) LOWERCASE=$(e ...