diff --git a/GeeksForGeeks/April/06-4-24/GFG.java b/GeeksForGeeks/April/06-4-24/GFG.java new file mode 100644 index 0000000..5fd5cee --- /dev/null +++ b/GeeksForGeeks/April/06-4-24/GFG.java @@ -0,0 +1,42 @@ +//{ Driver Code Starts +import java.io.*; +import java.util.*; + +class GFG { + public static void main(String[] args) { + + // taking input using Scanner class + Scanner sc = new Scanner(System.in); + + // taking total testcases + int t = sc.nextInt(); + + while (t-- > 0) { + + // taking total number of stairs + int n = sc.nextInt(); + + // creating an object of class DynamicProgramming + Solution obj = new Solution(); + + // calling method of class countWays() + // of class DynamicProgramming + System.out.println(obj.countWays(n)); + } + } +} + +// } Driver Code Ends + + + +class Solution +{ + // Function to count number of ways to reach the nth stair + // when order does not matter. + Long countWays(int n) + { + // your code here + return (long) (1 + (n / 2)); + } +} diff --git a/GeeksForGeeks/April/06-4-24/README.md b/GeeksForGeeks/April/06-4-24/README.md new file mode 100644 index 0000000..b85bed6 --- /dev/null +++ b/GeeksForGeeks/April/06-4-24/README.md @@ -0,0 +1,2 @@ +Time complexity - O(1) +Space complexity - O(1) diff --git a/LeetCode/April/06-4-24/README.md b/LeetCode/April/06-4-24/README.md new file mode 100644 index 0000000..7384c4b --- /dev/null +++ b/LeetCode/April/06-4-24/README.md @@ -0,0 +1,2 @@ +Time complexity - O(n) +Space complexity - O(n) diff --git a/LeetCode/April/06-4-24/Solution.java b/LeetCode/April/06-4-24/Solution.java new file mode 100644 index 0000000..e41b2bc --- /dev/null +++ b/LeetCode/April/06-4-24/Solution.java @@ -0,0 +1,28 @@ +class Solution +{ + public String minRemoveToMakeValid(String s) + { + Deque stack = new ArrayDeque<>(); + StringBuilder sb = new StringBuilder(s); + + for (int i = 0; i < s.length(); ++i) + { + if (sb.charAt(i) == '(') + { + stack.push(i); + } + else if (sb.charAt(i) == ')') + { + if (stack.isEmpty()) + sb.setCharAt(i, '#'); + else + stack.pop(); + } + } + + while (!stack.isEmpty()) + sb.setCharAt(stack.pop(), '#'); + + return sb.toString().replaceAll("#", ""); + } +}