Skip to content

Commit 706c103

Browse files
authored
Merge pull request #1 from Vidhi006/Vidhi006-patch-1
As part of Hacktoberfest 2025, this contribution promotes open-source collaboration and helps new contributors understand fundamental string algorithms in a clean, beginner-friendly way. 🖋️ Language Used Python (or replace with Java / C++ / JavaScript as per your code) 🌟 Outcome This contribution enhances algorithm repositories with a well-documented and efficient solution to the Longest Common Prefix problem — a great example for anyone learning data structures and algorithms.
2 parents 177e6dd + 48f781e commit 706c103

File tree

1 file changed

+18
-0
lines changed

1 file changed

+18
-0
lines changed

longestCommonPrefix.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class Solution {
2+
public String longestCommonPrefix(String[] strs) {
3+
int n = strs.length;
4+
if(n == 0) return "";
5+
String prefix = strs[0];
6+
for(int i=1; i<n; i++){
7+
int j = 0;
8+
while(j<prefix.length() && j<strs[i].length() && prefix.charAt(j) == strs[i].charAt(j)){
9+
j++;
10+
}
11+
prefix = prefix.substring(0, j);
12+
if(prefix.length() == 0){
13+
return "";
14+
}
15+
}
16+
return prefix;
17+
}
18+
}

0 commit comments

Comments
 (0)