Ctrl K

Two Pieces Glued Into a Code

Medium
You are given a list of digit strings pieces and a digit string target. Count the ordered index pairs (i, j) with i ≠ j such that pieces[i] followed by pieces[j] is exactly target. The pairs (i, j) and (j, i) count as different.
Example 1
pieces=["12", "3412", "1234", "12"], target="123412" → 4

Pairs (0, 1) and (3, 1) work: "12" + "3412". Pair (2, 0) also gives "123412" as "1234" + "12", and so does (2, 3). That is 4 in total.

Example 2
pieces=["5", "5"], target="55" → 2

Pairs (0, 1) and (1, 0) count separately, even though they glue identical strings.

Example 3
pieces=["7", "8", "9"], target="98" → 1
Constraints
  • 2 ≤ len(pieces) ≤ 100
  • 1 ≤ len(pieces[i]) ≤ 100
  • 2 ≤ len(target) ≤ 100
  • All strings consist of digits and have no leading zeros
Have you seen this problem in a real interview?
Solved by0
Tried by0
Submissions0
Acceptance—
Topics
Similar problemsMost Searched Words MediumDomain Visit Rollup MediumRanking Finalists by Jury Ballots MediumPalindrome From Two-Letter Cards MediumMost Talkative Sender Medium
loading Python…
Input
pieces=["12", "3412", "1234", "12"], target="123412"
Expected
4

Run checks the first 3 tests in your browser. Submit checks all 112 tests on the server, hidden ones included.