Final Value of Variable After Performing Operations
EASYDescription
There is a programming language with only four operations and one variable X:
++XandX++increments the value of the variableXby1.--XandX--decrements the value of the variableXby1.
Initially, the value of X is 0.
Given an array of strings operations containing a list of operations, return the final value of X after performing all the operations.
Example 1:
Input: operations = ["--X","X++","X++"] Output: 1 Explanation: The operations are performed as follows: Initially, X = 0. --X: X is decremented by 1, X = 0 - 1 = -1. X++: X is incremented by 1, X = -1 + 1 = 0. X++: X is incremented by 1, X = 0 + 1 = 1.
Example 2:
Input: operations = ["++X","++X","X++"] Output: 3 Explanation: The operations are performed as follows: Initially, X = 0. ++X: X is incremented by 1, X = 0 + 1 = 1. ++X: X is incremented by 1, X = 1 + 1 = 2. X++: X is incremented by 1, X = 2 + 1 = 3.
Example 3:
Input: operations = ["X++","++X","--X","X--"] Output: 0 Explanation: The operations are performed as follows: Initially, X = 0. X++: X is incremented by 1, X = 0 + 1 = 1. ++X: X is incremented by 1, X = 1 + 1 = 2. --X: X is decremented by 1, X = 2 - 1 = 1. X--: X is decremented by 1, X = 1 - 1 = 0.
Constraints:
1 <= operations.length <= 100operations[i]will be either"++X","X++","--X", or"X--".
Approaches
Checkout 2 different approaches to solve Final Value of Variable After Performing Operations. Click on different approaches to view the approach and algorithm in detail.
Iteration with Full String Comparison
This is a straightforward approach where we iterate through the array of operations. For each operation, we use full string comparison to determine whether to increment or decrement the variable X.
Algorithm
- Initialize an integer variable,
x, to 0. - Iterate through the
operationsarray from the first to the last element. - For each
operationstring:- If the string is equal to
"++X"or"X++", incrementxby 1. - Otherwise, it must be a decrement operation (
"--X"or"X--"), so decrementxby 1.
- If the string is equal to
- After the loop completes, return the final value of
x.
The core idea is to simulate the process described in the problem. We start with a variable X initialized to 0 and process each operation one by one. We use if-else conditions or a switch statement to match the operation string and update X accordingly.
Here is a code snippet demonstrating this approach:
class Solution {
public int finalValueAfterOperations(String[] operations) {
int x = 0;
for (String op : operations) {
if (op.equals("++X") || op.equals("X++")) {
x++;
} else {
x--;
}
}
return x;
}
}
A switch statement can also be used for clarity:
class Solution {
public int finalValueAfterOperations(String[] operations) {
int x = 0;
for (String op : operations) {
switch (op) {
case "++X":
case "X++":
x++;
break;
case "--X":
case "X--":
x--;
break;
}
}
return x;
}
}
Complexity Analysis
Pros and Cons
- Easy to understand and implement.
- Directly follows the problem statement, making the logic clear.
- Slightly less performant in practice compared to checking a single character due to the overhead of
String.equals()method calls which compare all characters in the string.
Code Solutions
Checking out 4 solutions in different languages for Final Value of Variable After Performing Operations. Click on different languages to view the code.
Video Solution
Watch the video walkthrough for Final Value of Variable After Performing Operations
Similar Questions
5 related questions you might find useful
Data Structures:
Subscribe to Scale Engineer newsletter
Learn about System Design, Software Engineering, and interview experiences every week.
No spam, unsubscribe at any time.