diff --git a/String Algorithms/Anagram/Anagram.cpp b/String Algorithms/Anagram/Anagram.cpp new file mode 100644 index 0000000..94f500f --- /dev/null +++ b/String Algorithms/Anagram/Anagram.cpp @@ -0,0 +1,29 @@ +#include +#include + +using namespace std; + +bool Anagram (string str1, string str2){ + int n1 = str1.length(); + int n2 = str2.length(); + + if (n1 != n2) return false; + + sort(str1.begin(), str1.end()); + sort(str2.begin(), str2.end()); + + for (int i = 0; i < n1; i++) + if (str1[i] != str2[i]) + return false; + return true; +} + +int main() { + string str1 = "listen"; + string str2 = "silent"; + + // Note that each letter need to be in same case. Otherwise, use toUpper Function before adding to function. + + cout << (Anagram(str1, str2) ? "Anagram" : "Not Anagram"); + +} \ No newline at end of file diff --git a/String Algorithms/Balanced_Paranthesis/BalancedParanthesis.cpp b/String Algorithms/Balanced_Paranthesis/BalancedParanthesis.cpp new file mode 100644 index 0000000..9db65a6 --- /dev/null +++ b/String Algorithms/Balanced_Paranthesis/BalancedParanthesis.cpp @@ -0,0 +1,55 @@ +#include +#include +using namespace std; + +bool BracketsBalanced(string expression) +{ + stack stk; // Stack for hold brackets + char x; + + // Traversing Expression + for (char & i : expression) { + if (i == '(' || i == '[' || i == '{') { + stk.push(i); + continue; + } + + // If current bracket isn't a opening bracket then stack cannot be zero. + if (stk.empty()) + return false; + + // Check + if (i == ')'){ + x = stk.top(); + stk.pop(); + if (x == '{' || x == '[') + return false; + } + else if (i == '}') { + x = stk.top(); + stk.pop(); + if (x == '(' || x == '[') + return false; + } else if (i == ']'){ + x = stk.top(); + stk.pop(); + if (x == '(' || x == '{') + return false; + } + else { + continue; + } + } + + return (stk.empty()); +} + +// Driver code +int main() +{ + string expression = "{()}[]"; + + cout << (BracketsBalanced(expression) ? "Balanced" : "Not Balanced"); + + return 0; +}