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;
    }
}

 

posted @ 2017-02-21 20:29  友哥  阅读(345)  评论(0编辑  收藏  举报