VOOZH about

URL: https://www.geeksforgeeks.org/dsa/maximizing-distance-in-binary-strings/

⇱ Maximizing Distance in Binary Strings - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Maximizing Distance in Binary Strings

Last Updated : 23 Jul, 2025

Given an array of binary strings arr[] of size N (1 <= N <= 103). The task is to find the maximum distance between any pair of these binary strings. The distance between two binary strings is calculated as the sum of their lengths after excluding the common prefix.

Examples:

Input: arr[] = { “01011”, “010110”, “0111”}
Output: 6
Explanation: The strings with the maximum distance between them are “010110” and “0111”, where the common prefix is “01”, Hence, the sum of their lengths after excluding the common prefix: 4 + 2 = 6.

Input: arr[] = {"001", "1", "111111"}
Output: 9
Explanation: The string "001" and "111111" have no common prefix, hence, the sum of their lengths after excluding the common prefix: 3 + 6 = 9.

We need to use of a Trie data structure to efficiently find the maximum distance between binary strings in the given array. We'll store the length of longest suffix from current node till its bottom leaf node. By traversing the Trie, we can find the maximum distance by considering the sum of lengths of non-overlapping substrings of binary strings, resulting in the desired maximum distance calculation. This approach is extremely more efficient than comparing every pair of binary strings directly.

  • Create a TrieNode class with attributes val for storing the length of string after excluding the common prefix, an ending flag, and left and right child pointers.
  • Create a buildTree() function to build a Trie from an array of binary strings (nums).
  • Initialize a pointer cur to the root node and iterate through each binary string in given array arr[].
  • For each character in the binary string:
    • If it's '0', traverse to the left child (0) in the Trie. If the left child doesn't exist, create it and update its val.
    • If it's '1', traverse to the right child (1) in the Trie. If the right child doesn't exist, create it and update its val.
  • Mark the current node as an ending node (isEnding = true)
  • Create a maxDistance() function to calculate the maximum distance between binary strings using the constructed Trie.
  • Create the Trie root node and build the Trie structure using buildTree() by above mentioned steps.
  • Initialize max_distance= 0 and a queue (q) for breadth-first traversal.
  • Start with the root node and push it into the queue.
  • While the queue is not empty:
    • Dequeue the front node cur.
    • If cur has left or right children:
      • If it has both left and right children, calculate the maximum distance as the sum of their val and update max_distance.
    • If cur is an ending node, calculate the maximum distance considering either the left or right child with val and update max_distance.
    • Enqueue the left and right children of cur, if they exist.
  • Return the max_distance.

Below is the implementation of the above approach:


Output
6

Time Complexity: O(N * M), where N is the number of binary strings and M is the average length of the binary strings.
Auxiliary Space: O(N * M)

Comment