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 ...
随机推荐
- fetch用法
fetch(REQUEST_URL) .then((response) => response.json()) .then((responseData)=> { console.log(r ...
- python中保留两位小数
今天写程序的时候碰到了一个问题关于如何控制浮点数只显示小数点后两位,正常的想法是用round函数,例如 round(a, 2),但是在面对下面的问题时候round就不太好用了 >>> ...
- C#接口作用的深入理解
1.C#接口的作用 C#接口是一个让很多初学C#者容易迷糊的东西,用起来好像很简单,定义接口,里面包含方法,但没有方法具体实现的代码,然后在继承该接口的类里面要实现接口的所有方法的代码,但没有真正认识 ...
- mysql四种事务隔离级的说明
·未提交读(Read Uncommitted):允许脏读,也就是可能读取到其他会话中未提交事务修改的数据 ·提交读(Read Committed):只能读取到已经提交的数据.Oracle等多数数据库默 ...
- mysql查询表中的所有字段
select column_name, table_name from information_schema.columns where table_schema='yizhan' and colum ...
- JABX简单介绍
主要引至http://suo.iteye.com/blog/1233458 一.简介 1.概念是什么:(Java Architecture for XML Binding) 是一个业界的标准,是一项可 ...
- Servlet Filter 1
1.Filter简介 )Filter也称之为过滤器,它是Servlet技术中最实用的技术,WEB开发人员通过Filter技术,对web服务器管理的所有web资源:例如Jsp, Servlet, 静态图 ...
- LintCode "Number of Islands II"
A typical Union-Find one. I'm using a kinda Union-Find solution here. Some boiler-plate code - yeah ...
- SPOJ #692. Fruit Farm
Another palindrome related problem. Actually nothing too theoretical here, but please keep following ...
- Windows Server 2008 - How to Move the Quorum Disk Group
I received this question from a friend the other day - asking how on a Windows Server 2008 cluster y ...