C#C
C#2y ago
morry329#

Fighting with the longest common prefix puzzle

Link to the puzzle description https://leetcode.com/problems/longest-common-prefix/submissions

as always I had a small hiccups . Here's my WIP code
public class Solution {
    public string LongestCommonPrefix(string[] strs) {
        string res = "";
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < strs.Length; i++)
        {
            string commonChars = string.Concat(strs
                .Select(strs=>strs.AsEnumerable())
                .Aggregate((s,a)=>s.Intersect(a))
                .OrderBy(c=>c)
                );
            
            res = commonChars;
        }
        

        return res;
    }
}
`
And this code is stuck with the following test case as per screenshot. It's fine with all the other test cases.

I assume Linq has some functions to compare the length of the common chars, but I am not versed with this technology. I assume Linq has a function to skip non-common chars sitting between the common chars (like in the screenshot)
Can anyone point me in the right direction?
Bildschirmfoto_2024-05-12_um_22.12.31.png
Was this page helpful?