[{"data":1,"prerenderedAt":348},["ShallowReactive",2],{"\u002Fblog\u002Fcode-log-day-2":3},{"id":4,"title":5,"author":6,"body":7,"date":334,"description":335,"draft":336,"extension":337,"image":338,"language":339,"meta":340,"navigation":69,"path":341,"seo":342,"stem":343,"tags":344,"__hash__":347},"blog\u002Fblog\u002FCode Log - Day 2.md","Code Log - Day 2","Caio Prado",{"type":8,"value":9,"toc":329},"minimark",[10,15,19,24,27,38,41,198,201,253,263,325],[11,12,14],"h2",{"id":13},"log","Log",[16,17,18],"p",{},"Today i decided to solve a LeetCode exercise.",[20,21,23],"h3",{"id":22},"leetcode","LeetCode",[16,25,26],{},"The LeetCode exercise i chose today was: Longest Common Prefix.\nIt requires a function that receive as input an array of strings to return the longest common prefix between them:",[28,29,30],"blockquote",{},[16,31,32,33,37],{},"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 ",[34,35,36],"code",{},"\"\"",".",[16,39,40],{},"My first approach to achieve this was to start a empty prefix string; sort the array by using the length of the strings; make a nested loop with the more external one being used to get the chars of the first string (the smallest one) of the array and the internal one to verify if that char exists in every other string in the array; add the char to the prefix string. However, it was way to slow (3 ms to finish).",[42,43,48],"pre",{"className":44,"code":45,"language":46,"meta":47,"style":47},"language-java shiki shiki-themes material-theme-lighter material-theme material-theme-palenight","class Solution {\n    public String longestCommonPrefix(String[] strs) {\n\n        String prefix = \"\";\n\n        if(strs.length == 1 ) return strs[0];\n\n        boolean isIn = true;\n\n        Arrays.sort(strs, Comparator.comparingInt(String::length));\n        for(int i=0; i \u003C strs[0].length() && isIn == true; i++){\n            for(int j=1; j \u003C strs.length; j++){\n\n                if(strs[0].charAt(i) != strs[j].charAt(i)){\n                    isIn = false;\n                    break;\n                }\n            }\n            if(isIn == true){\n                prefix = prefix.concat(String.valueOf(strs[0].charAt(i)));\n            }\n        }\n        return prefix;\n    }\n}\n","java","",[34,49,50,58,64,71,77,82,88,93,99,104,110,116,122,127,133,139,145,151,157,163,169,174,180,186,192],{"__ignoreMap":47},[51,52,55],"span",{"class":53,"line":54},"line",1,[51,56,57],{},"class Solution {\n",[51,59,61],{"class":53,"line":60},2,[51,62,63],{},"    public String longestCommonPrefix(String[] strs) {\n",[51,65,67],{"class":53,"line":66},3,[51,68,70],{"emptyLinePlaceholder":69},true,"\n",[51,72,74],{"class":53,"line":73},4,[51,75,76],{},"        String prefix = \"\";\n",[51,78,80],{"class":53,"line":79},5,[51,81,70],{"emptyLinePlaceholder":69},[51,83,85],{"class":53,"line":84},6,[51,86,87],{},"        if(strs.length == 1 ) return strs[0];\n",[51,89,91],{"class":53,"line":90},7,[51,92,70],{"emptyLinePlaceholder":69},[51,94,96],{"class":53,"line":95},8,[51,97,98],{},"        boolean isIn = true;\n",[51,100,102],{"class":53,"line":101},9,[51,103,70],{"emptyLinePlaceholder":69},[51,105,107],{"class":53,"line":106},10,[51,108,109],{},"        Arrays.sort(strs, Comparator.comparingInt(String::length));\n",[51,111,113],{"class":53,"line":112},11,[51,114,115],{},"        for(int i=0; i \u003C strs[0].length() && isIn == true; i++){\n",[51,117,119],{"class":53,"line":118},12,[51,120,121],{},"            for(int j=1; j \u003C strs.length; j++){\n",[51,123,125],{"class":53,"line":124},13,[51,126,70],{"emptyLinePlaceholder":69},[51,128,130],{"class":53,"line":129},14,[51,131,132],{},"                if(strs[0].charAt(i) != strs[j].charAt(i)){\n",[51,134,136],{"class":53,"line":135},15,[51,137,138],{},"                    isIn = false;\n",[51,140,142],{"class":53,"line":141},16,[51,143,144],{},"                    break;\n",[51,146,148],{"class":53,"line":147},17,[51,149,150],{},"                }\n",[51,152,154],{"class":53,"line":153},18,[51,155,156],{},"            }\n",[51,158,160],{"class":53,"line":159},19,[51,161,162],{},"            if(isIn == true){\n",[51,164,166],{"class":53,"line":165},20,[51,167,168],{},"                prefix = prefix.concat(String.valueOf(strs[0].charAt(i)));\n",[51,170,172],{"class":53,"line":171},21,[51,173,156],{},[51,175,177],{"class":53,"line":176},22,[51,178,179],{},"        }\n",[51,181,183],{"class":53,"line":182},23,[51,184,185],{},"        return prefix;\n",[51,187,189],{"class":53,"line":188},24,[51,190,191],{},"    }\n",[51,193,195],{"class":53,"line":194},25,[51,196,197],{},"}\n",[16,199,200],{},"So i removed the array sorting and added a check to the if statement inside the more internal nested loop, but it was still taking 2 ms to finish.",[42,202,204],{"className":44,"code":203,"language":46,"meta":47,"style":47},"\u002F\u002F ...\n\u002F\u002F internal loop\nfor(int j=1; j \u003C strs.length; j++){\n    \u002F\u002F changes here:\n    if(i >= strs[j].length() || (i \u003C strs[j].length() && strs[0].charAt(i) != strs[j].charAt(i))){\n        isIn = false;\n        break;\n    }\n}\n\u002F\u002F ...\n",[34,205,206,211,216,221,226,231,236,241,245,249],{"__ignoreMap":47},[51,207,208],{"class":53,"line":54},[51,209,210],{},"\u002F\u002F ...\n",[51,212,213],{"class":53,"line":60},[51,214,215],{},"\u002F\u002F internal loop\n",[51,217,218],{"class":53,"line":66},[51,219,220],{},"for(int j=1; j \u003C strs.length; j++){\n",[51,222,223],{"class":53,"line":73},[51,224,225],{},"    \u002F\u002F changes here:\n",[51,227,228],{"class":53,"line":79},[51,229,230],{},"    if(i >= strs[j].length() || (i \u003C strs[j].length() && strs[0].charAt(i) != strs[j].charAt(i))){\n",[51,232,233],{"class":53,"line":84},[51,234,235],{},"        isIn = false;\n",[51,237,238],{"class":53,"line":90},[51,239,240],{},"        break;\n",[51,242,243],{"class":53,"line":95},[51,244,191],{},[51,246,247],{"class":53,"line":101},[51,248,197],{},[51,250,251],{"class":53,"line":106},[51,252,210],{},[16,254,255,256,37],{},"Then i found an alternative, with 0 ms of runtime and fewer lines of code that starts with the first string as the prefix and loops through the rest of the array, verifying if the prefix exists in each string, and, if it doesn't, it pops the last character of the prefix and compares again with the same string until the prefix is in the string or it becomes an empty string. This solution can be found ",[257,258,262],"a",{"href":259,"rel":260},"https:\u002F\u002Fleetcode.com\u002Fproblems\u002Flongest-common-prefix\u002Fsolutions\u002F4182958\u002Fjava-c-python-beats-100-beginner-s-friendly\u002F",[261],"nofollow","here",[42,264,266],{"className":44,"code":265,"language":46,"meta":47,"style":47},"class Solution {\n    public String longestCommonPrefix(String[] strs) {\n\n        if (strs == null || strs.length == 0) return \"\";\n        String prefix = strs[0];\n\n        for(int i = 1; i \u003C strs.length; i++){\n            while (strs[i].indexOf(prefix) != 0)\n                prefix = prefix.substring(0, prefix.length() - 1);\n        }\n        return prefix;\n    }\n}\n",[34,267,268,272,276,280,285,290,294,299,304,309,313,317,321],{"__ignoreMap":47},[51,269,270],{"class":53,"line":54},[51,271,57],{},[51,273,274],{"class":53,"line":60},[51,275,63],{},[51,277,278],{"class":53,"line":66},[51,279,70],{"emptyLinePlaceholder":69},[51,281,282],{"class":53,"line":73},[51,283,284],{},"        if (strs == null || strs.length == 0) return \"\";\n",[51,286,287],{"class":53,"line":79},[51,288,289],{},"        String prefix = strs[0];\n",[51,291,292],{"class":53,"line":84},[51,293,70],{"emptyLinePlaceholder":69},[51,295,296],{"class":53,"line":90},[51,297,298],{},"        for(int i = 1; i \u003C strs.length; i++){\n",[51,300,301],{"class":53,"line":95},[51,302,303],{},"            while (strs[i].indexOf(prefix) != 0)\n",[51,305,306],{"class":53,"line":101},[51,307,308],{},"                prefix = prefix.substring(0, prefix.length() - 1);\n",[51,310,311],{"class":53,"line":106},[51,312,179],{},[51,314,315],{"class":53,"line":112},[51,316,185],{},[51,318,319],{"class":53,"line":118},[51,320,191],{},[51,322,323],{"class":53,"line":124},[51,324,197],{},[326,327,328],"style",{},"html .light .shiki span {color: var(--shiki-light);background: var(--shiki-light-bg);font-style: var(--shiki-light-font-style);font-weight: var(--shiki-light-font-weight);text-decoration: var(--shiki-light-text-decoration);}html.light .shiki span {color: var(--shiki-light);background: var(--shiki-light-bg);font-style: var(--shiki-light-font-style);font-weight: var(--shiki-light-font-weight);text-decoration: var(--shiki-light-text-decoration);}html .default .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .shiki span {color: var(--shiki-default);background: var(--shiki-default-bg);font-style: var(--shiki-default-font-style);font-weight: var(--shiki-default-font-weight);text-decoration: var(--shiki-default-text-decoration);}html .dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}html.dark .shiki span {color: var(--shiki-dark);background: var(--shiki-dark-bg);font-style: var(--shiki-dark-font-style);font-weight: var(--shiki-dark-font-weight);text-decoration: var(--shiki-dark-text-decoration);}",{"title":47,"searchDepth":60,"depth":60,"links":330},[331],{"id":13,"depth":60,"text":14,"children":332},[333],{"id":22,"depth":66,"text":23},"2025-01-08","Solving LeetCode's Longest Common Prefix in Java, and why sorting by string length made the solution simpler.",false,"md",null,"en",{},"\u002Fblog\u002Fcode-log-day-2",{"title":5,"description":335},"blog\u002FCode Log - Day 2",[345,346,22],"challenge","daily-coding","Cmjt5poJoQ7WhjgCJVlyy3h2vmeUMkHGlcOYmPn7ELQ",1785357636467]