Skip to content

Add Longest Common Prefix solution in Java (#408)#409

Open
maitriupadhyay03-cell wants to merge 2 commits into
saloni-jaiswal-dev:mainfrom
maitriupadhyay03-cell:main
Open

Add Longest Common Prefix solution in Java (#408)#409
maitriupadhyay03-cell wants to merge 2 commits into
saloni-jaiswal-dev:mainfrom
maitriupadhyay03-cell:main

Conversation

@maitriupadhyay03-cell

Copy link
Copy Markdown

Closes #408

This PR adds a Java solution for the Longest Common Prefix problem.

Approach

  • Horizontal scanning: start with the first string as prefix
    • Iteratively trim the prefix until it matches the start of each subsequent string
    • Return empty string if no common prefix exists

Complexity

  • Time: O(S) where S = total characters across all strings
    • Space: O(1)

Test Cases

  • ["flower", "flow", "flight"] → "fl"
    • ["dog", "racecar", "car"] → ""
    • ["abc", "abc", "abc"] → "abc"
    • Single string → returns itself
    • Empty string in array → ""

Implemented a function to find the longest common prefix among an array of strings using horizontal scanning. Added test cases to validate the functionality.
Implement Kadane's Algorithm to find the maximum sum of a contiguous subarray with test cases.
@maitriupadhyay03-cell

Copy link
Copy Markdown
Author

Also added Kadane's Algorithm solution in Java to address issue #358.

The KadanesAlgorithm.java file implements the maximum subarray sum problem:

  • Time Complexity: O(n)
    • Space Complexity: O(1)
    • Handles edge cases: all-negative arrays, single element, all-positive
    • Includes 4 test cases covering different scenarios

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

FIND the longest common prefix string amongst an array of strings.

1 participant