🌟 Day 28 of #100DaysOfCode 🌟 🔍 Reverse Words in a String — Clean & Elegant String Manipulation 🔹 What I Solved Today, I solved the classic “Reverse Words in a String” problem — a deceptively simple challenge that tests your understanding of string manipulation, trimming spaces, and efficient iteration. This problem is all about cleaning messy input and reordering data logically, which is a valuable skill in real-world text processing tasks. 📝 Problem Statement Given an input string s, reverse the order of the words. A word is defined as a sequence of non-space characters. The returned string should have only a single space separating words — with no leading or trailing spaces. Example 1: Input: s = "the sky is blue" Output: blue is sky the Example 2: Input: s = " hello world " Output: world hello Example 3: Input: s = "a good example" Output: example good a 🧠 Concepts Used String Splitting & Trimming Whitespace Handling Iteration & StringBuilder Efficient Memory Management ⚙️ Approach 1️⃣ Trim the string to remove extra leading/trailing spaces. 2️⃣ Split the string by one or more spaces using regex (\\s+). 3️⃣ Iterate backward through the words array. 4️⃣ Append words to a StringBuilder, adding spaces only between words. 5️⃣ Return the final reversed and clean string. 🚀 What I Learned This problem reinforced how attention to detail matters in coding — especially when handling strings and whitespace. It also reminded me that even simple problems can teach clean code principles, edge case thinking, and time-space optimization. Grateful to K.R. Mangalam University for continuously motivating me on this journey of learning and consistency. #100DaysOfCode #Java #ProblemSolving #CodingJourney #DataStructures #Algorithms
Solved "Reverse Words in a String" problem with Java
More Relevant Posts
-
🔥 Day 105 of My DSA Challenge – Permutations II 🔷 Problem : 47. Permutations II 🔷 Goal : Generate all unique permutations of a list of integers that may contain duplicates. Example → Input : nums = [1,1,2] Output : [[1,1,2], [1,2,1], [2,1,1]] 🔷 Key Insight : This is a backtracking + deduplication problem — similar to the basic permutation problem, but with an added twist : We must handle duplicates efficiently to avoid generating the same permutation multiple times. 🔷 Approach : 1️⃣ Sort the array to group duplicates together. 2️⃣ Use a boolean vis[] array to track visited elements. 3️⃣ Before choosing a number, skip it if it’s the same as the previous one and the previous one hasn’t been used — this avoids duplicate branches. 4️⃣ Backtrack after each recursive call to explore other paths. 🔷 My Java Approach : Sorted the array to make duplicate detection easier. Used recursion to build permutations step by step. Applied duplicate-skipping condition inside the loop. 🔷 Complexity : Time → O(N! × N) (in the worst case, when all numbers are unique) Space → O(N) (for recursion + visited array) This problem beautifully blends recursion, sorting, and logical pruning, teaching how to handle duplicate elements without extra data structures. Every step in backtracking builds precision — not just in code, but in thinking. ⚡ #Day105 #100DaysOfCode #LeetCode #DSA #Java #ProblemSolving #Backtracking #Recursion #Permutations #CodingChallenge #Programming #SoftwareEngineering #Algorithms #DataStructures #TechJourney #EngineerMindset #DeveloperJourney #Growth
To view or add a comment, sign in
-
-
🌟 Day 26 of #100DaysOfCode 🌟 🔍 Remove Duplicate Letters — Lexicographically Smallest Unique String 🔹 What I Solved Today, I tackled the “Remove Duplicate Letters” problem — a fascinating challenge that combines stack operations, greedy logic, and lexicographical ordering. It’s a perfect test of both analytical and implementation skills. 📝 Problem Statement Given a string s, remove duplicate letters so that every letter appears once and only once. You must ensure the resulting string is the smallest in lexicographical order among all possible results. ✅ Example 1: Input: s = "bcabc" Output: "abc" ✅ Example 2: Input: s = "cbacdcbc" Output: "acdb" Constraints: 1 ≤ s.length ≤ 10⁴ s consists of lowercase English letters 🧠 Concepts Used Stack Greedy Algorithm HashMap / Frequency Counting Character Visitation Tracking ⚙️ Approach Count the frequency of each character using a map. Use a stack to build the result string. Iterate through each character: Decrease its frequency (as it’s now visited). If the character is already in the stack, skip it. Otherwise, while the current character is smaller than the top of the stack and the top of the stack will appear later again, pop it out to maintain lexicographical order. Push the current character into the stack. Convert the stack to the final result string. 🚀 What I Learned This problem beautifully demonstrates how greedy thinking and data structures like stacks can work together to maintain order and constraints. It deepened my understanding of how lexicographical optimization problems can be efficiently solved using character frequency and conditional popping. #100DaysOfCode #ProblemSolving #Java #DataStructures #Algorithms #CodingJourney #GeeksforGeeks #KeepLearning
To view or add a comment, sign in
-
-
#100DaysOfCode – Day 66 String Manipulation & Primitive Decomposition 🧩 Task: Given a valid parentheses string, decompose it into its primitive components and then remove the outermost parentheses from each component. Example: Input: s = "(()())(())" Primitive Decomposition: "(()())" + "(())" After removing outermost parentheses: "()()" + "()" Output: "()()()" My Approach: I iterated through the string while keeping a counter to track the balance of open parentheses. When an opening parenthesis ( was found, I incremented the counter. If the count was greater than 1, it meant this parenthesis was not an outermost one, so I appended it to the result. When a closing parenthesis ) was found, I only appended it if the counter was greater than 1 before decrementing. This ensures the final closing parenthesis of a primitive part is excluded. This simple counter-based approach effectively identifies and removes the correct parentheses without needing a more complex data structure like a stack. Time Complexity: O(N) Space Complexity: O(N) Sometimes, a simple counter is all you need to elegantly handle nested structures. It can be a clean and efficient alternative to more complex data structures for certain problems. #takeUforward #100DaysOfCode #Java #ProblemSolving #LeetCode #DataStructures #Algorithms #StringManipulation #CodeNewbie
To view or add a comment, sign in
-
-
💻 Day 57 of #LeetCode100DaysChallenge Solved LeetCode 166: Fraction to Recurring Decimal — a problem that tests mathematical precision, string construction, and use of hashing to detect cycles. 🧩 Problem: Given two integers, numerator and denominator, return their fractional result as a string. If the decimal part repeats, enclose the repeating portion in parentheses. Example: Input: numerator = 1, denominator = 2 Output: "0.5" Input: numerator = 2, denominator = 3 Output: "0.(6)" 💡 Approach — Long Division with Remainder Tracking: 1️⃣ Handle edge cases like zero numerator and sign of the result. 2️⃣ Perform integer division for the whole part (numerator / denominator). 3️⃣ For the fractional part: Use a HashMap to store remainders and their positions in the result string. Multiply remainder by 10 at each step and find the next digit. If a remainder repeats, insert parentheses around the repeating sequence. 4️⃣ Combine the integer and fractional parts to form the final result. ⚙️ Complexity: Time: O(N) — where N is the length of the repeating cycle. Space: O(N) — for storing remainders in the HashMap. ✨ Key Takeaways: ✅ Strengthened understanding of long division logic. ✅ Learned how to detect repeating patterns using hashing. ✅ Reinforced precision handling and string manipulation in math-based problems. #LeetCode #100DaysOfCode #Java #HashMap #Math #StringManipulation #DSA #CodingJourney #WomenInTech #FractionToRecurringDecimal
To view or add a comment, sign in
-
-
🌟 Day 31 of #100DaysOfCode 🌟 🔍 Backspace String Compare — Stack Logic & Two-Pointer Optimization 🔹 What I Solved Today, I solved the “Backspace String Compare” problem — an elegant challenge that simulates typing behavior in text editors. The twist? The # symbol acts as a backspace, and we must determine if two strings are equal after applying all backspaces. 📝 Problem Statement Given two strings s and t, return true if they are equal when both are typed into empty text editors. # means a backspace character. Example 1: Input: s = "ab#c", t = "ad#c" Output: true Explanation: Both become "ac". Example 2: Input: s = "ab##", t = "c#d#" Output: true Explanation: Both become "". Example 3: Input: s = "a#c", t = "b" Output: false Explanation: s = "c", t = "b". 🧠 Concepts Used Stack / StringBuilder for backspace simulation Character traversal from left to right Two-pointer optimization (O(1) space approach) String manipulation & comparison ⚙️ Approach 1️⃣ Traverse each string and simulate typing: • If the character isn’t #, add it. • If it’s #, remove the last character (if present). 2️⃣ Compare the final processed versions of both strings. 3️⃣ Alternatively, use a two-pointer approach from the end for constant space optimization. 🚀 What I Learned Small-looking problems can teach efficient simulation techniques. Understanding string traversal logic is crucial for interview-ready problem solving. #100DaysOfCode #Java #ProblemSolving #CodingJourney #LeetCode #Algorithms #Strings #Stack #TwoPointers #CodingChallenge #KeepBuilding
To view or add a comment, sign in
-
-
🌬️ Airflow Mistakes That Kill Your Pipelines (Part 1) 🚀 Most Airflow DAGs don’t fail because of Python. They fail because of design choices that make workflows brittle, unreadable, or impossible to scale. Here are 3 practices that saved me hours in production 👇 --- ✅ 1. Keep DAGs Declarative, Not Procedural Why: DAGs should describe what runs, not embed heavy logic. `python with DAG("etl_pipeline") as dag: extract = PythonOperator(...) transform = BashOperator(...) extract >> transform ` Impact: Clean DAGs, easier debugging. --- ✅ 2. Use Task Groups for Clarity Why: Complex DAGs become unreadable without grouping. `python with TaskGroup("loadstage") as loadgroup: load_a = PythonOperator(...) load_b = PythonOperator(...) ` Impact: Simplifies the UI and onboarding. --- ✅ 3. Avoid Passing Large Data via XComs Why: XComs live in the metadata DB — not built for big payloads. Best Practice: Pass IDs or flags via XCom, store large data in S3/DB. Impact: Prevents DB bloat and keeps jobs fast. --- 💡 Final Thought Airflow isn’t just about triggering jobs — it’s about designing workflows that scale and recover gracefully. 👉 What’s one Airflow mistake you’ve seen in production? #Airflow #WorkflowOrchestration #DataEngineering #ETL #PipelineDesign #Observability #ProductionReady #TechTips #Python
To view or add a comment, sign in
-
Lazy imports in Python are useful but limited by obscuring of typing information. In my latest blog post, I discuss a method to create typing information that more people should know about. https://lnkd.in/en_VfCfA
To view or add a comment, sign in
-
🚀 𝗦𝗽𝗲𝗲𝗱 𝗨𝗽 𝗬𝗼𝘂𝗿 𝗣𝘆𝘁𝗵𝗼𝗻 𝗣𝗿𝗼𝗴𝗿𝗮𝗺𝘀 𝘄𝗶𝘁𝗵 𝗖𝗼𝗻𝗰𝘂𝗿𝗿𝗲𝗻𝗰𝘆! 🚀 Ever felt like your Python apps could be faster? The secret often isn’t 𝗺𝗼𝗿𝗲 𝗰𝗼𝗱𝗲, it’s 𝗯𝗲𝘁𝘁𝗲𝗿 𝗰𝗼𝗻𝗰𝘂𝗿𝗿𝗲𝗻𝗰𝘆. Concurrency lets your program handle 𝗺𝘂𝗹𝘁𝗶𝗽𝗹𝗲 𝘁𝗮𝘀𝗸𝘀 𝗮𝘁 𝘁𝗵𝗲 𝘀𝗮𝗺𝗲 𝘁𝗶𝗺𝗲, unlocking massive performance gains. It’s especially powerful for I/O-bound tasks (like API calls or file operations) and CPU-bound tasks (like data crunching or heavy computations). ⚙️ 𝗧𝗵𝗲 3 𝗠𝗮𝗶𝗻 𝗪𝗮𝘆𝘀 𝘁𝗼 𝗗𝗼 𝗖𝗼𝗻𝗰𝘂𝗿𝗿𝗲𝗻𝗰𝘆 𝗶𝗻 𝗣𝘆𝘁𝗵𝗼𝗻 🧵 1. 𝗧𝗵𝗿𝗲𝗮𝗱𝗶𝗻𝗴 Perfect for I/O-bound tasks. Even though Python’s GIL limits true parallelism, 𝘁𝗵𝗿𝗲𝗮𝗱𝘀 are great for handling multiple network requests or file reads simultaneously, making your app feel faster and more responsive. ⚡ 2. 𝗔𝘀𝘆𝗻𝗰𝗜𝗢 Modern, elegant, and event-driven. With 𝗮𝘀𝘆𝗻𝗰 and 𝗮𝘄𝗮𝗶𝘁, you can run thousands of concurrent I/O operations efficiently. Great for APIs, web scraping, or anything that waits on external data. 🧠 3. 𝗠𝘂𝗹𝘁𝗶𝗽𝗿𝗼𝗰𝗲𝘀𝘀𝗶𝗻𝗴 The key to 𝘁𝗿𝘂𝗲 𝗽𝗮𝗿𝗮𝗹𝗹𝗲𝗹𝗶𝘀𝗺 in Python. By running multiple processes, you can fully use all CPU cores. Perfect for data processing, simulations, or ML workloads. 🧩 𝗖𝗵𝗼𝗼𝘀𝗶𝗻𝗴 𝘁𝗵𝗲 𝗥𝗶𝗴𝗵𝘁 𝗧𝗼𝗼𝗹 • For web scraping or API calls → use 𝘁𝗵𝗿𝗲𝗮𝗱𝗶𝗻𝗴 or 𝗮𝘀𝘆𝗻𝗰𝗶𝗼 • For CPU-heavy tasks → go with 𝗺𝘂𝗹𝘁𝗶𝗽𝗿𝗼𝗰𝗲𝘀𝘀𝗶𝗻𝗴 The right choice can transform a slow, blocking program into a fast and scalable one. Understanding concurrency is a game-changer for performance-minded developers. Once you get it right, you’ll never look at sequential code the same way again. What’s your go-to concurrency model in Python? 💬 Drop your thoughts in the comments. Let’s share some performance wisdom! #Python #Concurrency #AsyncIO #Threading #Multiprocessing #Performance #SoftwareEngineering #Backend #PythonDeveloper #TechTips #DevCommunity
To view or add a comment, sign in
-
-
𝑷𝒚𝒕𝒉𝒐𝒏: 𝐋𝐢𝐬𝐭 vs 𝐒𝐞𝐭 vs 𝐓𝐮𝐩𝐥𝐞 — 𝘛𝘩𝘦 𝘊𝘭𝘦𝘢𝘳𝘦𝘴𝘵 𝘌𝘹𝘱𝘭𝘢𝘯𝘢𝘵𝘪𝘰𝘯 𝘌𝘷𝘦𝘳 When you're starting Python, three data structures show up everywhere: List, Set, and Tuple. They look similar… but behave completely differently. Here’s a crystal-clear breakdown you can save for future reference 👇 🔹 𝐋𝐈𝐒𝐓 — 𝘖𝘳𝘥𝘦𝘳𝘦𝘥 | 𝘔𝘶𝘵𝘢𝘣𝘭𝘦 | 𝘈𝘭𝘭𝘰𝘸𝘴 𝘋𝘶𝘱𝘭𝘪𝘤𝘢𝘵𝘦𝘴 A List is like a flexible shopping list. Example: 1, 3, 2, 1, 3 → [1, 3, 2, 1, 3] 𝘒𝘦𝘺 𝘗𝘰𝘪𝘯𝘵𝘴: ↳ Order preserved ↳ Add / remove / replace anytime ↳ Duplicates allowed ↳ Uses [ ] Best used when: You need a 𝐝𝐲𝐧𝐚𝐦𝐢𝐜 collection where order matters. 🔹 𝐒𝐄𝐓 — 𝘜𝘯𝘰𝘳𝘥𝘦𝘳𝘦𝘥 | 𝘔𝘶𝘵𝘢𝘣𝘭𝘦 | 𝘕𝘰 𝘋𝘶𝘱𝘭𝘪𝘤𝘢𝘵𝘦𝘴 A Set behaves like a bag that keeps unique items. Example: 1, 3, 2, 1, 3 → {1, 2, 3} Key Points: ↳ No order maintained ↳ Add / remove items ↳ Removes duplicates automatically ↳ Uses { } Best used when: You need uniqueness + fast membership checks. 🔹 𝐓𝐔𝐏𝐋𝐄 — 𝘖𝘳𝘥𝘦𝘳𝘦𝘥 | 𝘐𝘮𝘮𝘶𝘵𝘢𝘣𝘭𝘦 | 𝘈𝘭𝘭𝘰𝘸𝘴 𝘋𝘶𝘱𝘭𝘪𝘤𝘢𝘵𝘦𝘴 A Tuple is a sealed container — once created, it cannot be changed. Example: 1, 3, 2, 1, 3 → (1, 3, 2, 1, 3) Key Points: ↳ Order preserved ↳ Cannot modify once created ↳ Duplicates allowed ↳ Uses ( ) Best used when: You want fixed data that should not change. The attached gif illustrates the python collections concept, though it's not 100% perfect. 📌 If you like my posts, please follow Sreenidhi Rajakrishnan and hit the 🔔 on my profile to get notifications for all my new posts. #programming #coding #ai #automation #visualization #generators #python #pythonprogramming #pythonlearning #qa #automation #selenium #interviewtips #qainterview #manualtesting #java
To view or add a comment, sign in
-
-
Difference between Python2 and Python3? Here is the few differences between Python2 and Python3 1. Print Statement vs Print Function 2. Integer Division Behavior 3. Unicode String Support 4. xrange() vs range() 5. Error Handling Syntax 6. Input Function Behavior 7. Iterators and Generators in dict Methods 8. Library and Package Compatibility 9. Function Annotations Support 10. The next() Function Usage 11. Standard Library Reorganization 12. Performance Improvements 13. String Formatting Methods 14. New Syntax for Integer Literals 15. Exceptions Syntax Change 16. Async and Await Support 17. Function Arguments with Default Values 18. Unicode and str Separation 19. Type Annotations for Variables 20. Simplified super() Function 21. Handling of Empty Dicts and Sets 22. Removal of cmp() Function 23. Removal of long Type 24. time Module Changes 25. os Module Modifications 26. socket Module Enhancements 27. Improved finally Block Behavior 28. Updated Metaclass Syntax 29. Removal of basestring Class 30. input() vs raw_input() Difference 31. Revised next() Function Implementation 32. range() Return Type Change 33. Improved callable() Function Behavior 34. Renamed Standard Library Modules 35. Enhanced String Formatting (format, f-strings) 36. Improved Integer Literal Syntax
To view or add a comment, sign in
Explore related topics
Explore content categories
- Career
- Productivity
- Finance
- Soft Skills & Emotional Intelligence
- Project Management
- Education
- Technology
- Leadership
- Ecommerce
- User Experience
- Recruitment & HR
- Customer Experience
- Real Estate
- Marketing
- Sales
- Retail & Merchandising
- Science
- Supply Chain Management
- Future Of Work
- Consulting
- Writing
- Economics
- Artificial Intelligence
- Employee Experience
- Workplace Trends
- Fundraising
- Networking
- Corporate Social Responsibility
- Negotiation
- Communication
- Engineering
- Hospitality & Tourism
- Business Strategy
- Change Management
- Organizational Culture
- Design
- Innovation
- Event Planning
- Training & Development