#DSAChallengeDay53 Today’s challenge was a neat exercise in bit manipulation + set intersection logic 🔠⚡ 📌 Problem Solved Today (LeetCode - 3120): Count the Number of Special Characters I ✅ Logic: The task is to count characters that appear in both lowercase and uppercase in the string. I solved this using bit manipulation for efficient tracking: ✅ Traversed the string and tracked lowercase characters using a bitmask ✅ Tracked uppercase characters using a separate bitmask ✅ Used bitwise AND (lower & upper) to find common characters ✅ Counted set bits using Integer.bitCount() to get the final answer 💡 Learnings: 🔹 Bit manipulation is a powerful way to optimize space and speed for character tracking 🔹 Using bitmasks avoids extra data structures like sets/maps 🔹 The intersection of two bitmasks directly gives the required result 🔹 Efficient solution with O(n) time and O(1) space 🚀 This problem reinforced how low-level techniques like bit operations can simplify seemingly straightforward problems and make solutions more elegant. #DSAChallengeDay53 #100DaysOfDSA #LeetCode #LeetCodeDailyChallenge #BitManipulation #Java #DSA #CodingJourney #KeepCoding #OptimizationTricks #geekstreak2025 #personalguidanceprogram #pgpbyanchal
Count Special Characters with Bit Manipulation
More Relevant Posts
-
🔥 Day 148/360 – Learning Something New Today 🚀 This looks tricky at first… but it’s actually very simple 👀 Let’s break it down 👇 📌 Topic: Bit Manipulation 🧩 Problem: Reverse Bits 📝 Problem Statement: Reverse all 32 bits of a given integer 🔍 Example: Input: 43261596 Output: 964176192 💡 Approach: Bit Manipulation (Bitwise Operators) ✔ Step 1 – Extract last bit using (n & 1) ✔ Step 2 – Shift result left and add bit ✔ Step 3 – Shift input right and repeat 32 times ⚡ Key Idea: Build the reversed number bit by bit by shifting and appending the least significant bit ⏱ Complexity: Time → O(1) Space → O(1) #DSA #Java #Coding #ProblemSolving #InterviewPrep #LeetCode #TechJourney #148DaysOfCode
To view or add a comment, sign in
-
-
Daily LeetCode done ✅ Today’s question: Number of Special Characters 🚀 At first glance, the problem looks very straightforward… but the interesting part was handling the order condition carefully. 💡 Idea behind my approach: We need to count characters where: ✅ lowercase appears first ✅ uppercase appears later So instead of storing frequencies, I tracked INDEX POSITIONS. 🔹 For lowercase letters: → store the latest occurrence index 🔹 For uppercase letters: → store only the first occurrence index using putIfAbsent() Then for every lowercase character: if lastIndex(lowercase) < firstIndex(UPPERCASE) ➡️ it becomes a valid “special character”. Example: aAbBc ✔️ ‘a’ appears before ‘A’ ✔️ ‘b’ appears before ‘B’ Answer = 2 One thing I’m enjoying lately: trying to make solutions cleaner every time I revisit patterns. 🚀 Even simple problems improve: ⚡ map usage ⚡ edge case thinking ⚡ observation skills ⚡ implementation clarity That consistency matters more than difficulty tags. 💯 #LeetCode #Java #DSA #ProblemSolving #CodingJourney #CompetitiveProgramming #Algorithms #SoftwareEngineering #DailyCoding LinkedIn: @rajgupta40
To view or add a comment, sign in
-
-
🚀 Day 33 of #DSAChallenge Solved Reverse Integer (LeetCode #7) today 🔢 💡 Key Learnings: - Handling integer overflow is the real challenge here - Cannot use 64-bit integers → must carefully check limits before updating result - Used a simple loop + digit extraction ("x % 10") - Important condition: - If "rev > Integer.MAX_VALUE/10" or "rev < Integer.MIN_VALUE/10" → return 0 🧠 Concepts Covered: - Math-based digit manipulation - Edge case handling (negative numbers, trailing zeros) - Overflow detection logic ⚡ Approach: 1. Extract last digit 2. Check overflow before multiplying 3. Build reversed number step-by-step ✅ Result: Accepted ✔️ (Runtime: 0 ms) Consistency > Intensity. Showing up every day 💪 #Day33 #LeetCode #Java #DSA #CodingJourney #PlacementPrep #ProblemSolving #100DaysOfCode
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
-
-
💡 Day 44 – Solved Problem #208: "Implement Trie (Prefix Tree)" using Java! Today I explored an important data structure used in real-world applications like autocomplete and search systems 🌳 🔍 Problem: Design a Trie (Prefix Tree) with insert, search, and prefix search operations. 🧠 Key Insight: A Trie stores words character by character, allowing efficient prefix-based operations. 🚀 Approach: Used an array of size 26 at each node to represent lowercase English letters and a boolean flag to mark complete words. ⏱ Time Complexity: O(m) per operation 📦 Space Complexity: O(26 × m) This problem helped me understand how powerful tree-based structures can optimize string operations! #Day50 #LeetCode #Problem208 #Java #Trie #DataStructures #CodingJourney
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 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
To view or add a comment, sign in
-
-
Today’s LeetCode Daily Problem 💻 LeetCode 3121. Count the Number of Special Characters II => Problem Task: Count Special Characters. For a character to be special, two conditions must be satisfied: • It must appear in both lowercase and uppercase. • All lowercase occurrences must come before the first uppercase occurrence. => Constraints: 1 <= word.length <= 2 * 10^5 Checking all lowercase-uppercase pairs manually would lead to an O(n^2) approach: (2 * 10^5)^2 > 10^8, which is far beyond acceptable limits. => Core Intuition: Instead of checking every pair manually, we only need: • the last occurrence of each lowercase character • the first occurrence of each uppercase character If last lowercase index < first uppercase index, then every lowercase occurrence naturally appears before every uppercase occurrence, making that character special. => One small optimization I used: Instead of initializing arrays with -1, I kept the default 0 initialization and stored indices as i+1. This avoids extra initialization work while also helping differentiate between: • 0 -> character not present • >0 -> valid stored index => Complexity • Time Complexity: O(N) • Space Complexity: O(1) => My Step-by-Step Detailed Solution with Dry Run: https://lnkd.in/eFX7iu6e Feel free to share your approach in the comments. #leetcode #leetcodedailycodingchallenge #java #dsa #programming #coding #softwareengineering #competitiveprogramming #algorithms #computerscience
To view or add a comment, sign in
-
-
🚀 Day 57/100 – LeetCode DSA Challenge 📌 Problem: 1365. How Many Numbers Are Smaller Than the Current Number 💡 Problem Statement : Given an array nums, for each element nums[i], count how many numbers in the array are smaller than it (excluding itself). 🧠 Approach to Solve: 🔹 Brute Force Approach (Easy & Intuitive): Use two loops For each element, compare it with all others Count how many are smaller Store result in output array 🔹 Better Approach (Sorting): Create a copy of array and sort it First occurrence index of each number = its count of smaller elements Use HashMap to store value → count 📚 What I Learned Today: ✔️ How to apply nested loops for comparison problems ✔️ Importance of index-wise result storage ✔️ Difference between brute force and optimized approaches ✔️ Strengthened problem-solving thinking in arrays ✨ Every day one problem closer to success! #Day57 #100DaysOfCode #LeetCode #DSA #Java #CodingJourney #Consistency #ProblemSolving
To view or add a comment, sign in
-
-
LeetCode Problem 29: Divide Two Integers – 0ms Solution After revisiting one of the classic "medium" integer problems, I came across a surprisingly straightforward edge-case–aware implementation that runs in 0ms and beats 100% in runtime. The catch? · You cannot use division, multiplication, or modulo. · You must handle 32-bit signed integer overflow gracefully. The core insight? 👉 Almost every failing solution misses the Integer.MIN_VALUE / -1 edge case. 👉 Handle that first, and the rest falls into place cleanly. ✅ Runtime: 0 ms (100%) ✅ Memory: ~42.7 MB (48th percentile) Not every solution needs to re-invent bitwise long division. Sometimes, simplicity wins – once you know where the trap lies. 🎯 Key takeaway: In coding interviews, edge cases > cleverness. #leetcode #divideTwoIntegers #codinginterview #java #algorithms #problemsolving #datastructures #codingchallenge #softwareengineering #interviewprep #techinterview #100DaysOfCode #bigintegers #edgecases #runtimeoptimization
To view or add a comment, sign in
-