Skip to content Navigation Menu Toggle navigation Sign in Appearance settings PlatformAI CODE CREATIONGitHub CopilotWrite better code with AIGitHub Copilot appDirect agents from issue to mergeMCP RegistryNewIntegrate external toolsDEVELOPER WORKFLOWSActionsAutomate any workflowCodespacesInstant dev environmentsIssuesPlan and track workCode ReviewManage code changesAPPLICATION SECURITYGitHub Advanced SecurityFind and fix vulnerabilitiesCode securitySecure your code as you buildSecret protectionStop leaks before they startEXPLOREWhy GitHubDocumentationBlogChangelogMarketplaceView all featuresSolutionsBY COMPANY SIZEEnterprisesSmall and medium teamsStartupsNonprofitsBY USE CASEApp ModernizationDevSecOpsDevOpsCI/CDView all use casesBY INDUSTRYHealthcareFinancial servicesManufacturingGovernmentView all industriesView all solutionsResourcesEXPLORE BY TOPICAISoftware DevelopmentDevOpsSecurityView all topicsEXPLORE BY TYPECustomer storiesEvents & webinarsEbooks & reportsBusiness insightsGitHub SkillsSUPPORT & SERVICESDocumentationCustomer supportCommunity forumTrust centerPartnersView all resourcesOpen SourceCOMMUNITYGitHub SponsorsFund open source developersPROGRAMSSecurity LabMaintainer CommunityAcceleratorGitHub StarsArchive ProgramREPOSITORIESTopicsTrendingCollectionsEnterpriseENTERPRISE SOLUTIONSEnterprise platformAI-powered developer platformAVAILABLE ADD-ONSGitHub Advanced SecurityEnterprise-grade security featuresCopilot for BusinessEnterprise-grade AI featuresPremium SupportEnterprise-grade 24/7 supportPricing Search or jump to... Search code, repositories, users, issues, pull requests... Search Clear Search syntax tips Provide feedback We read every piece of feedback, and take your input very seriously. Include my email address so I can be contacted Saved searches Use saved searches to filter your results more quickly Name Query To see all available qualifiers, see our documentation. Sign in Sign up Appearance settings Resetting focus You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session. Dismiss alert {{ message }} rajeevranjancom / Leetcode_Java Public Notifications You must be signed in to change notification settings Fork 0 Star 0 Code Issues 0 Pull requests 0 Actions Projects Security and quality 0 Insights Additional navigation options Code Issues Pull requests Actions Projects Security and quality Insights FilesExpand file tree masterBreadcrumbsLeetcode_Java/014_LongestCommonPrefix14.javaCopy pathBlameMore file actionsBlameMore file actions Latest commit HistoryHistoryHistory142 lines (128 loc) · 3.78 KB masterBreadcrumbsLeetcode_Java/014_LongestCommonPrefix14.javaCopy pathTopFile metadata and controlsCodeBlame142 lines (128 loc) · 3.78 KBRawCopy raw fileDownload raw fileOpen symbols panelEdit and raw actions123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142class Solution { public static String longestCommonPrefix(String[] strs) { if (strs.length == 0) return ""; Trie trie = new Trie(); String searchString = null; for (String word : strs) { if (searchString == null) { searchString = word; } else if (searchString.length() > word.length()) { searchString = word; } trie.insert(word); } return trie.longestCommonPrefix(searchString); }} class Trie { public static class TrieNode { Map<Character, TrieNode> map; boolean isWord; public TrieNode() { map = new HashMap<>(); isWord = false; } } private final TrieNode root; public Trie() { root = new TrieNode(); } public void insert(String word) { TrieNode current = root; for (int i = 0; i < word.length(); i++) { TrieNode newNode = current.map.get(word.charAt(i)); if (newNode == null) { newNode = new TrieNode(); current.map.put(word.charAt(i), newNode); } current = newNode; } current.isWord = true; } public String longestCommonPrefix(String word) { TrieNode current = root; if (current.map.size() > 1) return ""; String longestCommonPrefix = ""; for (int i = 0; i < word.length(); i++) { TrieNode newNode = current.map.get(word.charAt(i)); if (newNode == null || newNode.map.size() > 1) return longestCommonPrefix + word.charAt(i); else { longestCommonPrefix += word.charAt(i); current = newNode; } } return longestCommonPrefix; } } ********************************************************************* class Solution { public String longestCommonPrefix(String[] strs) { if(strs.length == 0){ return ""; } int count = lengthOfLongestCommonPrefix(strs); return strs[0].substring(0, count); } private int lengthOfLongestCommonPrefix(String[] strs){ int count = 0; for(int i = 0 ; i < strs[0].length(); i++){ boolean same = true; char ch = ' '; for(int j = 0; j < strs.length; j++){ if(i >= strs[j].length()){ return count; } if(j == 0){ ch = strs[j].charAt(i); continue; } if(ch != strs[j].charAt(i)){ same = false; } } if(same){ count ++; } else{ break; } } return count; }} ************************************************************************* class Solution { public String longestCommonPrefix(String[] strs) { if(strs.length==0) return ""; boolean flag = true; int count = 0; int smallestLen = strs[0].length(); for(int i=1;i<strs.length;i++) { if(strs[i].length()<smallestLen) smallestLen = strs[i].length(); } for(int i=0;i<smallestLen;i++) { char c = strs[0].charAt(i); for(int j=1;j<strs.length;j++) { if(strs[j].charAt(i)==c) flag=true; else { flag=false; break; } } if(flag==true) count++; else break; } if(count==0) return ""; return strs[0].substring(0,count); }} You can’t perform that action at this time.