Final Value of Variable After Performing Operations (LeetCode 2011): Solution and Explanation
A variable X starts at 0. You get a list of operations, each one of ++X, X++, --X or X--. The first two add 1 and the last two subtract 1. Return the value of X after applying every operation in order.
Intuition
The prefix and postfix forms look different, but in this problem nothing reads the value of the expression. Each operation is a statement on its own, so ++X and X++ have the same effect: X goes up by one. The same holds for the two decrement forms.
That leaves only one question per operation: plus or minus? Every string is exactly three characters, and whether X sits at the front or the back, the middle character is always a sign. So op[1] alone tells you the direction.
From there the solution is a counter. Start at 0, add one for each + in the middle, subtract one for each -, and return the counter.
Approach: Count by the middle character
- Set
x = 0. - For each operation
op, look atop[1]. - If it is
+, add 1 tox; otherwise subtract 1. - Return
xafter the loop.
Step-by-step example
operations = ["--X","X++","X++"]
- Start with
x = 0. "--X": the middle character is-, soxbecomes -1."X++": the middle character is+, soxbecomes 0."X++": the middle character is+again, soxbecomes 1.- All operations are applied, so the answer is 1.
Code: Python, Java, C++
1class Solution:
2 def finalValueAfterOperations(self, operations: List[str]) -> int:
3 x = 0
4 for op in operations:
5 if op[1] == '+':
6 x += 1
7 else:
8 x -= 1
9 return x1class Solution {
2 public int finalValueAfterOperations(String[] operations) {
3 int x = 0;
4 for (String op : operations) {
5 if (op.charAt(1) == '+') {
6 x++;
7 } else {
8 x--;
9 }
10 }
11 return x;
12 }
13}1class Solution {
2public:
3 int finalValueAfterOperations(vector<string>& operations) {
4 int x = 0;
5 for (const string& op : operations) {
6 if (op[1] == '+') {
7 x++;
8 } else {
9 x--;
10 }
11 }
12 return x;
13 }
14};Time and space complexity
O(n)
Each of the n operations is looked at once, and reading one character is constant work.
O(1)
Only the counter is stored.
Other approaches
Compare the whole string (time O(n), space O(1))
Check op == "++X" or op == "X++" to decide on an increment. It is just as fast and arguably more explicit, but it is longer and easier to mistype than one character check.
Count the plus signs (time O(n), space O(1))
Count how many operations contain a +, call it p, and return p - (n - p), which is 2p - n. It is the same idea written as a formula.
Common mistakes
- Checking
op[0]instead ofop[1]. ForX++andX--the first character is X, so you would treat every postfix operation the same way. - Overthinking prefix versus postfix. Nothing uses the value of the expression, so both forms change X by exactly one.
- Starting X at something other than 0, or returning the number of operations instead of the value.
Frequently asked questions
Why is the middle character enough?
Every operation is three characters long, and X is either the first or the last one. The middle character is therefore always a sign, and it is the same sign as the operation's direction.
Does the difference between ++X and X++ matter here?
No. That difference only shows up when the expression's value is used in a larger expression. Here each operation is a standalone statement, so both add exactly one.
What is the time complexity?
O(n) time for n operations and O(1) extra space, since you only keep a single integer counter.