public String toString() {
        Stack<String> stack = new Stack<>();
        StringBuilder full = new StringBuilder();
        Set<Integer> powers = terms.keySet();

        for (int power : powers) {
            StringBuilder sb = new StringBuilder();
            double coefficient = terms.get(power);
            sb.append("+");
            if (Math.abs(coefficient) != 1) {
                sb.append(coefficient);
            }
            if (power == 1) {
                sb.append("x");
            } else if (power > 1) {
                sb.append("x^" + power);
            }
            stack.push(sb.toString());
        }
        while (!(stack.empty())) {
            full.append(stack.pop());
        }
        return full.toString().replace("+-", "-");
    }