Skip to content

Commit

Permalink
Added for 9 Jan
Browse files Browse the repository at this point in the history
  • Loading branch information
Tanmay-312 committed Jan 9, 2024
1 parent 949fdd9 commit 293a8ba
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 0 deletions.
63 changes: 63 additions & 0 deletions GeeksForGeeks/9-1-24/GFG.java
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;
}
}
2 changes: 2 additions & 0 deletions GeeksForGeeks/9-1-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)
2 changes: 2 additions & 0 deletions LeetCode/9-1-24/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Time complexity - O(T1 + T2)
Space complexity - O(T1 + T2)
26 changes: 26 additions & 0 deletions LeetCode/9-1-24/Solution.java
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);
}
}

0 comments on commit 293a8ba

Please sign in to comment.