成人国产在线小视频_日韩寡妇人妻调教在线播放_色成人www永久在线观看_2018国产精品久久_亚洲欧美高清在线30p_亚洲少妇综合一区_黄色在线播放国产_亚洲另类技巧小说校园_国产主播xx日韩_a级毛片在线免费

資訊專欄INFORMATION COLUMN

[LintCode] Compare Strings

avwu / 705人閱讀

摘要:建立一個(gè)長(zhǎng)度為的數(shù)組,每一位對(duì)應(yīng)那個(gè)字母出現(xiàn)的個(gè)數(shù),先遍歷,對(duì)數(shù)組做增操作,再遍歷,對(duì)數(shù)組做減操作。

Problem

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.

Notice

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

Example

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

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

Note

建立一個(gè)長(zhǎng)度為26的數(shù)組,每一位對(duì)應(yīng)那個(gè)字母出現(xiàn)的個(gè)數(shù),先遍歷A,對(duì)數(shù)組做增操作,再遍歷B,對(duì)數(shù)組做減操作。

Solution
public class Solution {
    public boolean compareStrings(String A, String B) {
        int[] count = new int[26];
        for(int i = 0; i < A.length(); i++){
            count[A.charAt(i) - "A"]++;
        }
        for(int i = 0; i < B.length(); i++){
            count[B.charAt(i) - "A"]--;
            if(count[B.charAt(i) - "A"] < 0) {
            return false;
            }
        }
        return true;    
    }
}

文章版權(quán)歸作者所有,未經(jīng)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請(qǐng)注明本文地址:http://systransis.cn/yun/65950.html

相關(guān)文章

  • [LintCode] Nuts & Bolts Problem

    Problem Given a set of n nuts of different sizes and n bolts of different sizes. There is a one-one mapping between nuts and bolts. Comparison of a nut to another nut or a bolt to another bolt is not ...

    tuomao 評(píng)論0 收藏0
  • [LintCode/LeetCode] Two Strings are Anagrams/Valid

    摘要:建立一個(gè)長(zhǎng)度為的數(shù)組,統(tǒng)計(jì)所有個(gè)字符在出現(xiàn)的次數(shù),然后減去這些字符在中出現(xiàn)的次數(shù)。否則,循環(huán)結(jié)束,說(shuō)明所有字符在和中出現(xiàn)的次數(shù)一致,返回。 Program Write a method anagram(s,t) to decide if two strings are anagrams or not. Example Given s=abcd, t=dcab, return true....

    vslam 評(píng)論0 收藏0
  • [LeetCode/LintCode] Top K Frequent Words

    LeetCode version Problem Given a non-empty list of words, return the k most frequent elements. Your answer should be sorted by frequency from highest to lowest. If two words have the same frequency, t...

    0x584a 評(píng)論0 收藏0
  • [LintCode] Kth Smallest Number in Sorted Matrix

    Problem Find the kth smallest number in at row and column sorted matrix. Example Given k = 4 and a matrix: [ [1 ,5 ,7], [3 ,7 ,8], [4 ,8 ,9], ] return 5 Challenge O(k log n), n is the maximal n...

    mgckid 評(píng)論0 收藏0
  • [LintCode/LeetCode] Find Median From / Data Stream

    摘要:建立兩個(gè)堆,一個(gè)堆就是本身,也就是一個(gè)最小堆另一個(gè)要寫一個(gè),使之成為一個(gè)最大堆。我們把遍歷過(guò)的數(shù)組元素對(duì)半分到兩個(gè)堆里,更大的數(shù)放在最小堆,較小的數(shù)放在最大堆。同時(shí),確保最大堆的比最小堆大,才能從最大堆的頂端返回。 Problem Numbers keep coming, return the median of numbers at every time a new number a...

    zxhaaa 評(píng)論0 收藏0

發(fā)表評(píng)論

0條評(píng)論

最新活動(dòng)
閱讀需要支付1元查看
<