Skip to content

Commit

Permalink
Added codes for 6 April
Browse files Browse the repository at this point in the history
  • Loading branch information
Tanmay-312 committed Apr 6, 2024
1 parent 230dc59 commit 0a216c2
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 0 deletions.
42 changes: 42 additions & 0 deletions GeeksForGeeks/April/06-4-24/GFG.java
Original file line number Diff line number Diff line change
@@ -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));
}
}
2 changes: 2 additions & 0 deletions GeeksForGeeks/April/06-4-24/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Time complexity - O(1)
Space complexity - O(1)
2 changes: 2 additions & 0 deletions LeetCode/April/06-4-24/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Time complexity - O(n)
Space complexity - O(n)
28 changes: 28 additions & 0 deletions LeetCode/April/06-4-24/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
class Solution
{
public String minRemoveToMakeValid(String s)
{
Deque<Integer> 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("#", "");
}
}

0 comments on commit 0a216c2

Please sign in to comment.