Loading content...
You are given two strings: targetText and sourcePool. Your task is to determine whether it is possible to construct the targetText string using only the characters available in the sourcePool string.
Each character in the sourcePool can only be consumed once when building the targetText. In other words, if the targetText requires a particular letter multiple times, the sourcePool must contain at least that many occurrences of that letter.
Return true if it is possible to fully construct the targetText from the characters in sourcePool, and false otherwise.
Key Insight: Think of this as checking whether one multiset of characters is a subset of another multiset. The frequency of every character in targetText must be less than or equal to its frequency in sourcePool.
targetText = "a"
sourcePool = "b"falseThe targetText requires the letter 'a', but the sourcePool only contains 'b'. Since 'a' is not available in the sourcePool, the targetText cannot be constructed.
targetText = "aa"
sourcePool = "ab"falseThe targetText requires two 'a' characters, but the sourcePool only contains one 'a' and one 'b'. There are insufficient 'a' characters to build the targetText.
targetText = "aa"
sourcePool = "aab"trueThe targetText needs two 'a' characters. The sourcePool contains two 'a' letters and one 'b'. Since there are enough 'a' characters available (we consume both 'a' from the pool), the targetText can be successfully constructed.
Constraints