Find minimum value expression by inserting addition or multiplication operator between digits of given number
Last Updated :
26 Oct, 2021
Given string str that contains only digits, the task is to return an expression by inserting the ‘+’ or ‘*’ operator between every two digits such that the arithmetic value of the expression is minimized.
Example:
Input: str = “322”
Output: “3+2*2”
Explanation: The value of above expression is 7 which is minimum possible over the other expressions “3+2+2”, “3*2+2”, “3*3*2”
Input: str = “391118571”
Output: “3+9*1*1*1+8+5+7*1”
Approach: The given problem can be solved using a greedy approach. Follow the steps below to solve the problem:
- If the string str contains character ‘0’ then:
- Insert multiplication operator between every character of the string
- Else create a string ans to store the iterate the string and if the current character str[i] is:
- ‘1’, then Insert multiplication operator ‘*’ between the current and previous character
- not ‘1’ then Insert addition operator ‘+’ between the current and previous character
Below is the implementation of the above approach:
C++
#include <bits/stdc++.h>
using namespace std;
string minimalExpression(string str)
{
string ans = "" ;
ans.push_back(str[0]);
bool iszero = false ;
for ( int i = 0; i < str.size();
i++) {
if (str[i] == '0' ) {
iszero = true ;
}
}
if (iszero) {
for ( int i = 1; i < str.size();
i++) {
ans.push_back( '*' );
ans.push_back(str[i]);
}
return ans;
}
for ( int i = 1; i < str.size(); i++) {
if (str[i] == '1' ) {
ans.push_back( '*' );
ans.push_back(str[i]);
}
else {
ans.push_back( '+' );
ans.push_back(str[i]);
}
}
return ans;
}
int main()
{
string str = "391118571" ;
cout << minimalExpression(str);
}
|
Java
class GFG {
public static String minimalExpression(String str) {
String ans = "" ;
ans = ans + str.charAt( 0 );
boolean iszero = false ;
for ( int i = 0 ; i < str.length(); i++) {
if (str.charAt(i) == '0' ) {
iszero = true ;
}
}
if (iszero) {
for ( int i = 1 ; i < str.length(); i++) {
ans += '*' ;
ans += str.charAt(i);
}
return ans;
}
for ( int i = 1 ; i < str.length(); i++) {
if (str.charAt(i) == '1' ) {
ans += '*' ;
ans += str.charAt(i);
}
else {
ans += '+' ;
ans += str.charAt(i);
}
}
return ans;
}
public static void main(String args[]) {
String str = "391118571" ;
System.out.println(minimalExpression(str));
}
}
|
Python3
def minimalExpression( str ):
ans = ""
ans + = str [ 0 ]
iszero = False
for i in range ( 0 , len ( str )):
if ( str [i] = = '0' ):
iszero = True
if (iszero):
for i in range ( 1 , len ( str )):
ans + = '*'
ans + = str [i]
return ans
for i in range ( 1 , len ( str )):
if ( str [i] = = '1' ):
ans + = '*'
ans + = str [i]
else :
ans + = '+'
ans + = str [i]
return ans
if __name__ = = "__main__" :
str = "391118571"
print (minimalExpression( str ))
|
C#
using System;
class GFG {
static string minimalExpression( string str)
{
string ans = "" ;
ans += str[0];
bool iszero = false ;
for ( int i = 0; i < str.Length; i++) {
if (str[i] == '0' ) {
iszero = true ;
}
}
if (iszero) {
for ( int i = 1; i < str.Length; i++) {
ans += '*' ;
ans += (str[i]);
}
return ans;
}
for ( int i = 1; i < str.Length; i++) {
if (str[i] == '1' ) {
ans += '*' ;
ans += (str[i]);
}
else {
ans += '+' ;
ans += (str[i]);
}
}
return ans;
}
public static void Main()
{
string str = "391118571" ;
Console.WriteLine(minimalExpression(str));
}
}
|
Javascript
<script>
function minimalExpression(str) {
let ans = [];
ans.push(str[0]);
let iszero = false ;
for (let i = 0; i < str.length; i++) {
if (str[i] == "0" ) {
iszero = true ;
}
}
if (iszero) {
for (let i = 1; i < str.length; i++) {
ans.push( "*" );
ans.push(str[i]);
}
return ans;
}
for (let i = 1; i < str.length; i++) {
if (str[i] == "1" ) {
ans.push( "*" );
ans.push(str[i]);
}
else {
ans.push( "+" );
ans.push(str[i]);
}
}
return ans.join( "" );
}
let str = "391118571" ;
document.write(minimalExpression(str));
</script>
|
Time Complexity: O(N)
Auxiliary Space: O(N), where N is the length of the given string
Share your thoughts in the comments
Please Login to comment...