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的更多相关文章

  1. [LintCode]——目录

    Yet Another Source Code for LintCode Current Status : 232AC / 289ALL in Language C++, Up to date (20 ...

  2. (lintcode全部题目解答之)九章算法之算法班题目全解(附容易犯的错误)

    --------------------------------------------------------------- 本文使用方法:所有题目,只需要把标题输入lintcode就能找到.主要是 ...

  3. Lintcode 85. 在二叉查找树中插入节点

    -------------------------------------------- AC代码: /** * Definition of TreeNode: * public class Tree ...

  4. Lintcode 166. 主元素

    ----------------------------------- Moore's voting algorithm算法:从一个集合中找出出现次数半数以上的元素,每次从集合中去掉一对不同的数,当剩 ...

  5. Lintcode 166. 链表倒数第n个节点

    ----------------------------------- 最开始的想法是先计算出链表的长度length,然后再从头走 length-n 步即是需要的位置了. AC代码: /** * De ...

  6. Lintcode 157. 判断字符串是否没有重复字符

    ------------------------ 因为字符究竟是什么样的无法确定(比如编码之类的),恐怕是没办法假设使用多大空间(位.数组)来标记出现次数的,集合应该可以但感觉会严重拖慢速度... 还 ...

  7. Lintcode 175. 翻转二叉树

    -------------------- 递归那么好为什么不用递归啊...我才不会被你骗...(其实是因为用惯了递归啰嗦的循环反倒不会写了...o(╯□╰)o) AC代码: /** * Definit ...

  8. Lintcode 372. O(1)时间复杂度删除链表节点

    ----------------------------------- AC代码: /** * Definition for ListNode. * public class ListNode { * ...

  9. Lintcode 469. 等价二叉树

    ----------------------------------------------- AC代码: /** * Definition of TreeNode: * public class T ...

  10. Lintcode 375.克隆二叉树

    -------------------------- 水题 AC代码: /** * Definition of TreeNode: * public class TreeNode { * public ...

随机推荐

  1. SAP CRM WebClient UI和Hybris backoffice UI开发的相同点

    CRM WebClient和Hybris backoffice的UI开发都不需要开发人员手写原生的html代码. CRM WebClient UI 在CRM WebUI workbench里,开发人员 ...

  2. DOM(四):h5扩展方法

    getElementByClassName()方法getElementByClassName()方法接收一个参数,即一个包含一或多个类名的字符串,返回带有指定类的所有元素的NodeList //取得所 ...

  3. 使用正则表达式验证IP地址

    实现效果: 知识运用: 实现代码: public bool validate(string str_IP) { string regex = @"(25[0-5]|2[0-4]\d|[0-1 ...

  4. Win10预览版激活信息

    微软在10月2日零点正式公开了Win10预览版的下载地址,这个时间大家应该逐步开始安装工作了,因此提出下面两个问题的用户特别多,IT之家再稍作告知一下.1.Win10预览版安装密钥是什么?答:NKJF ...

  5. iOS 多线程 之 GCD(大中枢派发)(二)

    本文接着上一篇讲.主要讲:dispatch_source. dispatch_source主要用户监听事件,可以监听如下事件 DISPATCH_SOURCE_TYPE_DATA_ADD DISPATC ...

  6. 易语言调用C++写的DLL

    直接调用会弹出堆栈错误的信息,原因是VS默认是__cdcel方式,而易语言是__stdcall,所以调用约定不一致导致堆栈错误. 解决方案很简单,易语言声明DLL函数时“在库中对应命令名”函数名前加一 ...

  7. C/C++程序基础 (十)模板和泛型

    什么是泛型编程 基于模板,有效将算法和数据结构分离. 模板 包括类型和参数 模板函数:抽象的函数定义,代表一类同构函数.编译器在其调用位置自动完成对应模板函数的实例化. 模板类:抽象的类定义,代表更高 ...

  8. MappingException:class com.zsn.crm.Model.user not found whie looking for property user id

    之前好好地运行 什么东西都没动过 再次运行突然报异常*****MappingException:class com.zsn.crm.Model.user not found whie looking ...

  9. Linux - 用户环境变量的查看与设置

    1. 查看当前有哪些环境变量 直接输入命令:env 2. 设置用户环境变量 输入命令:vim ~/.bash_profile,打开文件,输入如下内容: 范例(设置maven环境变量): export ...

  10. 【牛客 错题集】Linux系统方面错题合集

    前言:牛客Linux322道全部刷完,有些题目较老,甚至考核5系统,现在7都出来了几年了 = = 还有些题目解析的很好部分也摘录了进来.很多涉及嵌入式开发的选择题同样的摘录的作为了解使用 ------ ...