
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Program to find out if the strings supplied differ by a character in the same position in Python
Suppose, we are provided an array that contains several strings that are of the same length. We have to find out if any two of the supplied strings differ by a single character at the same position. If this difference is present, we return true or else we return false.
So, if the input is like dict = ['pqrs', 'prqs', 'paqs'], then the output will be True. The output produced is True because the strings listed in the input all differ in index 1. So, if any two pairs are taken, there is a difference in the same position.
To solve this, we will follow these steps −
seens := a new set
for each word in dict, do
for each index i and character c in word, do
masked_word := word[from index 0 to i] + '.' + word[from index i+1 to end of string]
if masked_word is present in seens, then
return True
otherwise,
add(masked_word) to seens
return False
Example (Python)
Let us see the following implementation to get better understanding −
def solve(dict): seens = set() for word in dict: for i, c in enumerate(word): masked_word = word[:i] + '.' + word[i+1:] if masked_word in seens: return True else: seens.add(masked_word) return False print(solve(['pqrs', 'prqs', 'paqs']))
Input
['pqrs', 'prqs', 'paqs']
Output
True
- Related Articles
- Program to Find Out the Strings of the Same Size in Python
- Python program to find the character position of Kth word from a list of strings
- Program to count substrings that differ by one character in Python
- Program to find out if the graph is traversable by everybody in Python
- Program to find out the position of a ball after n reversals in Python
- C++ Program to find out the health of a character
- Python – Sort by Rear Character in Strings List
- Program to equal two strings of same length by swapping characters in Python
- Program to find the indexes where the substrings of a string match with another string fully or differ at one position in python
- Program to find out if we win in a game in Python
- Program to find out is a point is reachable from the current position through given points in Python
- Program to find out the minimum number of moves for a chess piece to reach every position in Python
- Program to find the number of possible position in a line in Python
- Program to Find Out the Minimal Submatrices in Python
- Program to Find Out the Special Nodes in a Tree in Python
