// Given a string s that contains only digits (0-9) and an integer target, return all possible strings by inserting the binary operator ' + ', ' - ', and/or ' * ' between the digits of s such that the resultant expression evaluates to the target value.
// Note:
// Operands in the returned expressions should not contain leading zeros. For example, 2 + 03 is not allowed whereas 20 + 3 is fine.
// It is allowed to not insert any of the operators.
// Driver code will print the final list of strings in lexicographically smallest order.
// Examples:
// Input: s = "124", target = 9
// Output: ["1+2*4"]
// Explanation: The valid expression that evaluate to 9 is 1 + 2 * 4
// Input: s = "125", target = 7
// Output: ["1*2+5", "12-5"]
// Explanation: The two valid expressions that evaluate to 7 are 1 * 2 + 5 and 12 - 5.
// Input: s = "12", target = 12
// Output: ["12"]
// Explanation: s itself matches the target. No other expressions are possible.
// Input: s = "987612", target = 200
// Output: []
// Explanation: There are no expressions that can be created from "987612" to evaluate to 200.
// Constraints:
// 1 ⤠s.size() ⤠9
// s consists of only digits (0-9).
// -231 ⤠target ⤠231-1
/*
* Problem: Generate all possible expressions by inserting +, -, * operators
* between digits of a string such that the expression evaluates
* to a given target value.
*
* Approach: Backtracking / DFS (Depth-First Search)
*
* Key idea:
* - We recursively try all possible placements of operators between digits.
* - Maintain running evaluation (`currVal`), previous operand (`prevOpr`),
* and the current expression string.
* - Use backtracking to explore all combinations.
*
* Time Complexity: O(4^n) (in worst case, for each position we try 3 operators)
* Space Complexity: O(n) (recursion stack depth)
*/
import java.util.*;
class Solution {
/**
* Recursive helper function to explore all possible expressions.
*
* @param s The numeric string input.
* @param target The target value we want to achieve.
* @param idx Current index in the string.
* @param currVal Current evaluated result of the formed expression so far.
* @param prevOpr The last operand used (to handle multiplication precedence).
* @param expression Current expression string being built.
* @param result List to store valid expressions that match the target.
*/
public void solve(String s, int target, int idx, long currVal, long prevOpr, String expression, ArrayList