> For the complete documentation index, see [llms.txt](https://luj.gitbook.io/code/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://luj.gitbook.io/code/string/7-evaluation/solve-the-equation.md).

# Solve the Equation

Solve a given equation and return the value of`x`in the form of string "x=#value". The equation contains only '+', '-' operation, the variable`x`and its coefficient.

* If there is no solution for the equation, return "No solution".
* If there are infinite solutions for the equation, return "Infinite solutions".
* If there is exactly one solution for the equation, we ensure that the value of`x`is an integer.

## Example

**Example 1:**

```
Input:
 "x+5-3+x=6+x-2"

Output:
 "x=2"
```

**Example 2:**

```
Input:
 "x=x"

Output:
 "Infinite solutions"
```

**Example 3:**

```
Input:
 "2x=x"

Output:
 "x=0"
```

**Example 4:**

```
Input:
 "2x+3x-6x=x+2"

Output:
 "x=-1"
```

**Example 5:**

```
Input:
 "x=x+2"

Output:
 "No solution"
```

## Note

```
按特定匹配条件split
String[] tokens = s.split("(?=[+-])");
```

See Comments

## Code

```java
class Solution {
    /*
    x +5 -2x = 6 -3x;
    leftPart : tokens= { x, +5, -2x}; coefficient for x = 1-2 =-1; constant = 5;
    rightPart: tokens= {6, -3x}; coefficient for x = -3; constant = 6;
    final result = (6-5)/ (-1 - (-3))
    */
    public String solveEquation(String equation) {
        String[] parts = equation.split("=");
        int[] leftPart = evaluate(parts[0]);
        int[] rightPart = evaluate(parts[1]);
        if (leftPart[0] == rightPart[0] &&
            leftPart[1] == rightPart[1]) {
            return "Infinite solutions";
        } else if (leftPart[0] == rightPart[0]){
            return "No solution";
        }
        return "x="+ (rightPart[1] - leftPart[1]) / (leftPart[0] - rightPart[0]);
    }

    private int[] evaluate(String s) {
        String[] tokens = s.split("(?=[+-])");  
        // ()for match group; ?= for match and include in res; [+-] means + or -;
        int[] res = new int[2];
        for(String token : tokens) {
            if (token.equals("+x") || token.equals("x")) { 
                res[0]++; // x means 1x
            } else if (token.equals("-x")) {
                res[0]--;// -x means -1x
            } else if (token.contains("x")) { // +kx or -kx to get k
                res[0] += Integer.parseInt(token.substring(0, token.length() - 1)); 
            } else {
                res[1] += Integer.parseInt(token);
            }
        }

        return res;
    }
}
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://luj.gitbook.io/code/string/7-evaluation/solve-the-equation.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
