lintcode
public class Solution {
/**
* @param s: The first string
* @param b: The second string
* @return true or false
*/
public boolean anagram(String s, String t) {
// write your code here
Map<String , Integer> sMap = new HashMap<String , Integer>();
Map<String , Integer> tMap = new HashMap<String , Integer>();
if(s.length() != t.length()){
return false;
}
int i=0;
int wCount=0;
while(i<s.length()){
if(sMap.containsKey(s.substring(i,i+1))){
wCount=sMap.get(s.substring(i,i+1)).intValue();
wCount++;
sMap.put(s.substring(i,i+1) , new Integer(wCount));
}
else{
sMap.put(s.substring(i,i+1) , new Integer(1));
}
i++;
}
i=0;
while(i<t.length()){
if(tMap.containsKey(t.substring(i,i+1))){
wCount=tMap.get(t.substring(i,i+1)).intValue();
wCount++;
tMap.put(t.substring(i,i+1) , new Integer(wCount));
}
else{
tMap.put(t.substring(i,i+1) , new Integer(1));
}
i++;
}
Set sEntrySet = sMap.entrySet();
Iterator sIterator = sEntrySet.iterator();
while(sIterator.hasNext()){
Map.Entry pair = (Map.Entry)sIterator.next();
if(! tMap.containsKey(pair.getKey()) ){
return false;
}else{
if(((Integer)tMap.get(pair.getKey())).intValue() != ((Integer)(pair.getValue())).intValue()){
return false;
}
}
}
return true;
}
}
public class Solution {
/**
* @param A : A string includes Upper Case letters
* @param B : A string includes Upper Case letter
* @return : if string A contains all of the characters in B return true else return false
*/
public boolean compareStrings(String A, String B) {
// write your code here
String strTemp;
int i=0;
while(i<B.length()){
if((( strTemp=A.replaceFirst(B.substring(i,i+1),"") ).compareTo(A)) ==0 ){
return false;
}
else{
A=strTemp;
}
i++;
}
return true;
}
}
class Solution {
/**
* Returns a index to the first occurrence of target in source,
* or -1 if target is not part of source.
* @param source string to be scanned.
* @param target string containing the sequence of characters to match.
*/
public int strStr(String source, String target) {
// write your code here
int i=0;
if((source==null)||(target==null)) return -1;
while(i<(source.length()-target.length()+1)){
if(source.substring(i,i+target.length()).compareTo(target) == 0){
return i;
}
i++;
}
return -1;
}
}
lintcode的更多相关文章
- [LintCode]——目录
Yet Another Source Code for LintCode Current Status : 232AC / 289ALL in Language C++, Up to date (20 ...
- (lintcode全部题目解答之)九章算法之算法班题目全解(附容易犯的错误)
--------------------------------------------------------------- 本文使用方法:所有题目,只需要把标题输入lintcode就能找到.主要是 ...
- Lintcode 85. 在二叉查找树中插入节点
-------------------------------------------- AC代码: /** * Definition of TreeNode: * public class Tree ...
- Lintcode 166. 主元素
----------------------------------- Moore's voting algorithm算法:从一个集合中找出出现次数半数以上的元素,每次从集合中去掉一对不同的数,当剩 ...
- Lintcode 166. 链表倒数第n个节点
----------------------------------- 最开始的想法是先计算出链表的长度length,然后再从头走 length-n 步即是需要的位置了. AC代码: /** * De ...
- Lintcode 157. 判断字符串是否没有重复字符
------------------------ 因为字符究竟是什么样的无法确定(比如编码之类的),恐怕是没办法假设使用多大空间(位.数组)来标记出现次数的,集合应该可以但感觉会严重拖慢速度... 还 ...
- Lintcode 175. 翻转二叉树
-------------------- 递归那么好为什么不用递归啊...我才不会被你骗...(其实是因为用惯了递归啰嗦的循环反倒不会写了...o(╯□╰)o) AC代码: /** * Definit ...
- Lintcode 372. O(1)时间复杂度删除链表节点
----------------------------------- AC代码: /** * Definition for ListNode. * public class ListNode { * ...
- Lintcode 469. 等价二叉树
----------------------------------------------- AC代码: /** * Definition of TreeNode: * public class T ...
- Lintcode 375.克隆二叉树
-------------------------- 水题 AC代码: /** * Definition of TreeNode: * public class TreeNode { * public ...
随机推荐
- React怎么创建.babelrc文件
在windows环境下做react开发其实是一件非常让人头疼的事,强烈建议使用Mac或者是Linux系统,否则真的是自己挖坑自己跳了. 不过,这里还是给大家说说如何在windows环境下新建一个.ba ...
- mysql索引和正确使用方式
一.索引类型 B树索引:大部分都是,因此B树的特性限制了索引如何使用:必须看看索引的正确使用限制(含组合索引的限制)http://blog.csdn.net/lovemdx/article/detai ...
- IOS 封装View的fram(X Y W H )
@interface UIView (Extension) @property (nonatomic, assign) CGFloat x; @property (nonatomic, assign) ...
- 1.06 在WHERE子句中引用取别名的列
如下查询,会抛出错误: mysql> select sal as salary, comm as commission from emp where salary < 5000;ERROR ...
- 缓存头Cache-Control的含义和使用
首先Cache-Control有哪些特性呢?一个是可缓存性 可缓存性 public: 代表这个http请求返回的内容所经过的任何路径中,包括一些中间的http的代理服务器,以及发出这个请求的客户端浏览 ...
- SSM框架整合:转自:http://blog.csdn.net/zhshulin
使用SSM(Spring.SpringMVC和Mybatis)已经有三个多月了,项目在技术上已经没有什么难点了,基于现有的技术就可以实现想要的功能,当然肯定有很多可以改进的地方.之前没有记录SSM整合 ...
- P1540 机器翻译
题目背景 小晨的电脑上安装了一个机器翻译软件,他经常用这个软件来翻译英语文章. 题目描述 这个翻译软件的原理很简单,它只是从头到尾,依次将每个英文单词用对应的中文含义来替换.对于每个英文单词,软件会先 ...
- Java 字符串转码工具类
StringConvertUtils.java package javax.utils; /** * 字符串转码工具类 * * @author Logan * @createDate 2019-04- ...
- 第2章-如何安装KEIL5—零死角玩转STM32-F429系列
第2章 如何安装KEIL5 集视频教程和1000页PDF教程请到秉火论坛下载:www.firebbs.cn 野火视频教程优酷观看网址:http://i.youku.com/firege 本章内 ...
- matlab vs联调
vs 和matlab联调,最近真的把我搞挂了要. 首先,怎么进入联调呢.在vs里先设置一下. vs:tools->attach to process,选择matlab,注意此时matlab一定是 ...