From ceb0f44a095fe6e12d6e2b98d88e61004252ea01 Mon Sep 17 00:00:00 2001 From: N Bhanu Prakash Reddy <32775691+bhanu1131@users.noreply.github.com> Date: Wed, 28 Oct 2020 11:14:16 +0530 Subject: [PATCH] Create 282_Expression_Add_Operators.cpp --- cpp/282_Expression_Add_Operators.cpp | 53 ++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 cpp/282_Expression_Add_Operators.cpp diff --git a/cpp/282_Expression_Add_Operators.cpp b/cpp/282_Expression_Add_Operators.cpp new file mode 100644 index 0000000..2d8b2c9 --- /dev/null +++ b/cpp/282_Expression_Add_Operators.cpp @@ -0,0 +1,53 @@ +class Solution { +public: + vector addOperators(string num, int target) { + vector result; + if (num.size() == 0) return result; + findSolution(num, target, result, "", 0, 0, 0, ' '); + return result; + } + + //DFS algorithm + void findSolution(const string &num, const int target, + vector& result, + string solution, + int idx, + long long val, + long long prev, + char preop ) + { + + if (target == val && idx == num.size()){ + result.push_back(solution); + return; + } + if (idx == num.size()) return; + + string n; + long long v=0; + for(int i=idx; i