From ee9e359044c2d00e2b24027c57d9ee2140f92a87 Mon Sep 17 00:00:00 2001 From: thimira nirmal Date: Wed, 20 Oct 2021 22:24:54 +0530 Subject: [PATCH 1/2] Anagram CPP --- String Algorithms/Anagram/Anagram.cpp | 29 +++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 String Algorithms/Anagram/Anagram.cpp 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 From 2657d437ef5e44bf9c464cae60cb54460da46950 Mon Sep 17 00:00:00 2001 From: thimira nirmal Date: Thu, 21 Oct 2021 21:43:35 +0530 Subject: [PATCH 2/2] Balanced Parenthesis Py --- .../BalancedParanthesis.py | 46 +++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 String Algorithms/Balanced_Paranthesis/BalancedParanthesis.py diff --git a/String Algorithms/Balanced_Paranthesis/BalancedParanthesis.py b/String Algorithms/Balanced_Paranthesis/BalancedParanthesis.py new file mode 100644 index 0000000..2843a3c --- /dev/null +++ b/String Algorithms/Balanced_Paranthesis/BalancedParanthesis.py @@ -0,0 +1,46 @@ +""""Check Using Stacks""" + +open_br_list = ["[", "{", "("] +close_br_list = ["]", "}", ")"] + +def bracketStack(Str): + stack = [] + for i in Str: + if i in open_br_list: + stack.append(i) + elif i in close_br_list: + pos = close_br_list.index(i) + if (len(stack) > 0) and (open_br_list[pos] == stack[len(stack) - 1]): + stack.pop() + else: + return False + + if len(stack) == 0: + return True + else: + return False + + +""" Method 2 """ + +def brackets(expression): + bracks = ['()', '{}', '[]'] + while any(x in expression for x in bracks): + for br in bracks: + expression = expression.replace(br, '') + return not expression + + +# Driver code +input_string = "([]{}()){}" + +if bracketStack(input_string): + print("balanced") +else: + print("Not balanced") + +# Method 2 +if brackets(input_string): + print("balanced") +else: + print("Not balanced")