-
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
949fdd9
commit 293a8ba
Showing
4 changed files
with
93 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,63 @@ | ||
//{ Driver Code Starts | ||
//Initial Template for Java | ||
|
||
import java.io.*; | ||
import java.util.*; | ||
|
||
class GFG | ||
{ | ||
public static void main(String args[])throws IOException | ||
{ | ||
Scanner sc = new Scanner(System.in); | ||
int t = sc.nextInt(); | ||
while(t-- > 0) | ||
{ | ||
String s, patt; | ||
s = sc.next(); | ||
patt = sc.next(); | ||
|
||
Solution ob = new Solution(); | ||
|
||
ArrayList<Integer> res = ob.search(patt, s); | ||
if(res.size()==0) | ||
System.out.print(-1); | ||
else { | ||
for(int i = 0;i<res.size();i++) | ||
System.out.print(res.get(i) + " "); | ||
} | ||
System.out.println(); | ||
} | ||
} | ||
} | ||
// } Driver Code Ends | ||
|
||
|
||
//User function Template for Java | ||
|
||
class Solution | ||
{ | ||
ArrayList<Integer> search(String pat, String txt) | ||
{ | ||
// your code here | ||
ArrayList<Integer> ans=new ArrayList<>(); | ||
int i=0; | ||
|
||
while(true) | ||
{ | ||
int index = txt.indexOf(pat,i); | ||
|
||
if(index==-1) | ||
break; | ||
else | ||
{ | ||
ans.add(index+1); | ||
i=index+1; | ||
} | ||
} | ||
|
||
if(ans.size()==0) | ||
ans.add(-1); | ||
|
||
return ans; | ||
} | ||
} |
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(n) |
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(T1 + T2) | ||
Space complexity - O(T1 + T2) |
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,26 @@ | ||
class Solution | ||
{ | ||
public boolean leafSimilar(TreeNode root1, TreeNode root2) | ||
{ | ||
List<Integer> leaves1 = new ArrayList<>(); | ||
List<Integer> leaves2 = new ArrayList<>(); | ||
dfs(root1, leaves1); | ||
dfs(root2, leaves2); | ||
return leaves1.equals(leaves2); | ||
} | ||
|
||
public void dfs(TreeNode node, List<Integer> leaves) | ||
{ | ||
if (node == null) | ||
return; | ||
|
||
if (node.left == null && node.right == null) | ||
{ | ||
leaves.add(node.val); | ||
return; | ||
} | ||
|
||
dfs(node.left, leaves); | ||
dfs(node.right, leaves); | ||
} | ||
} |