242. Valid Anagram(leetcode)
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?
对于这道题,想到以下思路:
思路1://最容易想到的是,两个循环嵌套,把字符串s拆成一个一个字符拿去和字符串t比较,若t中有,则剔除掉该字符。(时间复杂度为O(n^2))不推荐
思路2://更简单的是,使用java内置的排序,然后判断字符串是否相等,也比较慢
思路3://想多一步,用一个数组来记录出现的次数,可以使时间复杂度达到O(n)
思路1比较简单,就不做过多的阐述。
思路2:先利用内置排序,使得字符数组有序,然后合成两个新的字符串,然后判断这两个新的字符串是否相等,即可判断是否是Anagram
代码如下:
public boolean isAnagram(String s, String t) {
if(s.length()!=t.length()) return false;
if(s.length()==0&&t.length()==0) return true;
String [] sa=new String[s.length()];
String [] ta=new String[s.length()];
for(int i=0;i<s.length();i++)
{
sa[i]=s.substring(i,i+1);
ta[i]=t.substring(i,i+1);
}
Arrays.sort(sa);
Arrays.sort(ta);
s=null;
t=null;
for(int i=0;i<sa.length;i++)
{
s=sa[i]+s;
t=ta[i]+t;
}
if(s.equals(t))
return true;
return false;
}
思路3:
声明两个26个单位的数组,当字母出现的时候,相应数组位置加一,最后判断相应位置是否等值,即可判断出结果。
public boolean isAnagram(String s, String t) {
if(s.length()!=t.length()) return false;
int[] team1=new int[26];
int[] team2=new int[26];
char[] cs=s.toCharArray();
char[] ct=t.toCharArray();
for(int i=0;i<s.length();i++)
{
team1[cs[i]-'a']+=1;
team2[ct[i]-'a']+=1;
}
for(int i=0;i<26;i++)
if(team1[i]!=team2[i])
return false;
return true;
}
思路3的变体:
在s中出现的字符就在相应的位置加一,在t中出现的字符就在相应的位置减一,最后判断结果是否为0就可以判断是否是Anagram,这样可以省去一个数组的空间。
public boolean isAnagram(String s, String t) {
if(s.length()!=t.length()) return false;
int[] team=new int[26];
char[] cs=s.toCharArray();
char[] ct=t.toCharArray();
for(int i=0;i<s.length();i++)
team[cs[i]-'a']+=1;
for(int i=0;i<s.length();i++)
team[ct[i]-'a']-=1;
for(int i=0;i<26;i++)
if(team[i]!=0)
return false;
return true;
}
242. Valid Anagram(leetcode)的更多相关文章
- 22. leetcode 242. Valid Anagram(由颠倒字母顺序而构成的字)
22. 242. Valid Anagram(由颠倒字母顺序而构成的字) Given two strings s and t, write a function to determine if t i ...
- LN : leetcode 242 Valid Anagram
lc 242 Valid Anagram 242 Valid Anagram Given two strings s and t, write a function to determine if t ...
- 242. Valid Anagram(C++)
242. Valid Anagram Given two strings s and t, write a function to determine if t is an anagram of s. ...
- 【LeetCode】242. Valid Anagram (2 solutions)
Valid Anagram Given two strings s and t, write a function to determine if t is an anagram of s. For ...
- [leetcode]242. Valid Anagram验证变位词
Given two strings s and t , write a function to determine if t is an anagram of s. Example 1: Input: ...
- 【一天一道LeetCode】#242. Valid Anagram
一天一道LeetCode 本系列文章已全部上传至我的github,地址:ZeeCoder's Github 欢迎大家关注我的新浪微博,我的新浪微博 欢迎转载,转载请注明出处 (一)题目 Given t ...
- LeetCode 242. 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 验证变位词
Given two strings s and t , write a function to determine if t is an anagram of s. Example 1: Input: ...
- 【LeetCode】242. Valid Anagram 解题报告(Java & Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 目录 题目描述 题目大意 字典统计词频 排序 日期 [LeetCode] 题目地址:ht ...
随机推荐
- Java课程设计——计算器
1.团队课程设计博客链接 http://www.cnblogs.com/yuanj/p/7072137.html 2.个人负责模块或任务说明 确定课题并进行任务分工 编写计算器删除,清零,清空,小数点 ...
- 201521123050 《Java程序设计》第9周学习总结
1. 本周学习总结 2. 书面作业 本次PTA作业题集异常 1.常用异常 题目5-1 1.1 截图你的提交结果(出现学号) 1.2 自己以前编写的代码中经常出现什么异常.需要捕获吗(为什么)?应如何避 ...
- 201521123031 《Java程序设计》第12周学习总结
1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结多流与文件相关内容. 2. 书面作业 1.将Student对象(属性:int id, String name,int age,dou ...
- 杭电acm-2007平方和立方和
#include<stdio.h>int main(){ int t,m,n,x,y,i; while(scanf("%d%d",&n, ...
- SpringMVC第五篇【方法返回值、数据回显、idea下配置虚拟目录、文件上传】
Controller方法返回值 Controller方法的返回值其实就几种类型,我们来总结一下-. void String ModelAndView redirect重定向 forward转发 数据回 ...
- 个人从源码理解angular项目在JIT模式下的启动过程
通常一个angular项目会有一个个模块(Module)来管理各自的业务,并且必须有一个根模块(AppModule)作为应用的入口模块,整个应用都围绕AppModule展开.可以这么说,AppModu ...
- JSP内置对象的实验报告,页面登陆设计
JSP内置对象的实验报告 一.实验目的: 本实验的目的是让学生掌握怎样在JSP中使用内置对象request.page.response等. 二.实验要求: 编写四个JSP 页面login.jsp.Re ...
- 理解ios 11中webview的视口
iOS 11在状态栏区域带来了一些新的,也许是不直观的行为,这对使用Apache Cordova或Ionic等工具的开发人员尤为重要.特别是,这种行为变化会影响任何基于Web的应用程序,这些应用程序在 ...
- 一、js的数据类型
一.数据类型 ECMAScript中有5种简单数据类型:Undefined.Null.Boolean.Number和String.还有一种复杂数据类型--Object.ECMAScript不支持任何创 ...
- c++builder中 扩展c++的关键字 : _published _automated Get/Set指令 _fastcall
C++Builder为C++增加了许多关键字,以适应其快速应用开发(RAD)环境.包括关键字和Get/Set指令. 1._published类似publich权限范围,_published像publi ...