Count pairs of equal-length digit strings that differ in exactly one position

Quick Overview

Count the index pairs of numeric strings that have the same length and differ in exactly one digit position, with up to ten thousand strings of up to 100 digits. It tests grouping by a derived key to avoid a quadratic pairwise scan, and careful handling of identical and repeated strings.

Count pairs of equal-length digit strings that differ in exactly one position

Role: Software Engineer

Category: Coding & Algorithms

Difficulty: medium

Interview Round: Other

Given an array of numeric strings `nums`, count the pairs `(i, j)` with `i < j` such that `nums[i]` and `nums[j]` have the same length and differ in exactly one digit. Two strings differ in exactly one digit when there is exactly one index at which their characters differ and every other character is identical. ### Function Signature ```python def count_one_digit_pairs(nums: list[str]) -> int: ``` ### Rules - Pairs are counted by index, so a string that appears several times in `nums` forms a separate pair with each matching partner. - Identical strings differ in zero positions and never count. Strings of different lengths never count. - The strings are compared character by character. Leading zeros are ordinary digits, so `"012"` and `"112"` differ in one position. ### Constraints - `2 <= len(nums) <= 10^4` - `1 <= len(nums[k]) <= 100` - `nums[k]` consists only of the digits `'0'` to `'9'`. ### Examples ```text Input: nums = ["123", "124", "133", "12", "13"] Output: 3 ``` Among the length-3 strings, "123" and "124" differ only at index 2, "123" and "133" differ only at index 1, and "124" and "133" differ at indices 1 and 2, so that group gives 2. Among the length-2 strings, "12" and "13" differ only at index 1, which adds 1. ```text Input: nums = ["11", "11", "11"] Output: 0 ``` All three strings are identical, so every pair differs in zero positions. ```text Input: nums = ["12", "12", "13", "22"] Output: 4 ``` The valid pairs are (0, 2), (0, 3), (1, 2) and (1, 3). The two copies of "12" each pair with "13" and with "22", while "13" and "22" differ in both positions and the two copies of "12" are identical.

Overview: Count the index pairs of numeric strings that have the same length and differ in exactly one digit position, with up to ten thousand strings of up to 100 digits. It tests grouping by a derived key to avoid a quadratic pairwise scan, and careful handling of identical and repeated strings.

PracHub logo
General
Nov 24, 2026
mediumSoftware EngineerOtherCoding & Algorithms
0
0

Given an array of numeric strings nums, count the pairs (i, j) with i < j such that nums[i] and nums[j] have the same length and differ in exactly one digit.

Two strings differ in exactly one digit when there is exactly one index at which their characters differ and every other character is identical.

Function Signature

def count_one_digit_pairs(nums: list[str]) -> int:

Rules

  • Pairs are counted by index, so a string that appears several times in nums forms a separate pair with each matching partner.
  • Identical strings differ in zero positions and never count. Strings of different lengths never count.
  • The strings are compared character by character. Leading zeros are ordinary digits, so "012" and "112" differ in one position.

Constraints

  • 2 <= len(nums) <= 10^4
  • 1 <= len(nums[k]) <= 100
  • nums[k] consists only of the digits '0' to '9' .

Examples

Input: nums = ["123", "124", "133", "12", "13"]
Output: 3

Among the length-3 strings, "123" and "124" differ only at index 2, "123" and "133" differ only at index 1, and "124" and "133" differ at indices 1 and 2, so that group gives 2. Among the length-2 strings, "12" and "13" differ only at index 1, which adds 1.

Input: nums = ["11", "11", "11"]
Output: 0

All three strings are identical, so every pair differs in zero positions.

Input: nums = ["12", "12", "13", "22"]
Output: 4

The valid pairs are (0, 2), (0, 3), (1, 2) and (1, 3). The two copies of "12" each pair with "13" and with "22", while "13" and "22" differ in both positions and the two copies of "12" are identical.