diff --git a/README.md b/README.md index 8bf3e42..588565c 100644 --- a/README.md +++ b/README.md @@ -1,134 +1,52 @@ -# πŸš€ Babua DSA Patterns Course 2025 – 90 Day Challenge (by CTO Bhaiya) +# πŸš€ DSA Patterns Course 2025 – 90-Day Challenge -Welcome to the **DSA Patterns 2025** Course β€” a **90-day challenge** to master the **30 core coding patterns** used in FAANG and top product interviews. -Instead of solving 1000 random questions, you'll learn how to **think in patterns** β€” and solve any problem confidently. +A structured 90-day preparation plan to master the 30 essential **DSA Patterns** used in top product-based company interviews. +This repository contains my daily progress, notes, solved questions, and reflections. --- -## πŸš€ Pre-requistes of Babua DSA Patterns Sheet 2025 -[Onboarding Sheet](https://github.com/team-codebug/babua-dsa-patterns-course/blob/main/ONBOARDING.md) - - -## πŸš€ Babua DSA Patterns Sheet 2025 -[Babua Sheet](https://docs.google.com/spreadsheets/d/1T3vPcwlWdMRMYSf8IxQCKb3gX7rGzDss-ZU-R4XFM14/edit?usp=sharing) - -## πŸš€ Babua DSA Patterns Revision Sheet 2025 -[Babua DSA Patterns Sheet 2025](https://docs.google.com/spreadsheets/d/1RESAUKlHFjdCrEO9YDySoHaHnn_FMdrAgSV9NdZdVos/edit?usp=sharing) -- Make a copy of this sheet to start using the DSA Revision sheet -- 🧠 **Revise Each thing at least 4 times** - Use the revision sheet to practice theory and questions multiple times so that you don't forget - -## πŸ“˜ About the Course +## πŸ“˜ Course Overview **Duration:** 90 Days -**Instructor:** CTO Bhaiya (NITKian) -**Language:** Java (main), Python (optional), Hindi explanations -**Daily Timing:** 5 PM IST on YouTube - -Each pattern includes: -- πŸŽ₯ **Theory Video** β€” Learn the logic and intuition behind the pattern -- 🧠 **Problem Set (15–20 LeetCode Questions)** -- πŸ—’οΈ **Notes & Solutions** - ---- +**Focus:** DSA Pattern-Based Learning +**Language:** Java (primary) -## πŸ’‘ Why Learn DSA by Patterns? +Each day includes: -βœ… Covers 95% of all coding interview problems -βœ… Learn faster by recognizing problem patterns -βœ… Helps you revise logically, not by brute memorization -βœ… Builds intuition and confidence for FAANG-level interviews +- Concept explanation +- Pattern intuition +- Problem set +- Notes & practice questions --- -## πŸ“… 90-Day Progress Tracker - -Mark your progress by ticking βœ… each day. -Links will be updated as the series goes live! +## 🎯 My Goals -| Day | Topic / Pattern | πŸŽ₯ Video Link | πŸ—’οΈ Notes | βœ… Done | -|-----|------------------|---------------|-----------|--------| -| 0 | Complete DSA Patterns In 90 Days Course - Crack Coding Interviews in 3 Months - Day 0/90 - Intro | [Watch Now](https://www.youtube.com/watch?v=zvK49innOZY&list=PLVItHqpXY_DDFNeS6NUUoRsloyaPRdl1l) | [Notes](https://github.com/team-codebug/babua-dsa-patterns-course/blob/main/0_Day) | βœ… | -| 1 | Day 1/90 - ⚑ Two Pointers Theory - DSA Pattern #1 - DSA Pattern You Must Know! | [Watch Now](https://www.youtube.com/watch?v=Elnhh8cC2vM&list=PLVItHqpXY_DDFNeS6NUUoRsloyaPRdl1l&index=2) | [Notes](https://github.com/team-codebug/babua-dsa-patterns-course/blob/main/1_Day) | βœ… | -| X | Disclaimer: NO CONTROVERSY!!! | [Watch Now](https://www.youtube.com/watch?v=vJ2xa878E-M&list=PLVItHqpXY_DDFNeS6NUUoRsloyaPRdl1l&index=3) | [Notes](https://github.com/team-codebug/babua-dsa-patterns-course/blob/main/we_need_to_talk.svg) | βœ… | -| X | DSA Crash Course (Topic-Wise) + Onboarding SHEET + PATTERNS LIST | [Watch Now](https://www.youtube.com/watch?v=Xl0wuOqWWfQ&list=PLVItHqpXY_DDFNeS6NUUoRsloyaPRdl1l&index=4) | [Notes](https://github.com/team-codebug/babua-dsa-patterns-course/blob/main/sheet.svg) | βœ… | -| 2 | Day 2/90 - ⚑Master 5 Two Pointer Problems - Babua DSA Patterns - Crack Coding Interviews | [Watch Now](https://www.youtube.com/watch?v=Yi90cuHYyWQ&list=PLVItHqpXY_DDFNeS6NUUoRsloyaPRdl1l&index=5) | [Notes](https://github.com/team-codebug/babua-dsa-patterns-course/tree/main/2_Day) | βœ… | -| X | The ONLY DSA Sheet You’ll Ever Need in 2025 ⚑ - 30 Patterns + Problems Explained | [Watch Now](https://www.youtube.com/watch?v=4KRw2qdWTKM&list=PLVItHqpXY_DDFNeS6NUUoRsloyaPRdl1l&index=6) | [Notes](https://github.com/team-codebug/babua-dsa-patterns-course/blob/main/Babua-DSA-Patterns-Questions.svg) | βœ… | -| 3 | Day 3/90 πŸ”₯ Master Two Pointer Pattern - 5 LeetCode Problems Explained - Babua DSA Patterns 2025 | [Watch Now](https://www.youtube.com/watch?v=SP065oLnxGA&list=PLVItHqpXY_DDFNeS6NUUoRsloyaPRdl1l&index=7) | [Notes](https://github.com/team-codebug/babua-dsa-patterns-course/tree/main/3_Day) | βœ… | -| 4 | Day 4/90 πŸ”₯Three Popular Interview Questions Two Pointer Pattern - Babua DSA Patterns 2025 | [Watch Now](https://www.youtube.com/watch?v=pcj6S1KLxWc&list=PLVItHqpXY_DDFNeS6NUUoRsloyaPRdl1l&index=8) | [Notes](https://github.com/team-codebug/babua-dsa-patterns-course/tree/main/4_Day) | βœ… | -| 5 | Day 5/90 πŸ”₯Three Popular Interview Questions of Two Pointer Pattern - Babua DSA Patterns 2025 | [Watch Now](https://www.youtube.com/watch?v=jA74srakqvI&list=PLVItHqpXY_DDFNeS6NUUoRsloyaPRdl1l&index=9) | [Notes](https://github.com/team-codebug/babua-dsa-patterns-course/tree/main/5_Day) | βœ… | -| X | Complete DSA Patterns Course 2025 - Recap + Onboarding + Homework - Babua DSA Patterns Course | [Watch Now](https://www.youtube.com/watch?v=MwI_UZlnl6I&list=PLVItHqpXY_DDFNeS6NUUoRsloyaPRdl1l&index=10) | [Notes](https://github.com/team-codebug/babua-dsa-patterns-course/blob/main/Recap%20%2B%20Homework.excalidraw.svg) | βœ… | -| X | From NOBODY to NITK, IITM, Intuit, Adobe, CTO and finally CEO of my own Company - My Journey so far | [Watch Now](https://www.youtube.com/watch?v=vMndRzz1SRo&list=PLVItHqpXY_DDFNeS6NUUoRsloyaPRdl1l&index=11) | [Notes](https://github.com/team-codebug/babua-dsa-patterns-course/blob/main/MY%20JOURNEY.svg) | βœ… | -| 6 | Day 6/90 πŸ”₯ Solve Three Popular String Interview Questions with 1 Trick - Babua DSA Patterns 2025 | [Watch Now](https://www.youtube.com/watch?v=IW8gQ2u2O_I&list=PLVItHqpXY_DDFNeS6NUUoRsloyaPRdl1l&index=12) | [Notes](https://github.com/team-codebug/babua-dsa-patterns-course/tree/main/6_Day) | βœ… | -| 7 | Day 7/90 - ⚑ πŸš€ Fast & Slow Pointers Explained - Crack Linked List Problems in O(n) - DSA Pattern #2 | [Watch Now](https://www.youtube.com/watch?v=FguUjuCzhj0&list=PLVItHqpXY_DDFNeS6NUUoRsloyaPRdl1l&index=13) | [Notes](https://github.com/team-codebug/babua-dsa-patterns-course/tree/main/7_Day) | βœ… | -| 8 | Day 8/90 - ⚑ πŸš€ 5 Popular Interview Questions Fast & Slow Pointers Pattern - DSA Pattern #2 | [Watch Now](https://www.youtube.com/watch?v=eIItwq8UTmU&list=PLVItHqpXY_DDFNeS6NUUoRsloyaPRdl1l&index=14) | [Notes](https://github.com/team-codebug/babua-dsa-patterns-course/tree/main/8_Day) | βœ… | -| X | The DSA Revolution has started - And "THEY" are not HAPPY | [Watch Now](https://www.youtube.com/watch?v=lkbQGa2tEYQ&list=PLVItHqpXY_DDFNeS6NUUoRsloyaPRdl1l&index=15) | [Notes](https://github.com/team-codebug/babua-dsa-patterns-course/blob/main/DSA%20Revolution%20Started.excalidraw.svg) | βœ… | -| 9 | Day 9/90 - ⚑ πŸš€ 3 Popular Interview Questions Fast & Slow Pointers Pattern - DSA Pattern #2 | [Watch Now](https://www.youtube.com/watch?v=E85jp_rHivc&list=PLVItHqpXY_DDFNeS6NUUoRsloyaPRdl1l&index=16) | [Notes](https://github.com/team-codebug/babua-dsa-patterns-course/tree/main/9_Day) | βœ… | -| 10 | Day 10/90 - ⚑2130. Maximum Twin Sum of a Linked List - Fast & Slow Pointers Pattern - DSA Pattern #2 | [Watch Now](https://www.youtube.com/watch?v=t40HN4Rla30&list=PLVItHqpXY_DDFNeS6NUUoRsloyaPRdl1l&index=17) | [Notes](https://github.com/team-codebug/babua-dsa-patterns-course/tree/main/10_Day) | βœ… | -| 11 | Day 11/90 - ⚑457. Circular Array Loop - Fast & Slow Pointers Pattern - DSA Pattern #2 | [Watch Now](https://www.youtube.com/watch?v=nFZ1mysCZc0&list=PLVItHqpXY_DDFNeS6NUUoRsloyaPRdl1l&index=18) | [Notes](https://github.com/team-codebug/babua-dsa-patterns-course/tree/main/11_Day) | βœ… | +- Build strong fundamental intuition of DSA patterns +- Solve 300+ high-quality coding problems +- Maintain a 90-day consistency streak +- Prepare effectively for internship & placement interviews --- -## 🏁 How to Track Progress - -1. **Fork this repo** -2. Replace ⬜ with βœ… as you finish each day -3. Post daily updates on **LinkedIn** with the hashtag **#DSAin90Days** and tag **@CTOBhaiya** -4. Keep your streak β€” and share your learning journey! - ---- - -## 🧩 **LinkedIn Daily Progress Template (for Students)** - -> πŸ“… **Day [X] / 90 – DSA Patterns Challenge 2025** -> -> βœ… **Today’s Pattern:** [Pattern Name β€” e.g., Sliding Window / Two Pointers] -> πŸŽ₯ **Video:** [YouTube Link] -> πŸ—’οΈ **Notes:** [Notes Link or GitHub Link] -> -> 🧠 **What I learned today:** -> -> * [Write 1–2 key insights β€” e.g., how the sliding window helps optimize brute force approaches, etc.] -> * [Any β€œaha moment” or tricky question solved today] -> -> πŸ” **Next Goal:** [Example: Practice all LeetCode questions from this pattern / Revise previous pattern tomorrow] -> -> πŸ’¬ **Reflection:** -> [Share your experience β€” was it easy, tough, interesting? Encourage others to join in!] -> -> πŸš€ **#DSAin90Days #DSAPatterns2025 #CTOBhaiya #FAANGPreparation #LeetCodePatterns #CodingInterviewPreparation #LearnCodeCrack #ThinkInPatterns** -> -> πŸ™Œ Learning under the guidance of **[@Anuj Kumar](https://www.linkedin.com/in/anuj-kumar-9a188968/)** – Thank you for making DSA feel simple and structured! - ---- - -### 🧠 Example Post - -> πŸ“… **Day 4 / 90 – DSA Patterns Challenge 2025** -> -> βœ… **Pattern:** Two Pointers -> πŸŽ₯ **Video:** [https://youtu.be/...](#) -> πŸ—’οΈ **Notes:** [https://github.com/...](#) -> -> 🧠 Learned how to solve *β€œRemove Nth Node from End of List”* and *β€œ3Sum”* efficiently using the two-pointer pattern. -> The biggest takeaway today β€” **simplify the logic by narrowing down the search space** instead of brute forcing! -> -> πŸ” **Next Goal:** Move to Fast & Slow Pointer pattern tomorrow. -> -> πŸ™Œ Thanks **[@Anuj Kumar](https://www.linkedin.com/in/anuj-kumar-9a188968/)** for this amazing structure! -> -> πŸš€ #DSAin90Days #DSAPatterns2025 #FAANGPreparation #CTOBhaiya #LeetCodePatterns #CodingInterviewPreparation #ZeroToHeroDSA - ---- - -## 🌟 Connect & Learn +## πŸ“… 90-Day Progress Tracker -- πŸ“Ί [YouTube Channel](https://www.youtube.com/@CodingwithCTOBhaiya) -- πŸ’Ό [LinkedIn](https://www.linkedin.com/in/anuj-kumar-9a188968/) -- 🌐 [Website](#) +| Day | Topic / Pattern | Video | Status | +| :-: | -------------------------------- | -------------------------------------------------------- | ------ | +| 1 | Two Pointers – Theory | [Link](https://youtu.be/Elnhh8cC2vM?si=OVrgsym_qKhpVgj4) | βœ… | +| 2 | Two Pointers – Questions | [Link](https://youtu.be/Yi90cuHYyWQ?si=BK3RJK2C4gYBk9BG) | βœ… | +| 3 | Two Pointers – Questions | [Link](https://youtu.be/SP065oLnxGA?si=3rQcrVhkDQQn2nqZ) | βœ… | +| 4 | Two Pointers – Questions | [Link](https://youtu.be/pcj6S1KLxWc?si=lpDF9O_Yw8Bv1HI5) | βœ… | +| 5 | Two Pointers – Questions | [Link](https://youtu.be/jA74srakqvI?si=ecv4UgGYbSyz7Tml) | βœ… | +| 6 | Two Pointers – Questions | [Link](https://youtu.be/IW8gQ2u2O_I?si=CEOpWgrmnhlVKU1l) | βœ… | +| 7 | Fast & Slow Pointers – Theory | [Link](https://youtu.be/FguUjuCzhj0?si=WF-FZUP6NwullPjW) | βœ… | +| 8 | Fast & Slow Pointers – Questions | [Link](https://youtu.be/eIItwq8UTmU?si=l-owDQYThTpMbeCC) | βœ… | +| 9 | Fast & Slow Pointers – Questions | [Link](https://youtu.be/E85jp_rHivc?si=ZPjAqpnRoUflGtfo) | βœ… | +| 10 | Fast & Slow Pointers – Questions | [Link](https://youtu.be/t40HN4Rla30?si=FtncjOsYniEbYk9L) | βœ… | +| 11 | Fast & Slow Pointers – Questions | [Link](https://youtu.be/nFZ1mysCZc0?si=orEJbkm8q9NEOdQb) | πŸ”² | --- -**Let’s master DSA by patterns β€” not by luck.** -Start your journey today. Comment β€œDone βœ…β€ when you finish Day 0! +## πŸ”— Resources ---- +- YouTube: _Coding with CTO Bhaiya_ diff --git a/my_solutions/Day_10 b/my_solutions/Day_10 new file mode 100644 index 0000000..231de00 --- /dev/null +++ b/my_solutions/Day_10 @@ -0,0 +1,40 @@ +#LC 2130: Maximum Twin Sum of a Linked List +(similar to palindrome LL) + + +class Solution { + public ListNode findMid(ListNode head){ + ListNode slow = head; + ListNode fast = head; + while(fast != null && fast.next != null){ + slow = slow.next; + fast = fast.next.next; + } + return slow; + } + public int pairSum(ListNode head) { + + ListNode midNode = findMid(head); + + ListNode curr = midNode; + ListNode prev = null; + ListNode next; + while(curr != null){ + next = curr.next; + curr.next = prev; + prev = curr; + curr = next; + } + ListNode right = prev; + ListNode left = head; + int maxSum = Integer.MIN_VALUE; + int sum = 0; + while(right != null){ + sum = left.val + right.val; + maxSum = Math.max(sum, maxSum); + left = left.next; + right = right.next; + } + return maxSum; + } +} diff --git a/my_solutions/Day_2 b/my_solutions/Day_2 new file mode 100644 index 0000000..7e0c31e --- /dev/null +++ b/my_solutions/Day_2 @@ -0,0 +1,171 @@ +# πŸ“ Solutions + +#1️ Palindrome String +Problem: Check if a given string is a valid palindrome considering only alphanumeric characters and ignoring cases. + +# βœ… Solution + +class Solution { + public boolean isPalindrome(String s) { + + int i = 0; + int j = s.length() - 1; + + s = s.toLowerCase(); + + while(i < j) + { + char ch_i = s.charAt(i); + char ch_j = s.charAt(j); + + if(!Character.isLetterOrDigit(ch_i)){ + i++; + continue; + } + + if(!Character.isLetterOrDigit(ch_j)){ + j--; + continue; + } + + if(ch_i != ch_j) + return false; + + i++; + j--; + } + return true; + } +} + +#️2 Reverse String +Problem: Reverse the characters of a character array in-place. + +# βœ… Solution + +class Solution { + public void reverseString(char[] s) { + int i = 0; + int j = s.length - 1; + + while(i < j){ + + char temp = s[i]; + s[i] = s[j]; + s[j] = temp; + + i++; + j--; + } + } +} + + +#️3 Sorted Squares +Problem:Given a sorted array, return the array of squares of each number, sorted non-decreasing. + +# βœ… Solution + +class Solution { + public int[] sortedSquares(int[] nums) { + + int i = 0; + int j = nums.length - 1; + + int[] ans = new int[nums.length]; + int idx = nums.length - 1; + + while(i <= j){ + + if(Math.abs(nums[i]) > Math.abs(nums[j])){ + ans[idx--] = nums[i] * nums[i]; + i++; + } + else{ // equal case handled automatically + ans[idx--] = nums[j] * nums[j]; + j--; + } + } + return ans; + } +} + +#️4 Valid Palindrome II +Problem:Given a string s, return true if the s can be palindrome after deleting at most one character from it. + +# βœ… Solution +class Solution { + public boolean helper(int i, int j, String s){ + + while(i < j){ + if(s.charAt(i) != s.charAt(j)) + return false; + + i++; + j--; + } + return true; + + } + public boolean validPalindrome(String s) { + + int i = 0 ; + int j = s.length() - 1; + + while(i < j){ + + char left = s.charAt(i); + char right = s.charAt(j); + + if(left != right){ + return helper(i + 1, j, s) || helper(i, j - 1, s); + } + + i++; + j--; + } + return true; + } +} + +#️5 Valid Word Abbreviation(NeetCode) + +class Solution { + public boolean validWordAbbreviation(String word, String abbr) { + int i = 0, j = 0; + int n = word.length(), m = abbr.length(); + + while(i < n && j < m){ + + char abbr_chr = abbr.charAt(j); + + // Case 1: Letter β†’ must match + if(Character.isLetter(abbr_chr)){ + if(word.charAt(i) != abbr_chr) + return false; + + i++; + j++; + } + else { + // Leading zero not allowed + if(abbr_chr == '0') return false; + + int dig = 0; + + + while(j < m && Character.isDigit(abbr.charAt(j))){ + dig = dig * 10 + (abbr.charAt(j) - '0'); + j++; + } + + + i += dig; + } + } + + return i == n && j == m; + } +} + + diff --git a/my_solutions/Day_3 b/my_solutions/Day_3 new file mode 100644 index 0000000..9adca31 --- /dev/null +++ b/my_solutions/Day_3 @@ -0,0 +1,118 @@ +#1 Merge Sorted Array (LC 88) + +class Solution { + public void merge(int[] nums1, int m, int[] nums2, int n) { + + int i = m - 1, + j = n - 1, + k = m + n - 1; + + while(i >= 0 && j >= 0){ + if(nums1[i] > nums2[j]){ + nums1[k] = nums1[i]; + i--; + }else{ + nums1[k] = nums2[j]; + j--; + } + k--; + } + while (j >= 0) { + nums1[k] = nums2[j]; + j--; + k--; + } + + } +} + +#2 Count Pairs Whose Sum is Less than Target + +class Solution { + public int countPairs(List nums, int target) { + //Optimised -> Two pointers + Collections.sort(nums); + int i = 0, j = nums.size() - 1; + + int pairs = 0; + while(i < j){ + if(nums.get(i) + nums.get(j) < target){ + pairs += j - i; + i++; + }else{ + j--; + } + } + return pairs; + } +} + +#3 Two Sum + +class Solution { + public int[] twoSum(int[] nums, int target) { + HashMap map = new HashMap<>(); + //O(n) + for(int i=0;i O(n) + +#4 Two Sum II - Input Array Is Sorted + +class Solution { + public int[] twoSum(int[] nums, int target) { + int i = 0, j = nums.length - 1; + while(i < j){ + int sum = nums[i] + nums[j]; + if(sum == target) + return new int[]{i+1, j+1}; + else if (sumtarget) k--; + else if(sum equal to target sum + return sum; + } + } + } + return closestSum; + } +} + +Related problems done βœ…: 4Sum(LC 18), 3Sum Closest(LC 16) diff --git a/my_solutions/Day_4 b/my_solutions/Day_4 new file mode 100644 index 0000000..79fef5d --- /dev/null +++ b/my_solutions/Day_4 @@ -0,0 +1,60 @@ +βœ…1 Sort Colors (LC 75) + +class Solution { + public void sortColors(int[] nums) { + //Dutch National Flag Algorithm + //TC -> O(n), SC -> O(1) + int i = 0, j = nums.length - 1, k = 0; + while(k <= j){ + if(nums[k] == 1) k++; + + else if(nums[k] == 2){ + int temp = nums[k]; + nums[k] = nums[j]; + nums[j] = temp; + + j--; + } + else{ + int temp = nums[i]; + nums[i] = nums[k]; + nums[k] = temp; + + k++; i++; + } + } + } +} + +βœ…2 Remove Nth Node From End of List(LC 19) + +class Solution { + public ListNode removeNthFromEnd(ListNode head, int n) { + + ListNode temp = head; + int size = 0; + while(temp != null){ + temp = temp.next; + size++; + } + + if(size == n) + return head.next; + + ListNode dummy = new ListNode(-1); + dummy.next = head; + + ListNode prev = dummy; + ListNode curr = head; + + int prevIdx = 1; + + while(prevIdx <= size - n){ + curr = curr.next; + prev = prev.next; + prevIdx++; + } + prev.next = prev.next.next; + return dummy.next; + } +} diff --git a/my_solutions/Day_5 b/my_solutions/Day_5 new file mode 100644 index 0000000..0029848 --- /dev/null +++ b/my_solutions/Day_5 @@ -0,0 +1,75 @@ +#1 Strobogrammatic Number + +import java.util.HashMap; + +public class strobogrammatic_number { + public static boolean checkisStrobogrammatic(String s, HashMap map){ + + int i=0, j=s.length()-1; + + while(i<=j){ //TC : O(N) + char l = s.charAt(i); + char r = s.charAt(j); + + if(!map.containsKey(l)) + return false; + else{ + if(map.get(l) != r) + return false; + else{ + i++; + j--; + } + } + } + return true; + } + public static void main(String[] args) { + String s = "12881"; + HashMap map = new HashMap<>(); + + map.put('0','0'); + map.put('1','1'); + map.put('8','8'); + map.put('6','9'); + map.put('9','6'); + + boolean ans = checkisStrobogrammatic(s, map); + System.out.println(ans); + } + +#2 Append Characters to String to Make Subsequence(LC 2486) + +class Solution { + public int appendCharacters(String s, String t) { + int i = 0, j = 0; + + while(i < s.length() && j < t.length()){ //TC -> O(N) + char sChar = s.charAt(i); + char tChar = t.charAt(j); + + if(sChar != tChar) i++; + else{ + i++; + j++; + } + } + return (j < t.length()) ? t.length() - j : 0; + } +} + +#3 Lowest Common Ancestor of a Binary Tree III(NeetCode, LC premium) +class Solution { + public Node lowestCommonAncestor(Node p, Node q) { + Node p1 = p, + q1 = q; + + while(p1 != q1){ + p1 = (p1==null)? q : p1.parent; + q1 = (q1==null)? p : q1.parent; + } + return p1; + } +} + +} diff --git a/my_solutions/Day_6 b/my_solutions/Day_6 new file mode 100644 index 0000000..d6f99cd --- /dev/null +++ b/my_solutions/Day_6 @@ -0,0 +1,68 @@ +#1 Reverse Words in a String(LC 151) + +//TC -> O(n), SC -> O(1), no use of lib functions +class Solution { + public String reverseWords(String s) { //SPACE OPTIMISED + //trim //" hello world " + int l = 0, r = s.length() - 1; + + while(s.charAt(l) == ' ') l+=1; + + while(s.charAt(r) == ' ') r-=1; + + //append the words in stringbuilder with no duplicate spaces + //"hello world" + // l + // r + StringBuilder sb = new StringBuilder(); + while(l <= r){ + char ch = s.charAt(l); + if(ch != ' '){ + sb.append(ch); + l = l + 1; + }else{ + if(sb.charAt(sb.length() - 1) != ' ') sb.append(' '); + l = l + 1; + } + } + + //sb = "hello world" + //reverse sb + int i = 0, j = sb.length() - 1; + while(i <= j){ + char temp = sb.charAt(i); + sb.setCharAt(i, sb.charAt(j)); + sb.setCharAt(j, temp); + + i = i + 1; + j = j - 1; + } + //sb = "dlrow olleh" + //reverse every word + sb.append(" "); + StringBuilder temp = new StringBuilder(); + StringBuilder ans = new StringBuilder(); + for(i = 0; i < sb.length() ; i++){ + char ch = sb.charAt(i); + if(ch != ' ') + temp.append(ch); + else{ + int st = 0; + int end = temp.length() - 1; + while(st <= end){ + char k = temp.charAt(st); + temp.setCharAt(st, temp.charAt(end)); + temp.setCharAt(end, k); + + st++; end--; + } + ans.append(temp); + if(i != sb.length() - 1) ans.append(" "); + temp.setLength(0); + } + } + return ans.toString(); + } +} + +Related problems done: Reverse String II (LC 541)βœ… diff --git a/my_solutions/Day_8 b/my_solutions/Day_8 new file mode 100644 index 0000000..247c320 --- /dev/null +++ b/my_solutions/Day_8 @@ -0,0 +1,120 @@ +#1 Happy Number(LC 202) + +class Solution { + public int sumOfSquares(int n){ + int sum = 0; + while(n > 0){ + int r = n % 10; + sum += r * r; + n = n / 10; + } + return sum; + } + public boolean isHappy(int n) { + int slow = n; + int fast = n; + + while(fast != 1){ + slow = sumOfSquares(slow); + fast = sumOfSquares(sumOfSquares(fast)); + + if(fast == 1) return true; + + if(slow == fast) return false; + } + return true; + } + +#2 Middle of LL(LC 876) + +class Solution { + public ListNode middleNode(ListNode head) { + ListNode slow=head,fast=head; + while(fast!=null && fast.next!=null) + { + slow=slow.next; + fast=fast.next.next; + } + return slow; + } +} + +#3 Linked List Cycle(LC 141) + +public class Solution { + public boolean hasCycle(ListNode head) { + ListNode fast=head; + ListNode slow=head; + while(fast!=null && fast.next!=null){ + slow=slow.next; + fast=fast.next.next; + + if(slow==fast) + return true; + } + return false; + } +} + +#4 Linked List Cycle II (LC 142) + +public class Solution { + public ListNode detectCycle(ListNode head) { + ListNode slow = head; + ListNode fast = head; + + while(fast != null && fast.next != null){ + slow = slow.next; + fast = fast.next.next; + + if(slow == fast){ + slow = head; + while(slow != fast){ + slow = slow.next; + fast = fast.next; + } + return slow; + } + } + return null; + } +} + +#5 Find length of loop in LL (gfg) + +class Solution { + public int lengthOfLoop(Node head) { + // code here + Node slow = head; + Node fast = head; + + while(fast != null && fast.next != null){ + + slow = slow.next; + fast = fast.next.next; + + if(slow == fast){// if cycle exists + // finding starting node + slow = head; + while(slow != fast){ + slow = slow.next; + fast = fast.next; + } + break; + } + } + + if(fast == null || fast.next == null) // no cycle + return 0; + + Node curr = slow.next; + int len = 1; + while(curr != slow){ + curr = curr.next; + len++; + } + return len; + } +} + +} diff --git a/my_solutions/Day_9 b/my_solutions/Day_9 new file mode 100644 index 0000000..18bc5bd --- /dev/null +++ b/my_solutions/Day_9 @@ -0,0 +1,85 @@ +#Split a Linked List into two halves + +class Solution { + public Pair splitList(Node head) { + // Code here + Node slow = head, fast = head.next; + + while(fast != head && fast.next != head) + { + slow = slow.next; + fast = fast.next; + + if(fast.next != head) + fast = fast.next; + } + fast.next = slow.next; + slow.next = head; + + return new Pair<>(head, fast.next); + } +} + +#2 Find the Duplicate Number(LC 287) + +class Solution { + public int findDuplicate(int[] arr) { + int i = 0; + while(i < arr.length){ + int corrIdx = arr[i] - 1; + if(arr[i] != arr[corrIdx]){ + int temp =arr[i]; + arr[i] = arr[corrIdx]; + arr[corrIdx] = temp; + } + else i++; + } + + for(i = 0 ; i < arr.length ; i++){ + if( i != arr[i] - 1) + return arr[i]; + } + + return -1; + } +} + +#3 Palindrome Linked List + + +class Solution { + public boolean isPalindrome(Node head) { + // code here + // find middle node of ll + Node slow = head; + Node fast = head; + while(fast != null && fast.next != null){ + slow = slow.next; + fast = fast.next.next; + } + Node midNode = slow; + //reverse second half of ll + Node curr = midNode; + Node prev = null; + Node next; + while(curr != null){ + next = curr.next; + curr.next = prev; + prev = curr; + curr = next; + } + + Node left = head; + Node right = prev; + while(right != null){ + if(left.data != right.data) + return false; + + left = left.next; + right = right.next; + } + return true; + } +} + +βœ…βœ…βœ