shlogg · Early preview
Prashant Mishra @prashantrmishra

Counting Prefix And Suffix Pairs In O(n*m) Time Complexity

Count prefix and suffix pairs in a list of words. Brute force: O(n^2*m). Optimal solution using Trie: O(n*m).

Count prefix and suffix I 
Brute force approach:
TC: O(n^2*m), n is words.length and m is the length of the word[i] i>=0 && <n

class Solution {
    public int countPrefixSuffixPairs(String[] words) {
        int count =0;
        for(int i=0;i<words.length;i++){
            for(int j = 0;j<words.length;j++){
                if(i==j || words[i].length() > words[j].length() || i > j) continue;
                if(check(words[i],words[j])){
                    count++;
                }
            }
        }
        return count;
    }
    public boolean check(String a , String b){
        int...