lintcode-easy-Compare Strings

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了lintcode-easy-Compare Strings相关的知识,希望对你有一定的参考价值。

Compare two strings A and B, determine whether A contains all of the characters in B.

The characters in string A and B are all Upper Case letters.

Example

For A = "ABCD"B = "ACD", return true.

For A = "ABCD"B = "AABC", return false.

Note

The characters of B in A are not necessary continuous or ordered.

 

这道题也没太多好说的,cracking code interview提过这种小技巧

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
        if(A == null)
            return false;
        
        if(B == null || B.length() == 0)
            return true;
        
        int[] count = new int[26];
        
        int lengthA = A.length();
        int lengthB = B.length();
        
        if(lengthB > lengthA)
            return false;
        
        for(int i = 0; i < lengthA; i++){
            count[A.charAt(i) - ‘A‘]++;
        }
        
        for(int i = 0; i < lengthB; i++){
            count[B.charAt(i) - ‘A‘]--;
            
            if(count[B.charAt(i) - ‘A‘] < 0)
                return false;
        }
        
        return true;
    }
}

 

以上是关于lintcode-easy-Compare Strings的主要内容,如果未能解决你的问题,请参考以下文章

为啥 ++str 和 str+1 有效而 str++ 无效?

两个大数相加

vim字符串替换命令

Pandas str.extract:AttributeError:'str'对象没有属性'str'

Vim 字符串替换命令

用函数实现对两个字符串str1和str2的比较:strcmp (str1,str2)