From dd1418290391c6e05009033e94a5c450fb7eeed9 Mon Sep 17 00:00:00 2001 From: Tanmay-312 Date: Wed, 6 Mar 2024 10:24:54 +0530 Subject: [PATCH] Added codes for 6 March --- GeeksForGeeks/March/06-3-24/GFG.java | 101 ++++++++++++++++++++++++++ GeeksForGeeks/March/06-3-24/README.md | 2 + LeetCode/March/06-3-24/README.md | 2 + LeetCode/March/06-3-24/Solution.java | 29 ++++++++ 4 files changed, 134 insertions(+) create mode 100644 GeeksForGeeks/March/06-3-24/GFG.java create mode 100644 GeeksForGeeks/March/06-3-24/README.md create mode 100644 LeetCode/March/06-3-24/README.md create mode 100644 LeetCode/March/06-3-24/Solution.java diff --git a/GeeksForGeeks/March/06-3-24/GFG.java b/GeeksForGeeks/March/06-3-24/GFG.java new file mode 100644 index 0000000..02a7491 --- /dev/null +++ b/GeeksForGeeks/March/06-3-24/GFG.java @@ -0,0 +1,101 @@ +//{ 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); + + for(int i = 0;i search(String pat, String txt) + { + // your code here + ArrayList indices = new ArrayList<>(); + int m = pat.length(); + int n = txt.length(); + int[] lps = computeLPSArray(pat); + int i = 0; + int j = 0; + while (i < n) + { + if (pat.charAt(j) == txt.charAt(i)) + { + i++; + j++; + } + if (j == m) + { + indices.add(i - j + 1); + j = lps[j - 1]; + } + else if (i < n && pat.charAt(j) != txt.charAt(i)) + { + if (j != 0) + { + j = lps[j - 1]; + } + else + { + i++; + } + } + } + return indices; + } + + + private int[] computeLPSArray(String pat) + { + int m = pat.length(); + int[] lps = new int[m]; + int len = 0; + for (int i = 1; i < m; ) + { + if (pat.charAt(i) == pat.charAt(len)) + { + len++; + lps[i] = len; + i++; + } + else + { + if (len != 0) + { + len = lps[len - 1]; + } + else + { + lps[i] = 0; + i++; + } + } + } + return lps; + } +} diff --git a/GeeksForGeeks/March/06-3-24/README.md b/GeeksForGeeks/March/06-3-24/README.md new file mode 100644 index 0000000..5851640 --- /dev/null +++ b/GeeksForGeeks/March/06-3-24/README.md @@ -0,0 +1,2 @@ +Time complexity - O(|text| + |pattern|) +Space complexity - O(1) diff --git a/LeetCode/March/06-3-24/README.md b/LeetCode/March/06-3-24/README.md new file mode 100644 index 0000000..9567f26 --- /dev/null +++ b/LeetCode/March/06-3-24/README.md @@ -0,0 +1,2 @@ +Time complexity - O(n) +Space complexity - O(1) diff --git a/LeetCode/March/06-3-24/Solution.java b/LeetCode/March/06-3-24/Solution.java new file mode 100644 index 0000000..10176ff --- /dev/null +++ b/LeetCode/March/06-3-24/Solution.java @@ -0,0 +1,29 @@ +/** + * Definition for singly-linked list. + * class ListNode { + * int val; + * ListNode next; + * ListNode(int x) { + * val = x; + * next = null; + * } + * } + */ +public class Solution +{ + public boolean hasCycle(ListNode head) + { + ListNode slow = head; + ListNode fast = head; + + while (fast != null && fast.next != null) + { + slow = slow.next; + fast = fast.next.next; + if (slow == fast) + return true; + } + + return false; + } +}