Given a string S of length N consisting of lower case character, the task is to find the minimum cost to reach from index i to index j.
At any index k, the cost to jump to the index k+1 and k-1(without going out of bounds) is 1.
Additionally, the cost to jump to any index m such that S[m] = S[k] is 0.
Input : S = “abcde”, i = 0, j = 4
Output : 4
The shortest path will be:
Thus, the answer will be 4.
Input : S = “abcdefb”, i = 0, j = 5
Output : 2
0->1 edge weight is 1, 1->6 edge weight is 0, and 6->5 edge weight is 1.
Thus, the answer will be 2
- One approach to solve this problem is 0-1 BFS.
- The setup can be visualized as a graph with N nodes.
- All the nodes will be connected to adjacent nodes with an edge of weight of ‘1’ and nodes with the same characters with an edge with weight ‘0’.
- In this setup, 0-1 BFS can be run to find the shortest path from index ‘i’ to index ‘j’.
Time complexity: O(N^2) – As number of vertices would be of O(N^2)
- For each character X, all the characters are found for which it is adjacent to.
- A graph is created with number of nodes as number of distinct characters in the string, each representing a character.
- Each node X, will have an edge of weight 1 with all the nodes representing characters adjacent to character X.
- Then BFS can be run from nodes representing S[i] to nodes representing S[j] in this new graph
Time complexity: O(N)
Below is the implementation of the above approach:
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
- Minimum initial vertices to traverse whole matrix with given conditions
- Minimum Cost using Dijkstra by reducing cost of an Edge
- Transform One String to Another using Minimum Number of Given Operation
- Minimum swaps required to convert one binary string to another
- Minimum number of subsequences required to convert one string to another
- Minimum number of subsequences required to convert one string to another using Greedy Algorithm
- Find length of longest subsequence of one string which is substring of another string
- Count of ways to traverse a Matrix according to given conditions
- Find the minimum number of moves needed to move from one cell of matrix to another
- Maximum length prefix of one string that occurs as subsequence in another
- Print array of strings in sorted order without copying one string into another
- Check if it is possible to convert one string into another with given constraints
- Check if it is possible to transform one string to another
- Map every character of one string to another such that all occurrences are mapped to the same character
- Check if one string can be converted to another
- Check if permutaion of one string can break permutation of another
- Minimum number of given operations required to convert a string to another string
- Split the string into minimum parts such that each part is in the another string
- Find the character in first string that is present at minimum index in second string
- Minimum cost to convert string into palindrome
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.