- 
          
 - 
                Notifications
    
You must be signed in to change notification settings  - Fork 49.1k
 
Added longest_palindromic_subsequence algorithm in Dynamic Programming with zero conflicts #9448
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
          
     Closed
      
      
    
  
     Closed
                    Changes from all commits
      Commits
    
    
            Show all changes
          
          
            4 commits
          
        
        Select commit
          Hold shift + click to select a range
      
      248321c
              
                added longest_palindromic_subsequence algorithm
              
              
                N16H7H4WK-3R 2d4f504
              
                [pre-commit.ci] auto fixes from pre-commit.com hooks
              
              
                pre-commit-ci[bot] daf03c3
              
                added longest_palindromic_subsequence algorithm
              
              
                N16H7H4WK-3R 949b5df
              
                Resolved Conflicts
              
              
                N16H7H4WK-3R File filter
Filter by extension
Conversations
          Failed to load comments.   
        
        
          
      Loading
        
  Jump to
        
          Jump to file
        
      
      
          Failed to load files.   
        
        
          
      Loading
        
  Diff view
Diff view
There are no files selected for viewing
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
              | Original file line number | Diff line number | Diff line change | 
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| """ | ||
| LPS Problem Statement: Find the length of the longest palindromic subsequence | ||
| in a given string. A palindromic subsequence is a sequence that reads the same | ||
| forward and backward, but not necessarily continuous. | ||
| 
     | 
||
| Example: For the input "babad," the longest palindromic subsequence | ||
| can be "bab" or "aba." | ||
| """ | ||
| 
     | 
||
| 
     | 
||
| def longest_palindromic_subsequence(s: str) -> tuple[int, str]: | ||
| """ | ||
| Finds the length and the longest palindromic subsequence in a given string. | ||
| 
     | 
||
| Parameters: | ||
| s (str): The input string. | ||
| 
     | 
||
| Returns: | ||
| Tuple[int, str]: A tuple containing the length of the longest | ||
| palindromic subsequence and the subsequence itself. | ||
| 
     | 
||
| Raises: | ||
| ValueError: If the input string is empty or None. | ||
| 
     | 
||
| Example: | ||
| >>> longest_palindromic_subsequence("bbbab") | ||
| (4, 'bbbb') | ||
| >>> longest_palindromic_subsequence("babad") | ||
| (3, 'bab') | ||
| >>> longest_palindromic_subsequence("cbbd") | ||
| (2, 'bb') | ||
| """ | ||
| if s is None or len(s) == 0: | ||
| raise ValueError("Input string cannot be empty or None") | ||
| 
     | 
||
| n = len(s) | ||
| # Create a table to store results of subproblems | ||
| dp = [[0] * n for _ in range(n)] | ||
| 
     | 
||
| for i in range(n): | ||
| dp[i][i] = 1 | ||
| 
     | 
||
| for cl in range(2, n + 1): | ||
| for i in range(n - cl + 1): | ||
| j = i + cl - 1 | ||
| if s[i] == s[j] and cl == 2: | ||
| dp[i][j] = 2 | ||
| elif s[i] == s[j]: | ||
| dp[i][j] = dp[i + 1][j - 1] + 2 | ||
| else: | ||
| dp[i][j] = max(dp[i][j - 1], dp[i + 1][j]) | ||
| 
     | 
||
| # Reconstruct the Longest Palindromic Subsequence | ||
| i, j = 0, n - 1 | ||
| lps = [] | ||
| while i < n and j >= 0: | ||
| if s[i] == s[j]: | ||
| lps.append(s[i]) | ||
| i += 1 | ||
| j -= 1 | ||
| elif i < j and dp[i][j - 1] >= dp[i + 1][j]: | ||
| j -= 1 | ||
| else: | ||
| i += 1 | ||
| 
     | 
||
| return dp[0][n - 1], "".join(lps) | ||
| 
     | 
||
| 
     | 
||
| if __name__ == "__main__": | ||
| import doctest | ||
| 
     | 
||
| doctest.testmod() | ||
  Add this suggestion to a batch that can be applied as a single commit.
  This suggestion is invalid because no changes were made to the code.
  Suggestions cannot be applied while the pull request is closed.
  Suggestions cannot be applied while viewing a subset of changes.
  Only one suggestion per line can be applied in a batch.
  Add this suggestion to a batch that can be applied as a single commit.
  Applying suggestions on deleted lines is not supported.
  You must change the existing code in this line in order to create a valid suggestion.
  Outdated suggestions cannot be applied.
  This suggestion has been applied or marked resolved.
  Suggestions cannot be applied from pending reviews.
  Suggestions cannot be applied on multi-line comments.
  Suggestions cannot be applied while the pull request is queued to merge.
  Suggestion cannot be applied right now. Please check back later.
  
    
  
    
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please provide descriptive name for the parameter:
sThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
input_string