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 ...
随机推荐
- Android拍照、录像、录音代码范例
<p>import java.io.File; import java.text.SimpleDateFormat; import java.util.Date; import andro ...
- no ocijdbc11 in java.library.path linux
no ocijdbc11 in java.library.path linux vi /etc/profile export ORACLE_HOME=/oracle/database/oracle/p ...
- CentOS快速搭建subversion服务器
环境:centos x86_64说明:使用daemon的方式来跑svn,不使用apache首先到:http://subversion.tigris.org/ 下载最新的subversion源码 我下的 ...
- object-单例设计模式
1.什么是单例? 单例是一种数据共享的方法,其实就是等同于c语言中的全局变量.在整个程序生命周期内,该对象只有一份存在内存中,可以在多个对象之间共享数据 单例其实就相当于一个类只能有一个实 ...
- DIV中的垂直居中
<div style="border:0px #ff0000 solid; width:100px;height:380px; line-height:380px; float:lef ...
- WinForm窗体拖动代码
本文转载自:http://www.cnblogs.com/ap0606122/archive/2012/10/23/2734964.html using System; using System.Co ...
- 【ntp】centos7下ntp服务器设置
安装ntp #检查服务是否安装 rpm -q ntp #安装ntp服务器 yum -y install ntp 修改配置文件:/etc/ntp.conf 内容如下: restrict default ...
- FFTW简介及使用
http://fftw.org/ FFTW is a C subroutine library for computing the discrete Fourier transform (DFT) i ...
- SVN设置实例
D:\scmserver\SVNROOT\safeControl,该SVN项目下,有erSystem和hcSystem两个项目.现在人员有两种类型的人,一个内部人员,一个是佰钧成人员. 设置要求: 1 ...
- purgeIdleCellConnections: found one to purge conn = 0x1e09f7d0
purgeIdleCellConnections: found one to purge conn = 0x1e09f7d0 你在iOS6下使用3G网络时可能会遇到这条log,不用紧张,这只是苹果的工 ...