From 293a8bafae75ebb15be0be0640096b9aee1722f9 Mon Sep 17 00:00:00 2001 From: Tanmay-312 Date: Tue, 9 Jan 2024 13:28:50 +0530 Subject: [PATCH] Added for 9 Jan --- GeeksForGeeks/9-1-24/GFG.java | 63 ++++++++++++++++++++++++++++++++++ GeeksForGeeks/9-1-24/README.md | 2 ++ LeetCode/9-1-24/README.md | 2 ++ LeetCode/9-1-24/Solution.java | 26 ++++++++++++++ 4 files changed, 93 insertions(+) create mode 100644 GeeksForGeeks/9-1-24/GFG.java create mode 100644 GeeksForGeeks/9-1-24/README.md create mode 100644 LeetCode/9-1-24/README.md create mode 100644 LeetCode/9-1-24/Solution.java 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); + } +}