From b37ed5a5dac072839ffc9a0f92f931052d975aea Mon Sep 17 00:00:00 2001 From: fobic Date: Wed, 2 Oct 2024 02:04:36 +0530 Subject: [PATCH 1/2] added remove outermost parenthesis algorithm --- strings/remove_outermost_parenthesis.dart | 33 +++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 strings/remove_outermost_parenthesis.dart diff --git a/strings/remove_outermost_parenthesis.dart b/strings/remove_outermost_parenthesis.dart new file mode 100644 index 0000000..7469e43 --- /dev/null +++ b/strings/remove_outermost_parenthesis.dart @@ -0,0 +1,33 @@ +class Solution { + String removeOuterParentheses(String s) { + int cnt = 0; + StringBuffer res = StringBuffer(); + + for (int i = 0; i < s.length; i++) { + if (s[i] == '(' && cnt == 0) { + cnt++; + } else if (s[i] == '(' && cnt >= 1) { + res.write(s[i]); + cnt++; + } else if (s[i] == ')' && cnt > 1) { + res.write(s[i]); + cnt--; + } else if (s[i] == ')' && cnt == 1) { + cnt--; + } + } + + return res.toString(); + } +} + +void main() { + Solution solution = Solution(); + + // Test case + String input = "(()())(())"; + String result = solution.removeOuterParentheses(input); + + print("Input: $input"); + print("Output: $result"); +} From 5ab9f2105c46e200d2178b7031d03400609b3d5d Mon Sep 17 00:00:00 2001 From: fobic Date: Wed, 2 Oct 2024 02:17:33 +0530 Subject: [PATCH 2/2] added rotate string algorithm --- strings/remove_outermost_parenthesis.dart | 33 ----------------------- strings/rotate_string.dart | 31 +++++++++++++++++++++ 2 files changed, 31 insertions(+), 33 deletions(-) delete mode 100644 strings/remove_outermost_parenthesis.dart create mode 100644 strings/rotate_string.dart diff --git a/strings/remove_outermost_parenthesis.dart b/strings/remove_outermost_parenthesis.dart deleted file mode 100644 index 7469e43..0000000 --- a/strings/remove_outermost_parenthesis.dart +++ /dev/null @@ -1,33 +0,0 @@ -class Solution { - String removeOuterParentheses(String s) { - int cnt = 0; - StringBuffer res = StringBuffer(); - - for (int i = 0; i < s.length; i++) { - if (s[i] == '(' && cnt == 0) { - cnt++; - } else if (s[i] == '(' && cnt >= 1) { - res.write(s[i]); - cnt++; - } else if (s[i] == ')' && cnt > 1) { - res.write(s[i]); - cnt--; - } else if (s[i] == ')' && cnt == 1) { - cnt--; - } - } - - return res.toString(); - } -} - -void main() { - Solution solution = Solution(); - - // Test case - String input = "(()())(())"; - String result = solution.removeOuterParentheses(input); - - print("Input: $input"); - print("Output: $result"); -} diff --git a/strings/rotate_string.dart b/strings/rotate_string.dart new file mode 100644 index 0000000..40f7fa8 --- /dev/null +++ b/strings/rotate_string.dart @@ -0,0 +1,31 @@ +// leetcode problem : https://leetcode.com/problems/rotate-string + +class Solution { + bool rotateString(String s, String goal) { + int m = s.length; + int n = goal.length; + + if (m != n) return false; + + for (int i = 0; i < m; i++) { + s = s.substring(1) + s[0]; + if (s == goal) return true; + } + return false; + } +} + +void main() { + Solution solution = Solution(); + + // Test case + String s = "abcde"; + String goal = "cdeab"; + + bool result = solution.rotateString(s, goal); + + print("Input:"); + print("s = \"$s\""); + print("goal = \"$goal\""); + print("Output: $result"); +}