摘要:建立一個長度為的數組,每一位對應那個字母出現的個數,先遍歷,對數組做增操作,再遍歷,對數組做減操作。
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.
NoticeThe characters of B in A are not necessary continuous or ordered.
ExampleFor A = "ABCD", B = "ACD", return true.
For A = "ABCD", B = "AABC", return false.
Note建立一個長度為26的數組,每一位對應那個字母出現的個數,先遍歷A,對數組做增操作,再遍歷B,對數組做減操作。
Solutionpublic 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; } }
文章版權歸作者所有,未經允許請勿轉載,若此文章存在違規行為,您可以聯系管理員刪除。
轉載請注明本文地址:http://specialneedsforspecialkids.com/yun/65950.html
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 ...
摘要:建立一個長度為的數組,統計所有個字符在出現的次數,然后減去這些字符在中出現的次數。否則,循環結束,說明所有字符在和中出現的次數一致,返回。 Program Write a method anagram(s,t) to decide if two strings are anagrams or not. Example Given s=abcd, t=dcab, return true....
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...
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...
摘要:建立兩個堆,一個堆就是本身,也就是一個最小堆另一個要寫一個,使之成為一個最大堆。我們把遍歷過的數組元素對半分到兩個堆里,更大的數放在最小堆,較小的數放在最大堆。同時,確保最大堆的比最小堆大,才能從最大堆的頂端返回。 Problem Numbers keep coming, return the median of numbers at every time a new number a...
閱讀 3436·2023-04-25 18:14
閱讀 1531·2021-11-24 09:38
閱讀 3247·2021-09-22 14:59
閱讀 3064·2021-08-09 13:43
閱讀 2569·2019-08-30 15:54
閱讀 567·2019-08-30 13:06
閱讀 1549·2019-08-30 12:52
閱讀 2721·2019-08-30 11:13