- Trending Categories
- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP

- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who

# Program to find out the total number of characters to be changed to fix a misspelled word in Python

Suppose we are given a list of cities and a list of roads that connect each other. The list 'cities' contain the name of cities that a tour bus visits in order. In the list 'roads' the roads are listed in a (source, destination) order meaning there is a one-way road from source to destination. Now, there is a problem that some city names in the list 'cities' may be misspelled. We have to correct such misspelled city names by changing the minimum number of characters. We return the number of characters changed as output.

So, if the input is like cities = ["HWH", "DLI", "BGL"], roads = [["HWH", "DLI"],["DLI", "BCT"], ["BCT", "HWH"]], then the output will be 2.

The misspelled city name in cities is 'BGL'. The correct name would be 'BCT'. So, too correct the name in cities we have to change 2 characters.

To solve this, we will follow these steps −

- Define a function diff() . This will take a, b
- return total difference in characters between a and b

- size := size of cities
- arr := a new map
- junctions := a new set from each source city in roads
- for each j in junctions, do
- arr[j] := diff(cities[0], j)

- for i in range 1 to size, do
- nxt := a new map
- for each r1, r2 in roads, do
- if r1 in present in arr, then
- cost := arr[r1] + diff(cities[i], r2)
- if r2 is not present in nxt or cost < nxt[r2], then
- nxt[r2] := cost

- if r1 in present in arr, then
- arr := nxt

- return minimum of all values in arr

## Example

Let us see the following implementation to get better understanding −

def diff(a, b): return sum(x != y for x, y in zip(a, b)) def solve(cities, roads): size = len(cities) arr = dict() junctions = set(r[0] for r in roads) for j in junctions: arr[j] = diff(cities[0], j) for i in range(1, size): nxt = dict() for r1, r2 in roads: if r1 in arr: cost = arr[r1] + diff(cities[i], r2) if r2 not in nxt or cost < nxt[r2]: nxt[r2] = cost arr = nxt return min(arr.values()) print(solve(["HWH", "DLI", "BGL"], [["HWH", "DLI"],["DLI", "BCT"], ["BCT", "HWH"]]))

## Input

["HWH", "DLI", "BGL"], [["HWH", "DLI"],["DLI", "BCT"], ["BCT", "HWH"]]

## Output

2

- Related Questions & Answers
- Program to Find Out the Number of Corrections to be Done to Fix an Equation in Python
- C++ Program to find out the total price
- Program to find total number of strings, that contains one unique characters in Python
- Program to find out the number of boxes to be put into the godown in Python
- Program to find out number of blocks that can be covered in Python
- Python Program to find out the number of rooms in which a prize can be hidden
- Program to find minimum number of characters to be added to make it palindrome in Python
- Program to find out the number of accepted invitations in Python
- Program to Find Out the Number of Squares in a Grid in Python
- How to use Python Pandas to find the total number of count for more than one special characters present in each word in a given series?
- Find out the minimum number of coins required to pay total amount in C++
- Program to find out the number of pairs of equal substrings in Python
- C++ program to find out the maximum number of cells that can be illuminated
- C++ program to find out the number of coordinate pairs that can be made
- Python Program to find out the price of a product after a number of days