filmov
tv
2976. Minimum Cost to Convert String I using Java | 27 July 2024 | Dynamic Programming |

Показать описание
LeetCode Problem of the day
27 July 2024
Problem No [2976] : Minimum Cost to Convert String I using Java
Level : Medium
Topic : Graph | Dynamic Programming | Shortest Path
------------------------------------------------
You may also explore my another playlists :
👉Java Tutorial :
👉 Geeks-for-Geeks Problem :
👉 Coding ninjas Easy coding problem :
----------------------------------------------------------------
⏩ Stay connected with me on social media for more updates and behind-the-scenes content !
You can find me on 🔍 :
📱Instagram Profile :
🌐 LinkedIn Profile :
-----------------------------------------------------
✅ Description about the problem as follows :
You are given two 0-indexed strings source and target, both of length n and consisting of lowercase English letters. You are also given two 0-indexed character arrays original and changed, and an integer array cost, where cost[i] represents the cost of changing the character original[i] to the character changed[i].
You start with the string source. In one operation, you can pick a character x from the string and change it to the character y at a cost of z if there exists any index j such that cost[j] == z, original[j] == x, and changed[j] == y.
Return the minimum cost to convert the string source to the string target using any number of operations. If it is impossible to convert source to target, return -1.
Note that there may exist indices i, j such that original[j] == original[i] and changed[j] == changed[i].
Example 1:
Input: source = "abcd", target = "acbe", original = ["a","b","c","c","e","d"], changed = ["b","c","b","e","b","e"], cost = [2,5,5,1,2,20]
Output: 28
Explanation: To convert the string "abcd" to string "acbe":
- Change value at index 1 from 'b' to 'c' at a cost of 5.
- Change value at index 2 from 'c' to 'e' at a cost of 1.
- Change value at index 2 from 'e' to 'b' at a cost of 2.
- Change value at index 3 from 'd' to 'e' at a cost of 20.
The total cost incurred is 5 + 1 + 2 + 20 = 28.
It can be shown that this is the minimum possible cost.
Example 2:
Input: source = "aaaa", target = "bbbb", original = ["a","c"], changed = ["c","b"], cost = [1,2]
Output: 12
Explanation: To change the character 'a' to 'b' change the character 'a' to 'c' at a cost of 1, followed by changing the character 'c' to 'b' at a cost of 2, for a total cost of 1 + 2 = 3. To change all occurrences of 'a' to 'b', a total cost of 3 * 4 = 12 is incurred.
Example 3:
Input: source = "abcd", target = "abce", original = ["a"], changed = ["e"], cost = [10000]
Output: -1
Explanation: It is impossible to convert source to target because the value at index 3 cannot be changed from 'd' to 'e'.
----------------------------------------------
🔔 If you enjoyed the content, please subscribe to the channel for more updates! 📺
👍 If you found this video helpful, don't forget to give it a thumbs up and share it with your friends! 📤
🙏 Thank you for watching the video! 😊
------------------------------------------------
📚 Check out other problems! 🔍:
🧩1. Partitions with Given Difference | Java | GeekforGeeks | Practice Problem
🧩2. K-Palindrom | Java | GeeksforGeeks | Practice Problems
🧩3. [ Part-02 ] Maximum Score Words Formed by Letters - Leetcode
🧩4. Mobile Numeric Keypad :
🧩5. Summed Matrix :
🧩6. Print Bracket Numbers :
🧩 7. Toeplitz matrix :
🧩 8. 350. Intersection of Two Arrays II :
🧩 9. 1701. Average Waiting Time :
------------------------------------------------
#leetcode | #leetcodeproblemoftheday | #leetcodeproblems | #leetcodesolutions | #Minimumcosttoconvertstringiusingjava
#2976minimumcosttoconvertstringi | #minimumcosttoconvertstringiproblemsolution | #minimumcosttoconvertstringileetcodeproblemsolution | #minimumcosttoconvertstringileetcodesolutionusingjava
27 July 2024
Problem No [2976] : Minimum Cost to Convert String I using Java
Level : Medium
Topic : Graph | Dynamic Programming | Shortest Path
------------------------------------------------
You may also explore my another playlists :
👉Java Tutorial :
👉 Geeks-for-Geeks Problem :
👉 Coding ninjas Easy coding problem :
----------------------------------------------------------------
⏩ Stay connected with me on social media for more updates and behind-the-scenes content !
You can find me on 🔍 :
📱Instagram Profile :
🌐 LinkedIn Profile :
-----------------------------------------------------
✅ Description about the problem as follows :
You are given two 0-indexed strings source and target, both of length n and consisting of lowercase English letters. You are also given two 0-indexed character arrays original and changed, and an integer array cost, where cost[i] represents the cost of changing the character original[i] to the character changed[i].
You start with the string source. In one operation, you can pick a character x from the string and change it to the character y at a cost of z if there exists any index j such that cost[j] == z, original[j] == x, and changed[j] == y.
Return the minimum cost to convert the string source to the string target using any number of operations. If it is impossible to convert source to target, return -1.
Note that there may exist indices i, j such that original[j] == original[i] and changed[j] == changed[i].
Example 1:
Input: source = "abcd", target = "acbe", original = ["a","b","c","c","e","d"], changed = ["b","c","b","e","b","e"], cost = [2,5,5,1,2,20]
Output: 28
Explanation: To convert the string "abcd" to string "acbe":
- Change value at index 1 from 'b' to 'c' at a cost of 5.
- Change value at index 2 from 'c' to 'e' at a cost of 1.
- Change value at index 2 from 'e' to 'b' at a cost of 2.
- Change value at index 3 from 'd' to 'e' at a cost of 20.
The total cost incurred is 5 + 1 + 2 + 20 = 28.
It can be shown that this is the minimum possible cost.
Example 2:
Input: source = "aaaa", target = "bbbb", original = ["a","c"], changed = ["c","b"], cost = [1,2]
Output: 12
Explanation: To change the character 'a' to 'b' change the character 'a' to 'c' at a cost of 1, followed by changing the character 'c' to 'b' at a cost of 2, for a total cost of 1 + 2 = 3. To change all occurrences of 'a' to 'b', a total cost of 3 * 4 = 12 is incurred.
Example 3:
Input: source = "abcd", target = "abce", original = ["a"], changed = ["e"], cost = [10000]
Output: -1
Explanation: It is impossible to convert source to target because the value at index 3 cannot be changed from 'd' to 'e'.
----------------------------------------------
🔔 If you enjoyed the content, please subscribe to the channel for more updates! 📺
👍 If you found this video helpful, don't forget to give it a thumbs up and share it with your friends! 📤
🙏 Thank you for watching the video! 😊
------------------------------------------------
📚 Check out other problems! 🔍:
🧩1. Partitions with Given Difference | Java | GeekforGeeks | Practice Problem
🧩2. K-Palindrom | Java | GeeksforGeeks | Practice Problems
🧩3. [ Part-02 ] Maximum Score Words Formed by Letters - Leetcode
🧩4. Mobile Numeric Keypad :
🧩5. Summed Matrix :
🧩6. Print Bracket Numbers :
🧩 7. Toeplitz matrix :
🧩 8. 350. Intersection of Two Arrays II :
🧩 9. 1701. Average Waiting Time :
------------------------------------------------
#leetcode | #leetcodeproblemoftheday | #leetcodeproblems | #leetcodesolutions | #Minimumcosttoconvertstringiusingjava
#2976minimumcosttoconvertstringi | #minimumcosttoconvertstringiproblemsolution | #minimumcosttoconvertstringileetcodeproblemsolution | #minimumcosttoconvertstringileetcodesolutionusingjava