Today I worked on a classic string manipulation problem that looks simple but tests your understanding of logic and edge cases. 🔍 Problem: Given a string and a substring, count how many times the substring appears in the main string. ⚠️ The catch? 👉 You must count overlapping occurrences as well. 💡 Approach I Used: Instead of using built-in shortcuts, I applied a sliding window technique: ++Loop through the string ++Extract a substring of the same length ++Compare it with the target substring ++Increment count when matched This ensures we don’t miss overlapping patterns. 🧠 Key Learning: Sometimes, simple problems reveal powerful concepts. This one reinforces: ++String slicing ++Loop boundaries ++Sliding window logic 📌 Example: "ABCDCDC" → "CDC" appears 2 times (including overlap) 💻 Check out the visuals: 🖼️ Problem breakdown 🧑💻 Python solution 🔥 Why this matters? This pattern is widely used in: --Text processing --Pattern matching --Data parsing #Python #HackerRank #CodingPractice #DataStructures #ProblemSolving #100DaysOfCode #LearningInPublic #SoftwareEngineering
Harivignesh D’s Post
More Relevant Posts
-
I got tired of scrolling through messy file names… so I fixed it with a small Python script. While reading One Piece manga PDFs, the file names were all over the place: chapter-1112, one-piece-chapter-1222, onepiece-1123, OP-Chapter-1123… Finding the correct order every time was annoying. So I wrote a simple script that: Extracts the chapter number from any format Renames files into a consistent structure Automatically arranges them in readable order Nothing fancy just solving a small personal problem and saving time. This reminded me: You don’t always need big projects. Even small scripts that remove friction from your daily life are worth building. Clean input → Clean output → Peace of mind 😌 #Python #LearningByDoing #Automation #OnePiece #Coding
To view or add a comment, sign in
-
-
🚀 Solved a great problem today: “Consecutive 1’s Not Allowed” At first glance, it looked like a simple binary string problem… but it quickly turned into a lesson in pattern recognition and dynamic thinking. 📌 What the problem was about: Count all binary strings of length n such that no two 1’s are consecutive. 💡 What I learned: Instead of brute forcing all combinations (which would be exponential), the key was to observe a pattern: If a string ends with 0 → we can add 0 or 1 If it ends with 1 → we can only add 0 This leads to a recurrence: 👉 dp[n] = dp[n-1] + dp[n-2] Which is basically the Fibonacci pattern in disguise. 🧠 Big takeaway: Many problems are not about coding harder… they’re about seeing the hidden pattern behind the problem. This was a reminder that: Brute force is rarely the answer Thinking in terms of state transitions is powerful Optimization often comes from observation, not syntax 📷 Sharing my solution screenshot below 👇 #DataStructures #DynamicProgramming #ProblemSolving #Python #LearningInPublic #DataAnalyticsJourney
To view or add a comment, sign in
-
-
Today I worked on an interesting string problem — counting how many times a substring appears in a string without using built-in methods like count(). At first, it seemed straightforward… until I realized an important twist 👇 👉 Built-in count() does not handle overlapping substrings So I implemented a manual sliding window approach: 🔹 Traverse the string from left to right 🔹 Extract substrings using slicing 🔹 Compare each slice with the target substring 🔹 Increment count when a match is found 💡 Example: String → ABCDCDC Substring → CDC There are 2 occurrences, not 1 — because overlapping is allowed. This small problem helped me understand: How string slicing works internally Why built-in functions aren’t always sufficient The importance of handling edge cases like overlapping 🧠 Key takeaway: Sometimes writing logic manually gives deeper insight than relying on shortcuts. Learning step by step and enjoying the process 🔥 #Python #CodingJourney #100DaysOfCode #ProblemSolving #DataStructures #Learning
To view or add a comment, sign in
-
Ever tried to sort a list and ended up with None? The logic looks correct: nums = [3, 1, 2] sorted_nums = nums.sort() print(sorted_nums) 👉 Output: None Why did it fail? In Python, there is a big difference between a Method that modifies an object and a Function that returns a new one. 1️⃣ .sort() is a Method: It modifies the original list "in-place." It doesn't need to return anything because the work is done directly on the original variable. 2️⃣ sorted() is a Function: It creates a brand-new list and leaves the original one exactly as it was. Use .sort() when you want to save memory and don't need the original order anymore. Use sorted() when you need to keep your original data safe and want a new sorted version. #Python #30DaysOfCode #BCA #LearningInPublic #Day22 #JECRC
To view or add a comment, sign in
-
-
Python Clarity Series – Episode 23 Topic: Floating Point Precision Issue 🤯 Why does this happen? print(0.1 + 0.2) Output: 0.30000000000000004 ❗ 👉 This is NOT a Python bug. It’s due to how floating-point numbers are stored in binary. 💡 Fix (when needed): round(0.1 + 0.2, 1) Output: 0.3 💡 Concept: Computers approximate decimal values internally. Important in: ✔ Financial calculations ✔ Data Science Don’t ignore this. #PythonConcepts #FloatingPoint #RealWorldCoding #python #clarity
To view or add a comment, sign in
-
-
Day 12/30 🔹 Problem: Print multiplication table of a number 🔹 What I focused on today: Using loops to repeat calculations efficiently 🔹 My Thinking Process: Take a number as input Use a loop from 1 to 10 Multiply the number with each value Print the result step by step 👉 Repetition becomes easy with loops 🔹 Inputs I used: A number 🔹 Code: num = int(input("Enter a number: ")) for i in range(1, 11): result = num * i print(num, "x", i, "=", result) 🔹 Example: Input: 5 Output: 5 x 1 = 5 5 x 2 = 10 5 x 3 = 15 ... 5 x 10 = 50 🔹 Key Takeaway: Loops help automate repetitive tasks, making code more efficient and scalable #Day12 #Python #30DaysOfCode #LearningInPublic #DataAnalytics #ProblemSolving
To view or add a comment, sign in
-
-
I bet your advisor has already told you to make your "code faster"! Want to solve the problem? Then use an emulator, my friend! We have released PyGLAM 0.2.2 (beta version), a Python framework for emulating probability distributions based on Generalized Lambda Distributions. Our implementation is based on the paper "Replication-based emulation of the response distribution of stochastic simulators using Generalized Lambda Distributions" by Professor Bruno Sudret. Thanks to the partners Prof. Ketson dos Santos, Prof. Marcos Luiz, and the students Victor Hugo Moreira and Renata Maria Pensin. Simple to install: pip install pyglam Learn all about it at: https://lnkd.in/gutJNpDj Feedback is more than welcome! 👇 #Python #DataScience #Math #Coding #Reliability #Reliability #Statistics #Mechanics #CivilEngineering
To view or add a comment, sign in
-
🚨 This behavior of Python might look like a BUG… but it isn’t actually. a = 10 b = 10 print(id(a)) print(id(b)) 👉 Same memory location 😲 “Why do we have two variables pointing to the same memory location?!” Here comes the second one and things get interesting 👇 a = [1, 2, 3] b = a b.append(4) print(a) # [1, 2, 3, 4] 🔥 👉 Hmmm… why did ‘a’ change?! 💡 Explanation: ⭐ id() returns the identity of an object ⭐ Python reuses memory locations for immutable values ⭐ For mutable objects however, there is no copying, just pointers! ⚠️ The misconception: Most people believe ‘=’ copies objects in variables. 👉 Nope! ✅ Solution: b = a.copy() Now the two variables are separate ✅ 🔥 Consequence: It can seriously mess up your program’s logic! Ever got caught by such a ghost bug in Python? 👇 #CodeWithSujith #Python #Programming #Coding #PythonTricks #LearnPython #PythonBeginner #100DaysOfCode #DeveloperJourney
To view or add a comment, sign in
-
-
📊 Comparing Two Outlier Removal Approaches in Python When cleaning datasets, how you remove outliers matters more than you think. I recently compared two common strategies: 1️⃣ Column-wise removal – Drop outliers sequentially, one column at a time. 2️⃣ Dataset-level removal – Flag all outliers across the entire dataset first, then remove them together. 🔍 What I found: The column-wise approach changes the IQR bounds after each removal, causing many non‑outlier rows to be wrongly filtered out (545 → 365 rows). The dataset-level approach respects original distributions, removes only true outliers (545 → 463 rows), and avoids over‑cleaning. ✅ Takeaway: Always identify outliers globally before removing them – your data will thank you. 📁 Used Python, pandas, IQR method, and a housing dataset. 🔗 Full code & notebook: https://lnkd.in/gheGYYEz #DataScience #Python #OutlierDetection #DataCleaning #Pandas #MachineLearning
To view or add a comment, sign in
-
🚀 Day 22 of My Python Journey – Solved Permutation in String! 💻✨ Today I worked on checking whether one string’s permutation exists in another string ✅ This problem was a great mix of strings + sliding window technique. 📌 Problem Statement Given two strings s1 and s2, return true if s2 contains a permutation of s1. In simple terms → check if any rearrangement of s1 exists as a substring in s2. Example: s1 = "ab" s2 = "eidbaooo" ✅ Output: true (because "ba" exists) 🔍 My Approach – Sliding Window + Frequency Count Instead of generating all permutations (which would be expensive), I used a smarter approach: Maintain frequency arrays for both strings Use a sliding window of size len(s1) over s2 Compare frequency counts at each step If they match → permutation found This avoids unnecessary computations and keeps it efficient 🚀 💡 Key Learning This problem helped me understand: ✔ Sliding window technique ✔ Frequency counting (arrays instead of maps) ✔ Optimizing brute-force solutions ✔ String pattern matching ⏱ Complexity Time Complexity: O(n) Space Complexity: O(1) (fixed size 26 array) Really enjoyed this one because it shows how powerful sliding window can be 🔥 #Python #DSA #SlidingWindow #Strings
To view or add a comment, sign in
-
More from this author
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