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;

    }
}
时间: 2024-07-30 20:32:45

lintcode的相关文章

[lintcode the-smallest-difference]最小差(python)

题目链接:http://www.lintcode.com/zh-cn/problem/the-smallest-difference/ 给定两个整数数组(第一个是数组 A,第二个是数组 B),在数组 A 中取 A[i],数组 B 中取 B[j],A[i] 和 B[j]两者的差越小越好(|A[i] - B[j]|).返回最小差. 排好序后用两个指针分别扫描两个数组,每次更新他们的差值的绝对值.并且依据他们两个数字的大小来决定谁来移动指针. 1 class Solution: 2 # @param

lintcode.44 最小子数组

最小子数组 描述 笔记 数据 评测 给定一个整数数组,找到一个具有最小和的子数组.返回其最小和. 注意事项 子数组最少包含一个数字 您在真实的面试中是否遇到过这个题? Yes 哪家公司问你的这个题? Airbnb Amazon LinkedIn Cryptic Studios Dropbox Apple Epic Systems TinyCo Yelp Hedvig Zenefits Uber Snapchat Yahoo Microsoft Bloomberg Facebook Google

lintcode 66.67.68 二叉树遍历(前序、中序、后序)

AC代码: /** * Definition of TreeNode: * public class TreeNode { * public int val; * public TreeNode left, right; * public TreeNode(int val) { * this.val = val; * this.left = this.right = null; * } * } */ public class Solution { /** * @param root: The r

[LintCode/LeetCode]——两数和、三数和、四数和

LintCode有大部分题目来自LeetCode,但LeetCode比较卡,下面以LintCode为平台,简单介绍我AC的几个题目,并由此引出一些算法基础. 1)两数之和(two-sum) 题目编号:56,链接:http://www.lintcode.com/zh-cn/problem/two-sum/ 题目描述: 给一个整数数组,找到两个数使得他们的和等于一个给定的数 target. 你需要实现的函数twoSum需要返回这两个数的下标, 并且第一个下标小于第二个下标.注意这里下标的范围是 1

Lintcode 469. 等价二叉树

----------------------------------------------- AC代码: /** * Definition of TreeNode: * public class TreeNode { * public int val; * public TreeNode left, right; * public TreeNode(int val) { * this.val = val; * this.left = this.right = null; * } * } */

Lintcode 75.寻找峰值

--------------------------------------- 按照给定的峰值定义,峰值的左半部分一定是递增的,所以只要找到不递增的即可. AC代码: class Solution { /** * @param A: An integers array. * @return: return any of peek positions. */ public int findPeak(int[] A) { for(int i=1;i<A.length;i++){ if(A[i]>=

Lintcode 9.Fizz Buzz 问题

------------------------ AC代码: class Solution { /** * param n: As description. * return: A list of strings. */ public ArrayList<String> fizzBuzz(int n) { ArrayList<String> results = new ArrayList<String>(); for (int i = 1; i <= n; i++

Lintcode 97.二叉树的最大深度

--------------------------------- AC代码: /** * Definition of TreeNode: * public class TreeNode { * public int val; * public TreeNode left, right; * public TreeNode(int val) { * this.val = val; * this.left = this.right = null; * } * } */ public class S

[Lintcode two-sum]两数之和(python,双指针)

题目链接:http://www.lintcode.com/zh-cn/problem/two-sum/ 给一个整数数组,找到两个数使得他们的和等于一个给定的数target. 备份一份,然后排序.搞两个指针分别从左从右开始扫描,每次判断这两个数相加是不是符合题意,如果小了,那就把左边的指针向右移,同理右指针.然后在备份的数组里找到位置. 1 class Solution: 2 """ 3 @param numbers : An array of Integer 4 @param

[LintCode] Trapping Rain Water II

Trapping Rain Water II Given n x m non-negative integers representing an elevation map 2d where the area of each cell is 1 x 1, compute how much water it is able to trap after raining. Example Given 5*4 matrix [12,13,0,12] [13,4,13,12] [13,8,10,12] [