Similar Problems

Similar Problems not available

Final Value Of Variable After Performing Operations - Leetcode Solution

Companies:

LeetCode:  Final Value Of Variable After Performing Operations Leetcode Solution

Difficulty: Easy

Topics: string array simulation  

Problem Statement: You are given a string s that consists of only digits. You are also given four integers a, b, c, and d. You can perform any number of operations on the string s where each operation is described as follows:

  • You can replace any digit in the string s with another digit from 0 to 9.
  • You can apply the following operations to the string s any number of times:
    • Replace the substring "a" with the substring "b".
    • Replace the substring "b" with the substring "c".
    • Replace the substring "c" with the substring "d". Return the final value of s after performing the operations described above.

Example: Input: s = "125", a = 1, b = 2, c = 3, d = 4 Output: "24" Explanation: Operation 1: replace the digit 1 with the digit 2 -> "225". Operation 2: replace the substring "2" with "3" -> "335". Operation 3: replace the substring "3" with "4" -> "44". No more operations can be performed.

Solution: To solve this problem, we need to perform two types of operations on the given string s:

  1. Replace a digit in s with another digit from 0 to 9.
  2. Apply the given substitutions to the given string s.

We can perform operations 1 and 2 in any order and any number of times until we cannot perform any operations anymore. Therefore, we can use a while loop to perform operations on the string s until the string does not change after a complete iteration of operations.

Algorithm:

  1. Initialize a variable flag to True and s_new to s.
  2. While flag is True: a. Set flag to False. b. Replace any digit in s_new with another digit from 0 to 9. c. Replace the substring "a" with the substring "b". d. Replace the substring "b" with the substring "c". e. Replace the substring "c" with the substring "d". f. If s_new is not equal to s, set s to s_new and set flag to True.
  3. Return the final value of s.

Let's implement the above algorithm in Python:

class Solution: def maximumValue(self, s: str, a: int, b: int, c: int, d: int) -> str:

    flag = True
    
    while flag:
        flag = False
        s_new = s
        
        for i in range(len(s_new)):
            for j in range(10):
                s_temp = s_new[:i] + str(j) + s_new[i+1:]
                if s_temp != s_new:
                    s_new = s_temp
                    flag = True
        
        s_new = s_new.replace(str(a), str(b))
        s_new = s_new.replace(str(b), str(c))
        s_new = s_new.replace(str(c), str(d))
        
        if s_new != s:
            s = s_new
            flag = True
    
    return s

Time Complexity: The time complexity of the above algorithm is O(n^2) where n is the length of the given string s. This is because the algorithm consists of two nested loops, one for iterating over the characters of the string s, and one for iterating over digits from 0 to 9. In the worst case, we will perform n iterations of the loop for iterating over the characters of the string s, and perform 10 iterations of the loop for iterating over digits from 0 to 9. Therefore, the time complexity of the algorithm is O(n^2).

Final Value Of Variable After Performing Operations Solution Code

1