Skip to content

Commit

Permalink
Added codes for 4 April
Browse files Browse the repository at this point in the history
  • Loading branch information
Tanmay-312 committed Apr 4, 2024
1 parent 4ac2edb commit 6a455cc
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 0 deletions.
53 changes: 53 additions & 0 deletions GeeksForGeeks/April/04-4-24/GFG.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
//{ Driver Code Starts
//Initial Template for Java

/*package whatever //do not write package name here */

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();
sc.nextLine();
while(t-->0)
{
//taking the String
String s=sc.nextLine();
Solution ob = new Solution();
//calling method sumSubstrings()
System.out.println(ob.sumSubstrings(s));
}
}
}
// } Driver Code Ends


//User function Template for Java

class Solution
{
//Function to find sum of all possible substrings of the given string.
public static long sumSubstrings(String s)
{
//Your code here
int mod = 1000000007;

int n = s.length();
long result = 0;
long multiplier = 1;

for (int i = n - 1; i >= 0; i--)
{
int digit = s.charAt(i) - '0';
result = (result + (digit * multiplier * (i + 1)) % mod) % mod;
multiplier = (multiplier * 10 + 1) % mod;
}

return result;
}
}
2 changes: 2 additions & 0 deletions GeeksForGeeks/April/04-4-24/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Time complexity - O(|s|)
Space complexity - O(|s|)
2 changes: 2 additions & 0 deletions LeetCode/April/04-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(1)
16 changes: 16 additions & 0 deletions LeetCode/April/04-4-24/Solution.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
class Solution
{
public int maxDepth(String s)
{
int ans = 0;
int opened = 0;

for (char c : s.toCharArray())
if (c == '(')
ans = Math.max(ans, ++opened);
else if (c == ')')
--opened;

return ans;
}
}

0 comments on commit 6a455cc

Please sign in to comment.