Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package g2001_2100.s2011_final_value_of_variable_after_performing_operations;

// #Easy #Array #String #Simulation #2021_10_26_Time_2_ms_(36.12%)_Space_40.1_MB_(100%)

public class Solution {
public int finalValueAfterOperations(String[] operations) {
int c1 = 0, c2 = 0;
for (int i = 0; i < operations.length; i++) {
if (operations[i].equals("--X") || operations[i].equals("X--")) ++c1;
else ++c2;
}
return c2 - c1;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
2011\. Final Value of Variable After Performing Operations

Easy

There is a programming language with only **four** operations and **one** variable `X`:

* `++X` and `X++` **increments** the value of the variable `X` by `1`.
* `--X` and `X--` **decrements** the value of the variable `X` by `1`.

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 <= 100`
* `operations[i]` will be either `"++X"`, `"X++"`, `"--X"`, or `"X--"`.
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package g2001_2100.s2011_final_value_of_variable_after_performing_operations;

import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.MatcherAssert.assertThat;

import org.junit.jupiter.api.Test;

class SolutionTest {
@Test
void finalValueAfterOperations() {
assertThat(
new Solution().finalValueAfterOperations(new String[] {"--X", "X++", "X++"}),
equalTo(1));
}

@Test
void finalValueAfterOperations2() {
assertThat(
new Solution().finalValueAfterOperations(new String[] {"++X", "++X", "X++"}),
equalTo(3));
}

@Test
void finalValueAfterOperations3() {
assertThat(
new Solution().finalValueAfterOperations(new String[] {"X++", "++X", "--X", "X--"}),
equalTo(0));
}
}