FastPrepConstruct Target String From a Character Bag
Problems/Construct Target String From a Character Bag

Construct Target String From a Character Bag

Google logoGoogle● EasyNEW GRADONSITE INTERVIEW
QuestionFunctionExamplesConstraints

Problem statement

Source note: The source directly provides bag and target strings, a construction decision, occurrence-frequency comparison, and a very-large-bag follow-up comparing fixed arrays with hash maps. Lowercase-English encoding, examples, and finite bounds are practice scaffolding. The judged core matches the visible source at about 96%.

Given lowercase English strings bag and target, return whether target can be constructed using the character occurrences in bag.

Each occurrence in bag may be used at most once. Character order does not matter.

Function

canConstructTarget(bag: String, target: String) → boolean

Examples

Example 1

bag = "aabbc"target = "abc"return = true

The bag supplies at least one a, b, and c.

Example 2

bag = "abc"target = "aabc"return = false

The target needs two occurrences of a, but the bag has only one.

Example 3

bag = "zzxy"target = "xyz"return = true

Order is irrelevant; x, y, and one z are available.

Constraints

  • 0 <= bag.length, target.length <= 1000000.
  • Both strings contain only lowercase English letters.

More Google problems

  • Apartment Housemate PreferencesPHONE SCREEN · Seen Nov 2026
  • Price at Query TimestampsPHONE SCREEN · Seen Nov 2026
  • 0/1 Knapsack with Weight and Volume LimitsONSITE INTERVIEW · Seen Nov 2026
All Google interview questionsSee Google hiring insights
Saved
class Solution {
    public boolean canConstructTarget(String bag, String target) {
        // write your code here
    }
}
Checking account…