🚀 LeetCode Daily Problem Explanation for June 4th! 🚀 -- These are my accepted solutions, I would appreciate any optimisations in the comments !! -- 📝 Problem: Given a string s which consists of lowercase or uppercase letters, return the length of the longest palindrome that can be built with those letters. Letters are case sensitive, for example, "Aa" is not considered a palindrome. 🏷️ Tag : Easy 🔍 Examples: Example 1: Input: s = "abccccdd" Output: 7 Explanation: One longest palindrome that can be built is "dccaccd", whose length is 7. Example 2: Input: s = "a" Output: 1 Explanation: The longest palindrome that can be built is "a", whose length is 1. 💡 Intuition A simple observation can be made: for a string to form a palindrome, all characters should have even counts, or all characters should have even counts except for one character that can have an odd count. 🛠️ Approach 1️⃣ Count the frequency of each character in the string using two arrays: one for lowercase letters and one for uppercase letters. 2️⃣ Calculate the number of characters that have an odd count. 3️⃣ If there are no characters with an odd count, the length of the longest palindrome is the length of the string. 4️⃣ If there are characters with odd counts, the length of the longest palindrome is the length of the string minus the number of odd counts plus one (since we can place one odd character in the center). ⏲️ Complexity Time complexity: 𝑂(𝑛) O(n), where 𝑛 is the length of the string. We iterate over the string to count character frequencies and then iterate over the counts. Space complexity: 𝑂(1) O(1), because the space used for the frequency arrays is constant (52 elements). -- Follow Kashyap Kale for daily code explanations -- #Coding #Programming #Algorithm #Tech #SoftwareEngineering #Developer #CodeNewbie #LinkedInTech #ProblemSolving #DSA #interviews #Cpp
Kashyap Kale’s Post
More Relevant Posts
-
Day 1: Solving LeetCode's Top 150 Interview Questions 🚀 LeetCode Challenge Solved: Minimum Size Subarray Sum 🚀 I'm excited to share that I’ve solved the "Minimum Size Subarray Sum" problem, one of LeetCode's top 150 interview questions! 🔍 Problem Statement: Find the minimal length of a contiguous subarray of which the sum is greater than or equal to a given target. If no such subarray exists, return 0. 💡 Solution Approach: Using a two-pointer technique to maintain a sliding window, I efficiently identified the smallest subarray that meets the criteria. By dynamically adjusting the window size, I ensured optimal performance with an O(n) time complexity. Feel free to check out my solution and discuss any optimizations or alternative approaches! #LeetCode #CodingChallenge #InterviewPreparation #C++ #Algorithms #ProblemSolving
To view or add a comment, sign in
-
Day 29/100 of 100-day DSA Q.1 https://lnkd.in/gMkNfq-j Q.2 https://lnkd.in/gAaij9Wu #DataStructures #Programming #Coding #Tech #ComputerScience #SoftwareEngineering #LeetCode #CodeOptimization #TechCommunity #ProblemSolving #Cpp #ProgrammingChallenges #TechSkills #100DaysOfCode #100DaysOfDSA #DSAChallenge #DailyCoding #CodeEveryday #CodingChallenge #Backtracking #Stacks #Queues
To view or add a comment, sign in
-
Mastering Arrays: 56 Interview Questions for DSA Success. #DataStructure #array #algorithms #interview #programming
Mastering Arrays: 56 Interview Questions for DSA Success
dev.to
To view or add a comment, sign in
-
🚀 Day 9/100: of my #100daysofLeetCode Challenge 🚀 Today’s problem was a big one :- Median of Two Sorted Arrays. 🔢 It’s a popular problem known for its complexity and relevance in technical interviews. 💡 Problem Summary: You’re given two sorted arrays, and the task is to find the median of the combined array in O(log(min(m, n))) time complexity. Sounds simple? The constraints make it tricky! 📚 Key Insights: A brute force solution would involve merging the arrays, but it doesn’t meet the logarithmic complexity requirement. The real challenge lies in using binary search to efficiently find the partition that divides the two arrays into halves with the desired properties. This problem emphasizes the importance of thinking beyond standard iterative approaches to solve advanced problems. 🎯 Takeaways: ⭐ DSA-Java in programming Problem Link: 👉 LeetCode - https://lnkd.in/dR_D_TZj This problem pushed me to step outside my comfort zone and explore advanced techniques like binary search in a non-traditional context. Challenges like these are what make the journey so rewarding! Excited to keep this momentum going into Day 10. 💪 #DSAwithedSlash #100DaysOfCode #LeetCode #BinarySearch #MedianOfTwoSortedArrays #CodingJourney #DSAinJavaProgramming
To view or add a comment, sign in
-
If you want to become good at cracking coding interviews, then learn these templates [part 2]: 7) Monotonic Stack: - https://lnkd.in/e7sUDtkS 6) Sum: - https://lnkd.in/e9wPU_ix 5) Bit Manipulation: - https://lnkd.in/enVAs_4c - https://lnkd.in/eAfxpEax 4) Word Break: - https://lnkd.in/eZJhZbeX 3) Trie: - https://lnkd.in/ezMcbZKX 2) Combination: - https://lnkd.in/eRsFVm64 1) Backtracking: - https://lnkd.in/e7c6XbdG - https://lnkd.in/eGyS6q3R - https://lnkd.in/eTZDncpG PS - Join 53,001+ like-minded software engineers learning system design: https://lnkd.in/ed4URjuY Also Part 1 if you missed it: https://lnkd.in/et3Tccu8 --- If you liked this post. 🔔 Follow: Neo Kim ♻ Repost to help others find it. 💾 Save it for future reference. #programming #coding #systemdesign #softwaredevelopment #leetcode
To view or add a comment, sign in
-
🚀 LeetCode Challenge: [🔥Day 307] 🔍 Problem Title: ❓Combination Sum II 📝 Description: 🧐Given a collection of candidate numbers (candidates) and a target number (target), find all unique combinations in candidates where the candidate numbers sum to target. Each number in candidates may only be used once in the combination. Note: The solution set must not contain duplicate combinations. Example 1: Input: candidates = [10,1,2,7,6,1,5], target = 8 Output: [ [1,1,6], [1,2,5], [1,7], [2,6] ] Example 2: Input: candidates = [2,5,2,1,2], target = 5 Output: [ [1,2,2], [5] ] Constraints: 1 <= candidates.length <= 100 1 <= candidates[i] <= 50 1 <= target <= 30 🔗Link to Problem: https://lnkd.in/dPyJyfVn ✨ Why this Challenge is Worth Your Time: Sharpen your problem-solving skills. Enhance your algorithmic understanding. Perfect for interview preparation! #LeetCode #CodingChallenge #ProblemSolving #Tech #Coding #Programming #SoftwareDevelopment #Algorithm
To view or add a comment, sign in
-
🚀 LeetCode's Top 150 Interview Questions 🚀 🌟 Question: 5 Majority Element 🌟 Problem Statement: Given an array nums of size n, return the majority element. The majority element is the element that appears more than ⌊n / 2⌋ times. You may assume that the majority element always exists in the array. Example 1: 📥 Input:nums = [3,2,3] 📤 Output:3 Example 2: 📥 Input:nums = [2,2,1,1,1,2,2] 📤 Output:2 Constraints: n == nums.length 1 <= n <= 5 * 10^4 -10^9 <= nums[i] <= 10^9 Solution (Moore's Voting algorithm) package top150InterviewQuestion; public class MajorityElement { public static int majorityElement(int nums[]) { int candidate = 0; int count = 0; for (int i = 0; i < nums.length; i++) { if (count == 0) { candidate = nums[i]; } if (nums[i] == candidate) { count++; } else { count--; } } return candidate; } public static void main(String[] args) { int nums[] = {1,1,2,2,2}; MajorityElement obj = new MajorityElement(); int result = obj.majorityElement(nums); System.out.println(result); } } In this solution, we use Moore's Voting Algorithm to find the majority element efficiently. The algorithm maintains a candidate and a count, updating them as we iterate through the array to find the element that appears more than half the time. Stay tuned for more solutions as I continue my journey through LeetCode's Top 150 Interview Questions! 🔖 #LeetCode #Coding #InterviewPrep #Java #ProblemSolving #Tech #CodingChallenge #DeveloperLife #TechCareer #LearnToCode #Programmer #JavaProgramming #TechJourney #TechCommunity #SoftwareDevelopment #CodingLife
To view or add a comment, sign in
-
Progressing through the Trees Arc! 🌳⚔️ Today’s quest: Check if one binary tree is a subtree of another. Given the roots of two trees, I need to determine if the smaller tree appears as a subtree within the larger one—structure and values must match! Topics covered: - Trees - Depth-First Search - Recursion Time to level up my skills and tackle this subtree challenge! 💻💪 #LeetCode #CodingChallenge #DataStructures #SoftwareEngineering #TreeAlgorithms #DepthFirstSearch #Recursion #TechCareer #Hiring #Programming #FullStackDevelopment #CodingJourney #JavaScript #Recruitment
To view or add a comment, sign in
-
🚀 Day 63 Challenge: Finding the Intersection of Two Linked Lists! 🚀 Today, I tackled the problem of identifying the intersection node of two singly linked lists. Algorithm and Time complexity: To find the intersection of two linked lists, I initialized two pointers, each starting at the head of one of the lists. These pointers traverse the lists one node at a time. When a pointer reaches the end of its list, it switches to the head of the other list. This way, both pointers traverse each list at most once. If the pointers are equal at any point, that node is the intersection node. If they both reach the end without meeting, it means there is no intersection. The algorithm efficiently finds the intersection node, if it exists, in linear time O(m + n), where m and n are the lengths of the two linked lists. It also uses a constant amount of extra space, making the space complexity O(1). This solution is both time and space-efficient, showcasing an elegant approach to solving a common interview question. #100DaysOfCode #Day63 #Java #CodingChallenge #LinkedLists #Algorithm #Tech
To view or add a comment, sign in
-
AlgoTutor 's Comprehensive DSA Sheet: Your Ultimate Guide to Mastering Algorithms Credit for this resource goes to AlgoTutor a platform that has assisted hundreds of aspirants in clearing their dream tech role interviews by mastering DSA and System Design concepts. Moreover, they offer dedicated placement support to ensure that talented aspirants secure their desired positions in their dream companies. To take the first step towards achieving your tech career goals. Book a free demo class to get a 10% carly bird discount- https://lnkd.in/dQ7g8-a8 #algotutor #algotutornotes #algorithm #programming #coding #techskills #AlgoTutor #sql #coding #python #developer #javascript #programmerjava #programming #software #html #database #sqlserver
To view or add a comment, sign in
Founding SDE at Cad and Cart | NextJs | React Native 📱 | AWS
7moHere's my approach: Use HashSet and check for each char whether it is present in HashSet if true count++ and remove current char from HashSet else add current char in HashSet Once you have iterated the string check if HashSet is not Empty? If yes return count*2+1 else return count * 2 Hnadles both Lowercase and Uppercase scenarios. This is single pass solution. Where TC is O(N) And SC is O(N) for HashSet. Your Solution uses 2 pass which is O(N+N) which is O(2N)= O(N) and space also O(N) or rather it's O(1) because it is using 26 chars. Thanks learned new approach of solving this question.