Find Longest Common Prefix in a String Array

As a Systems Engineer at Tata Consultancy Services, I deliver exceptional software products for mobile and web platforms, using agile methodologies and robust quality maintenance. I am experienced in performance testing, automation testing, API testing, and manual testing, with various tools and technologies such as Jmeter, Azure LoadTest, Selenium, Java, OOPS, Maven, TestNG, and Postman.
I have successfully developed and executed detailed test plans, test cases, and scripts for Android and web applications, ensuring high-quality standards and user satisfaction. I have also demonstrated my proficiency in manual REST API testing with Postman, as well as in end-to-end performance and automation testing using Jmeter and selenium with Java, TestNG and Maven. Additionally, I have utilized Azure DevOps for bug tracking and issue management.
Write a function to find the longest common prefix string amongst an array of strings.
If there is no common prefix, return an empty string "".
LeetCode Problem: Link | Click Here
class Solution {
public String longestCommonPrefix(String[] strs) {
// If the input array is empty or null, there's no common prefix.
if (strs == null || strs.length == 0) {
return "";
}
// Set the initial prefix to the first word in the array.
String prefix = strs[0];
// Loop through the array starting from the second word.
for (int i = 1; i < strs.length; i++) {
String current = strs[i];
int j = 0;
// Compare characters of the current word with the prefix.
while (j < prefix.length() && j < current.length() &&
prefix.charAt(j) == current.charAt(j)) {
j++;
}
// Update the prefix to the matched substring.
prefix = prefix.substring(0, j);
// If the prefix becomes empty, there's no common prefix.
if (prefix.isEmpty()) {
return "";
}
}
// Return the found common prefix.
return prefix;
}
}




