Change language

The number of string pairs that differ by exactly one position

|

Examples :

Input: arr [] = {“abc”, “abd”, “bbd”}
Output: 2
(abc, abd) and (abd, bbd) are the only valid pairs.

Input: arr [] = {“Def”, “deg”, “dmf”, “xef”, “dxg”}
Output: 4

Method 1: For each possible pair, check if both strings differ at exactly the same index position with one row traversal.

Method 2: two strings can be compare as follows to check if they differ at the same index position:

Let str1 = “abc” and str2 = “adc”
For str1 , add “#bc” , “a # c” and “ab #” to a set .
Now for str2 , generate the string in the similar manner and if any of the generated string
is already present in the set then both the strings differ in exactly 1 index position.
For example, “a # c” is one of the generated strings.
Note that “#” is used because it will not be a part of any of the original strings.

Below is the implementation of the above approach:

# Python3 implementation of the approach

 
# Function to return the number of identical pairs

def pair_count (d):

return sum ((i * (i - 1 )) / / 2 for i in d.values ​​())

 

  
# Function to return the total number of lines
# that meet the required condition

def Difference (array, m):

 

# Dictionary changed to store strings

# with wildcards

# The dictionary will store strings

  # which are equal

  changed, same = {}, {}

 

# Iterate for all rows in a given array

for s in array:

  

# If we find a line, we increase it by 1

# Otherwise it will default to 0

  same [s] = same.get (s , 0 ) + 1

 

# Iterate one line at a time

for i in range (m):

# Add a special character to the string

t = s [: i ] + ’#’ + s [i + 1 :]

 

# Increase line if found

 

# Otherwise it will default to 0

  changed [t] = changed.get (t , 0 ) + 1

  

  # Return counted pairs - equal pairs

return pair_count (changed) - pair_count (same) * m

  
# Driver code

if __ name__ = = "__ main__" :

n, m = 3 , 3

array = [ "abc" , "abd" , "bbd" ]

print (Difference (array, m))

Exit :

 2 

The number of string pairs that differ by exactly one position Python functions: Questions

The number of string pairs that differ by exactly one position String Variables: Questions

Shop

Best laptop for Fortnite

$

Best laptop for Excel

$

Best laptop for Solidworks

$

Best laptop for Roblox

$

Best computer for crypto mining

$

Best laptop for Sims 4

$

Best laptop for Zoom

$499

Best laptop for Minecraft

$590

Latest questions

NUMPYNUMPY

psycopg2: insert multiple rows with one query

12 answers

NUMPYNUMPY

How to convert Nonetype to int or string?

12 answers

NUMPYNUMPY

How to specify multiple return types using type-hints

12 answers

NUMPYNUMPY

Javascript Error: IPython is not defined in JupyterLab

12 answers

News

Wiki

Python OpenCV | cv2.putText () method

numpy.arctan2 () in Python

Python | os.path.realpath () method

Python OpenCV | cv2.circle () method

Python OpenCV cv2.cvtColor () method

Python - Move item to the end of the list

time.perf_counter () function in Python

Check if one list is a subset of another in Python

Python os.path.join () method