lintcode 比较字符串

作者: yzawyx0220 | 来源:发表于2017-01-03 10:58 被阅读44次

    比较两个字符串A和B,确定A中是否包含B中所有的字符。字符串A和B中的字符都是 大写字母
    样例
    给出 A = "ABCD" B = "ACD",返回 true
    给出 A = "ABCD" B = "AABC", 返回 false
    比较简单的题,直接上代码

    class Solution {
    public:
        /**
         * @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
         */
        bool compareStrings(string A, string B) {
            // write your code here
            sort(A.begin(),A.end());
            sort(B.begin(),B.end());
            int i = 0,j = 0;
            while (i < A.size() && j < B.size()) {
                if (A[i] != B[j]) i++;
                else {
                    i++;
                    j++;
                }
            }
            return (i == A.size() && j != B.size()) ? false : true;
        }
    };
    

    相关文章

      网友评论

        本文标题:lintcode 比较字符串

        本文链接:https://www.haomeiwen.com/subject/pgwgvttx.html