상위 150개 인터뷰중간

가장 긴 공통 부분 수열

'최장 공통 부분 수열' 문제에 대한 자세한 가이드 및 Python 구현입니다.

문제 설명

중간

두 개의 문자열 text1과 text2가 주어지면 가장 긴 공통 부분 수열의 길이를 반환합니다. 공통 부분 수열이 없으면 0을 반환합니다.

문자열의 하위 시퀀스는 나머지 문자의 상대적 순서를 변경하지 않고 일부 문자가 삭제된(없음일 수 있음) 원래 문자열에서 생성된 새 문자열입니다. (예를 들어 "ace"는 "abcde"의 하위 시퀀스입니다.)

longestCommonSubsequence(text1: str, text2: str) -> int 함수를 작성하세요.

제약
  • 1 <= len(text1), len(text2) <= 1000
  • text1 and text2 consist of only lowercase English characters

Example 1
Input
text1 = "abcde", text2 = "ace"
Output
3
Explanation

The longest common subsequence is "ace" and its length is 3.

Example 2
Input
text1 = "abc", text2 = "abc"
Output
3
Explanation

The longest common subsequence is "abc" and its length is 3.

Example 3
Input
text1 = "abc", text2 = "def"
Output
0
Explanation

There is no common subsequence, so the result is 0.

Need a Hint?
세트나 힙과 같은 2D DP 관련 데이터 구조를 사용해 보세요.
Edge Cases to Watch
  • 빈 입력 구조
  • 단일 요소 입력
  • 큰 수치 범위

해결할 준비가 되셨나요?

Open the problem in PyRun's browser-based Python editor. Your code runs fully offline — no server required.

편집기에서 열기
Found this breakdown helpful?

PyRun is built and maintained by an independent solo developer. If this helped your interview prep, consider buying a coffee!

Buy me a coffee

권장 Python 리소스

관련 대화형 튜토리얼, 치트 시트, 코드 비교를 통해 지식을 확장하세요.