javaarraysstringmassive

String[] cant be converted to String;


I'm trying to split Array strs to another but error

class Solution {
    public String longestCommonPrefix(String[] strs) {
        String[][] letters;
**        String cmon;
        for (int i = 0; i < strs.length; i++) {
            letters[i][i] = strs[i].split("");
        }**

        for (int j = 0; j < letters[0].length; j++) {
            for (int k = 0; k < letters.length; k++) {
                if (letters[k][j] == letters[k++][j]) {
                    cmon = letters[j][k];
                }  else {
                    cmon = "";
                }
            }
        }
        return cmon;
    }
}
Line 6: error: incompatible types: String[] cannot be converted to String
            letters[i][i] = strs[i].toString().split("");
                                                    ^

changed code a lot but nothing happend. already tried methods from stackoverflow but it doesnt help


Solution

  • "... I'm trying to split Array strs to another but error ..."

    The split method is for a String object specifically.

    Use a char array for this.

    char[][] letters = new char[strs.length][];
    for (int i = 0, n = strs.length; i < n; i++)
        letters[i] = strs[i].toCharArray();