Skip to content

Comments

[STRATEGY] - Longest Common Prefix#2

Merged
thenameisvicky merged 5 commits intomasterfrom
STRATEGY/LongestCommonPrefix
Aug 30, 2025
Merged

[STRATEGY] - Longest Common Prefix#2
thenameisvicky merged 5 commits intomasterfrom
STRATEGY/LongestCommonPrefix

Conversation

@thenameisvicky
Copy link
Owner

@thenameisvicky thenameisvicky commented Aug 30, 2025

Problem Description

longest common prefix string amongst an array of strings., If there is no common prefix, return an empty string.

Type of Problem solved

  • Leetcode Algorithum
  • Engineering Drill

Approach

  • Need to find the longest common prefix in the Array.
  • First sort the array in ascending order keeping length of each word in array as unit.
  • Assign the first word in sorted array as a window by initializing one ( currentWindow ).
  • Utilizing this window i will compare the elements in the array starting with the currentWindow or not, while not then i shrink the window by slicing the last letter in the window.
  • When ever i encounter empty string in this process i return it.
  • After completing the comparison using for loop i return the currentWindow which is the longest common prefix.

Changes Made

  • Have added parameter type in type declarations file.
  • Have written new strategy under sliding window topic.

Learnings

  • Learnt about loops and window shrinking.
  • Improved knowledge in sliding window topic.

Alternate Approaches

  • Divide and conquer method
  • Binary search method for effiency
  • Two Pointer Techinique

Edge Cases Covered

  • [ "hello", "" , "hi", "hell" ]

Make sure to check

Before submitting, please ensure:

  • Code compiles without errors
  • Code follows the existing style guide
  • No console.log statements in strategy
  • Time and space complexity updated in PR
  • Edge cases are handled

Stay Locked In!

@thenameisvicky thenameisvicky merged commit 9468dbc into master Aug 30, 2025
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.

1 participant