# Count of pairs of strings which differ in exactly one position

Given an array **arr[]** of strings of equal lengths. The task is to calculate the total number of pairs of strings which differ in 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 every possible pair, check if both the strings differ in exactly a single index position with a single traversal of the strings.

**Method 2:** Two string can be compared in the following way in order to check whether they differ in a single index position:

Let

str1 = “abc”andstr2 = “adc”

Forstr1, add“#bc”,“a#c”and“ab#”to a set.

Now forstr2, 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.

Notethat “#” 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 count of same pairs ` `def` `pair_count(d): ` ` ` `return` `sum` `((i` `*` `(i` `-` `1` `))` `/` `/` `2` `for` `i ` `in` `d.values()) ` ` ` ` ` `# Function to return total number of strings ` `# which satisfy required condition ` `def` `Difference(array, m): ` ` ` ` ` `# Dictionary changed will store strings ` ` ` `# with wild cards ` ` ` `# Dictionary same will store strings ` ` ` `# that are equal ` ` ` `changed, same ` `=` `{}, {} ` ` ` ` ` `# Iterating for all strings in the given array ` ` ` `for` `s ` `in` `array: ` ` ` ` ` `# If we found the string then increment by 1 ` ` ` `# Else it will get default value 0 ` ` ` `same[s]` `=` `same.get(s, ` `0` `)` `+` `1` ` ` ` ` `# Iterating on a single string ` ` ` `for` `i ` `in` `range` `(m): ` ` ` `# Adding special symbol to the string ` ` ` `t ` `=` `s[:i]` `+` `'#'` `+` `s[i ` `+` `1` `:] ` ` ` ` ` `# Incrementing the string if found ` ` ` `# Else it will get default value 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)) ` |

*chevron_right*

*filter_none*

**Output:**

2

## Recommended Posts:

- Count strings with consonants and vowels at alternate position
- Count pairs of strings that satisfy the given conditions
- Count pairs of non-overlapping palindromic sub-strings of the given string
- Pairs of complete strings in two sets of strings
- Count of strings that become equal to one of the two strings after one removal
- Character pairs from two strings with even sum
- Count characters at same position as in English alphabet
- Pairs of strings which on concatenating contains each character of "string"
- Python Set | Pairs of complete strings in two sets
- Print all pairs of anagrams in a given array of strings
- Minimum number of pairs required to make two strings same
- Total character pairs from two strings, with equal number of set bits in their ascii value
- Count all pairs with given XOR
- Count pairs with given sum
- Count of unique pairs (arr[i], arr[j]) such that i < j

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.