I am working on the Longest Common Prefix (LCP) problem in leetcode where you have to build a program that compares different strings in an array and look for the LCP if any. I am getting the error: “cannot find symbol” in line 10 of my code.
So far, I am trying a brute force approach, where I use the first string in the array to compare each character within the array at the same index. Once a characters differs I know it is not part of the LCP. Here is my code so far:
class Solution {
public String longestCommonPrefix(String[] strs) {
char checkingChar;
String prefix;
Boolean isPrefix;
String firstWord;
for (int i = 0; i < strs[0].length(); i++) {
checkingChar = strs[0].get(i);
isPrefix = true;
for (int j = 0; i < strs.length; j++) {
if (strs[j].charAt(i) != checkingChar) {
isPrefix = false;
}
}
if (isPrefix == true) {
prefix += checkingChar;
}
}
return prefix;
}
}
Jesse Loya is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.