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. vos套餐设置

    为实现对客户不同时段按不同费率计算,可以在vos里设置套餐, 具体案例: 1. 2.时段费率 套餐里没包含的时间段是不能打电话的 即:周一到周五       21:30—24:00    00:00- ...

  2. POJ-1469 COURSES---二分图最大匹配--匈牙利算法

    题目链接: https://vjudge.net/problem/POJ-1469 题目大意: 给你p门课程和n个学生,一个学生可以选0门,1门,或者多门课程,现在要求一个由p个学生组成的集合,满足下 ...

  3. A. Round House_数学问题

    A. Round House time limit per test 1 second memory limit per test 256 megabytes input standard input ...

  4. 2月4号学习的一个SSM整合项目,第一课

    本文引自:https://github.com/Sunybyjava/seckill  原作者:sunybyjava@gmail.com seckill 一个整合SSM框架的高并发和商品秒杀项目,学习 ...

  5. P1151 子数整数

    题目描述 对于一个五位数a_1a_2a_3a_4a_5a1​a2​a3​a4​a5​,可将其拆分为三个子数: sub_1=a_1a_2a_3sub1​=a1​a2​a3​ sub_2=a_2a_3a_ ...

  6. 20145238-荆玉茗 《Java程序设计》第四次实验报告

    20145238<Java程序设计>第四次实验报告 实验四 Android环境搭建 实验内容 1.搭建Android环境 2.运行Android 3.修改代码,能输出学号 实验步骤 搭建A ...

  7. Knockout 事件传递参数的方法

    在Knockout中直接使用函数传递参数是不行的,会导致函数在初始化时就被调用. 要实现参数的传递,有2种方法: 1.方法一:使用函数包裹 <div data-bind="event: ...

  8. [vijos p1028] 魔族密码

    描述 风之子刚走进他的考场,就……花花:当当当当~~偶是魅力女皇——花花!!^^(华丽出场,礼炮,鲜花)风之子:我呕……(杀死人的眼神)快说题目!否则……-_-###花花:……咦~~好冷~~我们现在要 ...

  9. docker swarm使用keepalived+haproxy搭建基于percona-xtradb-cluster方案的高可用mysql集群

    一.部署环境 序号 hostname ip 备注 1 manager107 10.0.3.107 centos7;3.10.0-957.1.3.el7.x86_64 2 worker68 10.0.3 ...

  10. 深入理解java虚拟机学习笔记(二)垃圾回收策略

    上篇文章介绍了JVM内存模型的相关知识,其实还有些内容可以更深入的介绍下,比如运行时常量池的动态插入,直接内存等,后期抽空再完善下上篇博客,今天来介绍下JVM中的一些垃圾回收策略.        一. ...