Zum Hauptinhalt springen
•
IT NachrichtenSean Parker is rebuilding Stability AI around music(02.10.2026 um 23:09 Uhr)
•
IT NachrichtenMeta open sources code to let you make Muse AI gadgets(02.10.2026 um 23:08 Uhr)
•••
IT NachrichtenTelekom und 1&1: Preise für Internettarife steigen(02.10.2026 um 20:45 Uhr)
••••••
IT NachrichtenSean Parker is rebuilding Stability AI around music(02.10.2026 um 23:09 Uhr)
•
IT NachrichtenMeta open sources code to let you make Muse AI gadgets(02.10.2026 um 23:08 Uhr)
•••
IT NachrichtenTelekom und 1&1: Preise für Internettarife steigen(02.10.2026 um 20:45 Uhr)
•••••
Intelligence View
⚡ tsecurity.de Intelligence

LeetCode 1143: Longest Common Subsequence — Step-by-Step Visual Trace

Medium — Dynamic Programming | String | Two Pointers The Problem Find the length of the longest common subsequence (LCS) between two strings, where a s…

Beitrag
0
Seite
0
↗ Quelle (dev.to)
Social ReaktionenReagiere als Erste:r — dein Feedback zählt!

Medium — Dynamic Programming | String | Two Pointers






The Problem



Find the length of the longest common subsequence (LCS) between two strings, where a subsequence maintains the relative order of characters but doesn't need to be contiguous.






Approach



Use dynamic programming with a 2D table where dp[i][j] represents the LCS length of the first i characters of text1 and first j characters of text2. If characters match, add 1 to the diagonal value; otherwise, take the maximum of the left or top cell.



Time: O(m*n) · Space: O(m*n)






Code






class Solution:
def longestCommonSubsequence(self, text1: str, text2: str) -> int:
m, n = len(text1), len(text2)

# Create a 2D dp array of size (m+1) x (n+1) and initialize it with zeros.
dp = [[0] * (n + 1) for _ in range(m + 1)]

# Fill in the dp array using dynamic programming.
for i in range(1, m + 1):
for j in range(1, n + 1):
if text1[i - 1] == text2[j - 1]:
dp[i][j] = dp[i - 1][j - 1] + 1
else:
dp[i][j] = max(dp[i - 1][j], dp[i][j - 1])

# The value in dp[m][n] represents the length of the LCS.
return dp[m][n]







Watch It Run










TraceLit — See exactly where your code breaks



Paste your LeetCode solution and see every pointer, variable, and data structure update step by step.



favicon
tracelit.dev








Open interactive visualization



Try it yourself: Open TraceLit and step through every line.







Built with TraceLit — the visual algorithm tracer for LeetCode practice.

🔍 CTI & Forensik

Cyber Threat Intelligence & Forensik

ATT&CK-Navigator · IoC-Radar · Exploit-Belege
CTI Threat Relationship Graph
Akteure · Techniken · Beziehungen
2 Knoten · 1 Relationen
CVE / Incident Threat Actor Software MITRE ATT&CK CWE Weakness IoC
Ähnliche Beiträge
🔍 Verwandte News

Auch interessante Nachrichten LeetCode 1143: Longest Common Subsequence — Step-by-Step Visual Trace

Thematisch verwandte Begriffe: LeetCode, 1143, Longest, Common · 6 Treffer

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

Laden...

Beiträge werden geladen ...

Laden...

Videos werden geladen ...

💬 Kommentare werden geladen…
Zum Aktualisieren ziehen
tsecurity.de Icon
Offline-Lesen, Eilmeldungen & 0ms Ladezeit

Installiere tsecurity.de direkt auf deinen Home-Bildschirm für das ultimative Vollbild-Magazinerlebnis ohne Browser-Leisten.

Nächster Beitrag