🚀 Day 581 of #750DaysOfCode 🚀 🔍 Problem Solved: Rotate String A simple-looking problem today — but with a very elegant trick ✨ 💡 Key Insight: Instead of simulating all rotations, we can use a string property: 👉 If goal is a rotation of s, then goal must be a substring of s + s 🧠 Why this works: When we concatenate the string with itself: s = "abcde" s + s = "abcdeabcde" 👉 All possible rotations of s are present inside this new string So we just check: 👉 Does (s + s) contain goal? 📈 Complexity: Time: O(n) Space: O(n) ✨ Takeaway: 👉 Don’t simulate when a pattern/property exists 👉 String concatenation tricks can simplify rotation problems 👉 Always look for hidden transformations Clean logic, powerful idea 💡 #LeetCode #DSA #Java #CodingJourney #ProblemSolving #Strings #Algorithms #LearningEveryday
Rotate String Problem Solved with Elegant Trick
More Relevant Posts
-
Day 52 – #100DaysOfLeetCode Today I solved LeetCode 796 – Rotate String. Problem Overview: Given two strings s and goal, check if s can become goal after performing any number of rotations. Rotation means shifting the first character to the end. Example: "abcde" → "bcdea" → "cdeab" ... Approach – String Trick (Concatenation): 🔹 Key Insight: If goal is a rotation of s, then it must be a substring of s + s. ✔️ First, check if lengths are equal ✔️ Concatenate s + s ✔️ Check if goal exists inside it This avoids manually rotating strings and keeps the solution efficient. Complexity: Time Complexity: O(n) Space Complexity: O(n) Key Learning: This problem showed how a simple observation can replace brute-force logic. Instead of simulating rotations, we transform the problem using string properties. Reflection: Sometimes the best solutions are not complex they just require a shift in perspective. Problems like this remind me to think smarter, not harder. #Day52 #100DaysOfLeetCode #LeetCode796 #RotateString #Java #ProblemSolving #CodingJourney #Consistency
To view or add a comment, sign in
-
-
Day 61/100 – LeetCode Challenge Problem: Rotate String Today I solved the “Rotate String” problem, which checks whether one string can be transformed into another through cyclic rotations. At first, I explored a brute-force approach by simulating rotations one by one and comparing with the target string. While this works, it’s not the most optimal. A more efficient insight is that if a string goal is a rotation of s, then it must be a substring of s + s. This eliminates the need for manual rotations and simplifies the solution significantly. By leveraging this property, we can solve the problem in a clean and efficient way. The solution runs in O(n) time with O(n) space complexity. This problem reinforced how recognizing patterns can turn a seemingly iterative problem into a much simpler one. Sometimes, the best solution comes from observation rather than simulation. Sixty-one days in. The mindset is shifting from solving to simplifying. #100DaysOfLeetCode #Java #DSA #Strings #ProblemSolving #Consistency
To view or add a comment, sign in
-
-
🚀 Day 42 of DSA | Solved Maximum Sum of Distinct Subarrays With Length K Today’s problem was a really good combination of: ✅ Sliding Window ✅ HashMap ✅ Variable window management 🧩 Problem Solved: ✅ Maximum Sum of Distinct Subarrays With Length K 💡 What I Learned: The challenge was not just finding subarrays of size k… It was making sure all elements inside the window are distinct while also maintaining the maximum sum 🔥 🧠 Approach Used: ➡️ Use a sliding window of size k ➡️ Maintain the current window sum ➡️ Use a HashMap to track frequencies ➡️ If window size exceeds k, remove elements from the left ➡️ Only update answer when all elements are distinct 📊 Complexity: ⏱️ Time Complexity: O(n) 📦 Space Complexity: O(k) 🧠 Big Takeaway: Sliding Window problems are starting to feel much more intuitive now. Earlier I used to think window problems were only about sums 😅 But now I’m understanding how powerful they become when combined with HashMaps and frequency tracking. Every problem is teaching a new variation of the same core pattern 🚀 #Day42 #DSA #Java #SlidingWindow #HashMap #Arrays #ProblemSolving #LeetCode #CodingJourney #100DaysOfCode
To view or add a comment, sign in
-
-
🚀 Solving LeetCode Problem | Day 18 📌 Problem: Binary Tree Postorder Traversal (LeetCode 145) 💡 Approach: Used recursive DFS traversal following the order: Left subtree Right subtree Root node The key difference from preorder and inorder traversal is that the current node is processed only after both subtrees are fully explored. ⚡ Key Learning: Tree traversals may look similar, but changing the position of a single line like: ans.add(root.val); completely changes the traversal order and output. This problem reinforced how recursion uses the call stack to naturally backtrack through the tree. ⏱ Time Complexity: O(n) 📦 Space Complexity: O(h) (where h is the height of the tree) #leetcode #dsa #java #binarytree #recursion #trees #100daysofcode
To view or add a comment, sign in
-
-
🚀 Day 13 — #100DaysOfLeetCode Today’s problem: Split Array Largest Sum (LeetCode 410) Initially, I tried solving this problem using brute force by checking every possible partition, but the complexity became too high. Then I explored DP Memoization, which was much better, but the most interesting part was learning the optimal approach using Binary Search + Greedy. ✔ Binary searched the answer space ✔ Greedily created partitions ✔ Minimized the largest subarray sum efficiently 📊 Complexity Comparison: Approach → Time → Space 🔸 Brute Force → O(2^n) → O(n) 🔸 DP Memoization → O(n² × k) → O(nk) ⭐ Binary Search + Greedy → O(n log(sum)) → O(1) Big takeaway from today: Sometimes the best solution comes from changing the way we search for the answer. #LeetCode #100DaysOfLeetCode #DSA #Java #ProblemSolving #Algorithms #CodingInterview #SoftwareDevelopment
To view or add a comment, sign in
-
-
Day 41/60 of my LeetCode Streak 🚀 Solved today’s LeetCode problem: Problem: Find the Prefix Common Array of Two Arrays Difficulty: Medium Concepts Used: • Frequency Array • Prefix Processing • Hashing Logic • Array Traversal What I learned: • Frequency counting can simplify problems involving shared occurrences between arrays • Elegant solutions often emerge after identifying the exact event that changes the answer • Prefix-based problems usually require maintaining incremental state efficiently The key insight was realizing that a number becomes “common” exactly when its frequency reaches 2 across both arrays. 🔥 #leetcode #dsa #java #arrays #hashing #prefixsum #codingjourney
To view or add a comment, sign in
-
Day 38/60 of my LeetCode Streak 🚀 Solved today’s LeetCode problem: Problem: Minimum Common Value Difficulty: Easy Concepts Used: • Two Pointer Technique • Sorted Arrays • Space Optimization What I learned: • Sorted arrays often allow optimized traversal using two pointers • Comparing elements strategically can eliminate unnecessary checks • Choosing the right approach can reduce extra space complexity significantly The key idea was leveraging the sorted property of both arrays to efficiently find the smallest common element in linear time. 🔥 #leetcode #dsa #java #twopointers #arrays #codingjourney
To view or add a comment, sign in
-
-
Day 39/60 of my LeetCode Streak 🚀 Solved today’s LeetCode problem: Problem: Find the Prefix Common Array of Two Arrays Difficulty: Medium Concepts Used: • Frequency Array • Prefix Processing • Hashing Logic • Array Traversal What I learned: • Frequency counting can simplify problems involving shared occurrences between arrays • Elegant solutions often emerge after identifying the exact event that changes the answer • Prefix-based problems usually require maintaining incremental state efficiently The key insight was realizing that a number becomes “common” exactly when its frequency reaches 2 across both arrays. 🔥 #leetcode #dsa #java #arrays #hashing #prefixsum #codingjourney
To view or add a comment, sign in
-
-
𝐃𝐚𝐲 𝟗𝟕 – 𝐃𝐒𝐀 𝐉𝐨𝐮𝐫𝐧𝐞𝐲 | 𝐀𝐫𝐫𝐚𝐲𝐬 🚀 Today’s problem focused on finding two words with no common characters and maximizing the product of their lengths. 𝐏𝐫𝐨𝐛𝐥𝐞𝐦 𝐒𝐨𝐥𝐯𝐞𝐝 • Maximum Product of Word Lengths 🔗 https://lnkd.in/dn8Y_MpV ⸻ 𝐀𝐩𝐩𝐫𝐨𝐚𝐜𝐡 – 𝐁𝐢𝐭𝐌𝐚𝐬𝐤𝐢𝐧𝐠 Key Idea: • Represent each word using a bitmask • Each bit corresponds to a character from a-z Steps: • Build bitmask for every word • Compare every pair of words • If (mask[i] & mask[j]) == 0 → no common characters exist • Update maximum product of lengths Bit operations make character comparison extremely fast. ⸻ 𝐊𝐞𝐲 𝐋𝐞𝐚𝐫𝐧𝐢𝐧𝐠𝐬 • Bitmasking efficiently represents character sets • Bit operations can optimize string comparison problems • Preprocessing helps reduce repeated computation • Compact representations improve performance significantly ⸻ 𝐂𝐨𝐦𝐩𝐥𝐞𝐱𝐢𝐭𝐲 • Time: O(n²) • Space: O(n) ⸻ 𝐓𝐚𝐤𝐞𝐚𝐰𝐚𝐲 Sometimes the best optimization comes from changing how data is represented. ⸻ 97 days consistent 🚀 On to Day 98. #DSA #Arrays #BitManipulation #BitMasking #LeetCode #Java #ProblemSolving #DailyCoding #LearningInPublic #SoftwareDeveloper
To view or add a comment, sign in
-
-
Day 40/60 of my LeetCode Streak 🚀 Solved today’s LeetCode problem: Problem: Minimum Common Value Difficulty: Easy Concepts Used: • Two Pointer Technique • Sorted Arrays • Space Optimization What I learned: • Sorted arrays often allow optimized traversal using two pointers • Comparing elements strategically can eliminate unnecessary checks • Choosing the right approach can reduce extra space complexity significantly The key idea was leveraging the sorted property of both arrays to efficiently find the smallest common element in linear time. 🔥 #leetcode #dsa #java #twopointers #arrays #codingjourney
To view or add a comment, sign in
-