Skip to main content
Back to problems
#1764
Medium Algorithms

Form array by concatenating subarrays of another array

Array Two Pointers Greedy String Matching
54.7% acceptance
Feb 25, 2026
351
45
You are given a 2D integer array groups of length n. You are also given an integer array nums. You are asked if you can choose n disjoint subarrays from the array nums such that the ith subarray is equal to groups[i]. Return true if you can do this task, and false otherwise.

Solution

Rust
Time O(1)
Space O(1)
LeetCode
solution.rs
impl Solution {
  pub fn can_choose(groups: Vec<Vec<i32>>, nums: Vec<i32>) -> bool {
    let mut pos = 0;
    'outer: for group in &groups {
      let glen = group.len();
      while pos + glen <= nums.len() {
        if nums[pos..pos + glen] == group[..] {
          pos += glen;
          continue 'outer;
        }
        pos += 1;
      }
      return false;
    }
    true
  }
}