LeetCode Problem 1143: "Longest Common Subsequence": Given two strings text1 and text2, return the length of their longest common subsequence. If there is no common subsequence, return 0. A subsequence of a string is a new string generated from the original string with some characters (can be none) deleted without changing the relative order of the remaining characters. For example, "ace" is a subsequence of "abcde". A common subsequence of two strings is a subsequence that is common to both strings. The below implementation in Python resolves this problem in O(m*n) time and space complexity using the dynamic programming approach. A dp array is created whose cells store the value of longest common subsequence upto a specific length of text1 and text2. At the last cell we get the value of "longest common subsequence" for the given two strings. #Python #LeetCode #DynamicProgramming #Algorithms #DataStructures #CompetitiveProgramming #CodingChallenge #ProblemSolving
LeetCode 1143: Longest Common Subsequence Length
More Relevant Posts
-
LeetCode Problem 83: Given the head of a sorted linked list, delete all duplicates such that each element appears only once. Return the linked list sorted as well. The below implementation in Python successfully resolves this in time complexity of O(n) where n is length of the list with constant space complexity. The approach is simple, handle the base case first like list having 0 or 1 number of nodes and then write the logic of handling lists of length greater than one. Maintain two pointers, one to keep track of present node and other to keep track of previous node. Check if the value of both nodes match or not, if match update the pointing of previous node, point its next to the present.next. #LeetCode #LinkedList #Python #CompetitiveProgramming #Algorithms #DataStructures #ProblemSolving
To view or add a comment, sign in
-
-
Finding Maximum Sum of k Consecutive Elements in Python Check out this simple sliding window trick to get the maximum sum of k consecutive numbers in an array. I used my own code to solve it: Python Copy code nums = [7,1,5,1,3,2] k = 3 n = len(nums) window = sum(nums[:k]) sums = window for i in range(k,n): window = window - nums[i-k] + nums[i] if sums < window: sums = window print(sums) How it works: Start with the sum of the first k numbers Slide the window by removing the first number and adding the next Keep track of the maximum sum Fast, simple, and efficient! #Python #DSA #Coding #Algorithms #LearnByDoing #DeveloperLife
To view or add a comment, sign in
-
🚀 Day 50 of My Python Journey Today I solved Complement of Base 10 Integer on LeetCode. 🔍 Problem Overview: The task is to find the bitwise complement of a given base-10 integer. The complement is obtained by flipping all bits in its binary representation — changing every 0 to 1 and every 1 to 0. 🧠 Approach: 1️⃣ Convert the integer into its binary representation. 2️⃣ Traverse the binary string and flip each bit (1 → 0, 0 → 1). 3️⃣ Convert the resulting binary string back to a decimal integer. ⚡ Key Learnings: • Practiced binary representation and bit manipulation • Improved understanding of number systems (binary ↔ decimal) • Strengthened string manipulation and logical thinking in Python 📊 Complexity: • Time Complexity: O(n) • Space Complexity: O(n) Under the Guidance of : Rudra Sravan kumar and Manoj Kumar Reddy Parlapalli #Day50 #Python #LeetCode #DataStructures #Algorithms #CodingJourney #ProblemSolving #100DaysOfCode 🚀
To view or add a comment, sign in
-
-
LeetCode #100 – Same Tree | Python Implementation I implemented a recursive DFS approach that simultaneously traverses both trees, comparing nodes at each step. Core Insight: Tree equality decomposes into three checks: both null (base case), structural equality, and value equality. Recursion naturally handles the "and both subtrees match" requirement. Time: O(n) | Space: O(h) where h = tree height (recursion stack) #LeetCode #DataStructures #Python #BinaryTree #Recursion #DFS #CodingInterview #SoftwareEngineering
To view or add a comment, sign in
-
-
#Projects 🚀 Excited to share my latest project: Thread Shed 🧵✨ https://lnkd.in/gssBsFwy Working with complex string data in Python has always fascinated me. This project was born out of the challenge of handling messy, layered text structures and turning them into something clean, efficient, and insightful. 🔹 Why it matters: Speeds up data parsing and transformation Handles intricate string manipulations with clarity Demonstrates how concurrency can simplify real-world text-heavy workflows 👉 I’d love to hear how others approach complex string challenges in Python. Do you lean on regex, threading, or something else entirely? #Python #Threading #StringProcessing #DataEngineering #Innovation Would you like me to make this post more technical (with code snippets and performance metrics) or more story-driven (focusing on your personal journey and motivation)?
To view or add a comment, sign in
-
-
🧠 Python Concept: List Comprehension Write powerful loops in one clean line. ❌ Traditional Way squares = [] for i in range(5): squares.append(i * i) print(squares) Output [0, 1, 4, 9, 16] ✅ Pythonic Way squares = [i * i for i in range(5)] print(squares) Same result, less code. ⚡ With Condition even_squares = [i * i for i in range(10) if i % 2 == 0] print(even_squares) Output [0, 4, 16, 36, 64] 🧒 Simple Explanation Imagine telling a robot: 👉 “Give me squares of numbers from 0–4.” 👉 Instead of repeating instructions, you give one rule. 👉 That rule = list comprehension. 💡 Why This Matters ✔ Shorter code ✔ Faster execution ✔ More readable loops ✔ Very Pythonic 🐍 Python often replaces multiple lines with a single elegant expression 🐍 List comprehensions are one of the most powerful examples of that philosophy. #Python #PythonTips #PythonTricks #AdvancedPython #List #ListComprehension #Tech #CleanCode #LearnPython #Programming #DeveloperLife #DailyCoding #100DaysOfCode
To view or add a comment, sign in
-
-
LeetCode Problem 516: "Longest Palindromic Subsequence": Given a string s, find the longest palindromic subsequence's length in s. A subsequence is a sequence that can be derived from another sequence by deleting some or no elements without changing the order of the remaining elements. The below implementation in Python first reverses the string and then creates a dp array, in which each cell stores the length of longest common subsequence up to the corresponding length of original string and its reversed string. The value at last cell thus contains the length of longest common subsequence for the two strings, since the 2nd string is just reversed of the first one, the common subsequence would be "palindrome". #LeetCode #Python #CompetitiveProgramming #Algorithms #DynamicProgramming #Strings #DataStructures #InterviewPrep #DailyCoding #ProblemSolving
To view or add a comment, sign in
-
-
Today I implemented Serialize and Deserialize Binary Tree in Python. The goal of the problem is to convert a binary tree into a string so it can be stored or transferred, and later reconstruct the exact same tree from that string. I used a BFS (level order traversal) approach for this. Idea: • Traverse the tree level by level using a queue. • Store node values in a list. • For missing children, store a special marker (#) so the structure of the tree is preserved. • Join the list into a single string. For deserialization, the process is reversed: • Split the string back into values. • Rebuild the tree using a queue. • Attach left and right children in the same order they were stored. What I liked about this problem is that it shows how important it is to preserve structure, not just values. Without storing null nodes, reconstructing the same tree would be impossible. Time Complexity: O(N) Space Complexity: O(N) Problems like this are a good reminder that tree problems often combine traversal, data representation, and careful reconstruction logic. #DSA #BinaryTree #Python #Algorithms #CodingInterview
To view or add a comment, sign in
-
-
Python Clarity Series – Episode 12 Topic: Default Arguments Trap ⚠️ Advanced Trap: Mutable Default Arguments This looks harmless: def add_item(item, lst=[]): lst.append(item) return lst print(add_item(1)) print(add_item(2)) **Output: [1] [1, 2] ❗ Why? Because default list is created ONLY ONCE. Better way: def add_item(item, lst=None): if lst is None: lst = [] lst.append(item) return lst 💡 Deep Concept: Default arguments are evaluated once at function definition. This is NOT exam-level trick. This is real Python behavior. Strong concept = strong programmer. Did this surprise you? #PythonAdvanced #CodingMindset #DeveloperThinking
To view or add a comment, sign in
-
-
LeetCode #76 – Minimum Window Substring | Python Implementation I implemented a sliding window approach with two HashMaps to find the smallest substring containing all characters from t. The countT map stores the required character frequencies, while window tracks the current window's frequencies. Two counters have and need track how many unique characters have met their required counts. The right pointer expands the window until all requirements are satisfied, then the left pointer contracts to minimize the window size while maintaining validity. This pattern is critical in text search engines, log parsers, and bioinformatics for pattern matching in genomic sequences. Key Takeaway: The have vs need tracking elegantly reduces the problem to counting satisfied unique characters rather than checking all frequencies repeatedly. The inner while loop aggressively shrinks the window once valid, ensuring we capture the smallest possible substring before expanding again. Time: O(n + m) where n = len(s), m = len(t) | Space: O(m) #LeetCode #DataStructures #Python #SlidingWindow #HashMap #CodingInterview #ProblemSolving #SoftwareEngineering
To view or add a comment, sign in
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