zoukankan      html  css  js  c++  java
  • 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;
        }
    }
    MySQL限时解答,24小时内友哥专业解答
    http://www.yougemysqldba.com
    如有进一步需要请联系微信onesoft007
    微博账号@友哥一指
  • 相关阅读:
    vue create is a Vue CLI 3 only command and you are using Vue CLI 2.9.6. You
    Vue2.x是怎么收集依赖的
    只绑定一次事件的简单方法
    Proxy是怎么做数据劫持的
    使用babel进行打包
    使用npm link进行模块调试
    Webpack 热加载插件的实现原理
    Vue 服务端渲染的数据流
    Vue的生命周期钩子
    Linux定时任务
  • 原文地址:https://www.cnblogs.com/youge-OneSQL/p/6426068.html
Copyright © 2011-2022 走看看