-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4ac2edb
commit 6a455cc
Showing
4 changed files
with
73 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Time complexity - O(|s|) | ||
Space complexity - O(|s|) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
Time complexity - O(n) | ||
Space complexity - O(1) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |