diff --git a/GeeksForGeeks/9-1-24/GFG.java b/GeeksForGeeks/9-1-24/GFG.java new file mode 100644 index 0000000..2fd00b1 --- /dev/null +++ b/GeeksForGeeks/9-1-24/GFG.java @@ -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 res = ob.search(patt, s); + if(res.size()==0) + System.out.print(-1); + else { + for(int i = 0;i search(String pat, String txt) + { + // your code here + ArrayList 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; + } +} diff --git a/GeeksForGeeks/9-1-24/README.md b/GeeksForGeeks/9-1-24/README.md new file mode 100644 index 0000000..7384c4b --- /dev/null +++ b/GeeksForGeeks/9-1-24/README.md @@ -0,0 +1,2 @@ +Time complexity - O(n) +Space complexity - O(n) diff --git a/LeetCode/9-1-24/README.md b/LeetCode/9-1-24/README.md new file mode 100644 index 0000000..3cb94c1 --- /dev/null +++ b/LeetCode/9-1-24/README.md @@ -0,0 +1,2 @@ +Time complexity - O(T1 + T2) +Space complexity - O(T1 + T2) diff --git a/LeetCode/9-1-24/Solution.java b/LeetCode/9-1-24/Solution.java new file mode 100644 index 0000000..70c73ba --- /dev/null +++ b/LeetCode/9-1-24/Solution.java @@ -0,0 +1,26 @@ +class Solution +{ + public boolean leafSimilar(TreeNode root1, TreeNode root2) + { + List leaves1 = new ArrayList<>(); + List leaves2 = new ArrayList<>(); + dfs(root1, leaves1); + dfs(root2, leaves2); + return leaves1.equals(leaves2); + } + + public void dfs(TreeNode node, List 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); + } +}