Given a string in the form of an equation i.e A + B + C – D = E where A, B, C, D and E are integers and -, + and = are operators. The task is to print Valid if the equation is valid else print Invalid.
Note: String only comprises of the characters from the set {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, +, -, =}.
Examples:
Input: str = “1+1+1+1=7”
Output: InvalidInput: str = “12+13-14+1=12”
Output: Valid
Approach:
- Traverse the string and store all the operands in an array operands[] and all the operators in an array operators[].
- Now perform the arithmetic operation stored in operators[0] on operands[0] and operands[1] and store it in ans.
- Then perform the seconds arithmetic operation i.e. operators[1] on ans and operators[2] and so on.
- Finally, compare the ans calculated with the last operand i.e. operands[4]. If they’re equal then print Valid else print Invalid.
Below is the implementation of the above approach:
C++
// C++ implementation of the approach #include <bits/stdc++.h> using namespace std; // Function that returns true if the equation is valid bool isValid(string str) { int k = 0; string operands[5] = ""; char operators[4]; long ans = 0, ans1 = 0, ans2 = 0; for (int i = 0; i < str.length(); i++) { // If it is an integer then add it to another string array if (str[i] != '+' && str[i] != '=' && str[i] != '-') operands[k] += str[i]; else { operators[k] = str[i]; // Evaluation of 1st operator if (k == 1) { if (operators[k - 1] == '+') ans += stol(operands[k - 1]) + stol(operands[k]); if (operators[k - 1] == '-') ans += stol(operands[k - 1]) - stol(operands[k]); } // Evaluation of 2nd operator if (k == 2) { if (operators[k - 1] == '+') ans1 += ans + stol(operands[k]); if (operators[k - 1] == '-') ans1 -= ans - stol(operands[k]); } // Evaluation of 3rd operator if (k == 3) { if (operators[k - 1] == '+') ans2 += ans1 + stol(operands[k]); if (operators[k - 1] == '-') ans2 -= ans1 - stol(operands[k]); } k++; } } // If the LHS result is equal to the RHS if (ans2 == stol(operands[4])) return true; else return false; } // Driver code int main() { string str = "2+5+3+1=11"; if (isValid(str)) cout << "Valid"; else cout << "Invalid"; return 0; } |
Python3
# Python3 implementation of the approach # Function that returns true if # the equation is valid def isValid(string) : k = 0; operands = [""] * 5 ; operators = [""] * 4 ; ans = 0 ; ans1 = 0; ans2 = 0; for i in range(len(string)) : # If it is an integer then add # it to another string array if (string[i] != '+' and string[i] != '=' and string[i] != '-') : operands[k] += string[i]; else : operators[k] = string[i]; # Evaluation of 1st operator if (k == 1) : if (operators[k - 1] == '+') : ans += int(operands[k - 1]) + int(operands[k]); if (operators[k - 1] == '-') : ans += int(operands[k - 1]) - int(operands[k]); # Evaluation of 2nd operator if (k == 2) : if (operators[k - 1] == '+') : ans1 += ans + int(operands[k]); if (operators[k - 1] == '-') : ans1 -= ans - int(operands[k]); # Evaluation of 3rd operator if (k == 3) : if (operators[k - 1] == '+') : ans2 += ans1 + int(operands[k]); if (operators[k - 1] == '-') : ans2 -= ans1 - int(operands[k]); k += 1 # If the LHS result is equal to the RHS if (ans2 == int(operands[4])) : return True; else : return False; # Driver code if __name__ == "__main__" : string = "2 + 5 + 3 + 1 = 11"; if (isValid(string)) : print("Valid"); else : print("Invalid"); # This code is contributed by Ryuga |
Valid
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Program for credit card number validation
- Find the missing value from the given equation a + b = c
- Reversing an Equation
- Number of ordered points pair satisfying line equation
- Given a string and an integer k, find the kth sub-string when all the sub-strings are sorted according to the given condition
- Restore original String from given Encrypted String by the given operations
- Check if a string can become empty by recursively deleting a given sub-string
- String containing first letter of every word in a given string with spaces
- Check if a given string is sum-string
- Lexicographically smallest string whose hamming distance from given string is exactly K
- Find if a string starts and ends with another given string
- Lexicographically smallest string formed by appending a character from the first K characters of a given string
- Find a palindromic string B such that given String A is a subsequense of B
- Minimum length of string having all permutation of given string.
- Count occurrences of a string that can be constructed from another given string
- Minimum number of given operations required to convert a string to another string
- Print the longest prefix of the given string which is also the suffix of the same string
- Queries to find the last non-repeating character in the sub-string of a given string
- Rearrange given string to maximize the occurrence of string t
- Longest string in an array which matches with prefix of the given string
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.
Improved By : AnkitRai01

