713369. Find the longest string

Medium

Find the Longest String

Summary

Given a list of strings, find the longest string in the list. The problem requires us to write an efficient algorithm that can handle large inputs and scale well with increasing size.

Detailed Explanation

To solve this problem, we can use a Trie data structure to efficiently store and search for the strings. Here's a step-by-step breakdown:

  1. Create a Trie: Start by creating a Trie node with a boolean flag isEndOfWord set to false. Each node will have a character array of size 26 (for lowercase English alphabets) initialized with all characters as null.
  2. Insert Strings into Trie: Iterate through the list of strings and insert each string into the Trie. For each string, start from the root node and recursively traverse down the Trie, creating new nodes if necessary. Set isEndOfWord to true for the last node in each string's traversal path.
  3. Find Longest String: Once all strings are inserted, perform a depth-first search (DFS) or breadth-first search (BFS) on the Trie to find the longest string. Start from the root node and explore each node's children recursively until you reach a leaf node with isEndOfWord set to true.
  4. Return Longest String: The leaf node with the longest path length will correspond to the longest string in the list.

Here's an ASCII art diagram illustrating the Trie construction:

       +---------------+
       |  Root Node   |
       +---------------+
              |
              |
              v
       +---------------+
       |  Node 'a'    |
       |  (isEndOfWord = false)|
       +---------------+
              |
              |
              v
       +---------------+
       |  Node 'b'    |
       |  (isEndOfWord = false)|
       +---------------+
              |
              |
              v
       +---------------+
       |  Node 'c'    |
       |  (isEndOfWord = true)|
       +---------------+

The time complexity of this algorithm is O(N * M), where N is the number of strings and M is the maximum length of a string. The space complexity is O(N * M) as well, since we're storing all strings in the Trie.

Optimized Solutions

Java

View on GeeksforGeeks
class TrieNode {
    boolean isEndOfWord;
    TrieNode[] children = new TrieNode[26];

    public TrieNode() {
        isEndOfWord = false;
        for (int i = 0; i < 26; i++) {
            children[i] = null;
        }
    }
}

class Solution {
    public String longestString(String[] strings) {
        TrieNode root = new TrieNode();
        int maxLength = 0;
        String longestString = "";

        for (String s : strings) {
            TrieNode current = root;
            for (char c : s.toCharArray()) {
                int index = c - 'a';
                if (current.children[index] == null) {
                    current.children[index] = new TrieNode();
                }
                current = current.children[index];
            }
            current.isEndOfWord = true;

            if (s.length() > maxLength) {
                maxLength = s.length();
                longestString = s;
            }
        }

        return longestString;
    }
}

DevExCode

Master coding interviews with Leetcode, system design, TechBit, QuickLearn, DevTips, Tech Battles, and career services.

© 2026 DevExCode. All rights reserved.