FastPrepConstruct Target String From a Character Bag

Construct Target String From a Character Bag

Google logoGoogle● EasyNEW GRADONSITE INTERVIEW
Learn

Problem statement

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

See Google hiring insights
public boolean canConstructTarget(String bag, String target) {
    // write your code here
}
bag"aabbc"
target"abc"
expectedtrue
Checking account…