Sort by

recency

|

172 Discussions

|

  • + 0 comments

    Test case 1 answer is 'd' is actually correct one, because you insert 'd' after you insert 'dbajkhbhbjdh' (the previous string), and right at that time, the function should determine that the word array is a BAD SET, so it should print out BAD SET d. Below are my JS code which apply Trie structure.

    class TrieNode {
        constructor() {
            this.children = {};
            this.isEndOfString = false;
            this.count = 0;
        }
    }
    class Trie {
        constructor() {
            this.root = new TrieNode();
        }
        insert(word) {
            let node = this.root;
            for(let char of word) {
                if(!node.children[char]) {
                    node.children[char] = new TrieNode()
                }
                node = node.children[char];
                node.count += 1;
            }
            node.isEndOfString = true;
        }
        isGoodSet(word) {
            let node = this.root;
            for(let i=0; i< word.length; i++) {
                node = node.children[word[i]];
                if(node.isEndOfString === true && node.count > 1) return false;
            }
            return true;
        }
    }
    function noPrefix(words) {
        // Write your code here
        let trie = new Trie();
        for(let word of words) {
            trie.insert(word);
            if(!trie.isGoodSet(word)) {
                console.log('BAD SET');
                console.log(word);
                return;
            }
        }
        console.log('GOOD SET');
    }
    
  • + 1 comment

    Why submission TestCase 1 is print "d" ? but not "dcjaichjejgheiaie"?

    "dcjaichjejgheiaie" should test before "d".

    • + 1 comment

      Yeah I was going to comment the same thing. d can't be a prefixed by any thing other than itself, and there is only one line with d. Looks like the same problem with other test cases.

      • + 0 comments

        I'm encountering the same issue yet.

  • + 0 comments

    Java 8 solution, passed all the tests

        public static void noPrefix(List<String> words) {
            final Set<String> strings = new HashSet<>();
            final Set<String> subSets = new HashSet<>();
            final int size = words.size();
            for (int i=0; i< size; i++){
                final String checkedWord = words.get(i);
                //Check if the current word is subset of previous words
                if(subSets.contains(checkedWord)){
                    System.out.println("BAD SET");
                    System.out.print(checkedWord);
                    return;
                }
                
                for(int j=1; j<=checkedWord.length(); j++){
                    final String subSet = checkedWord.substring(0, j);
                    //Check if any previous word is a subset of the current word
                    if(strings.contains(subSet)){
                        System.out.println("BAD SET");
                        System.out.print(checkedWord);
                        return;
                    }
                    subSets.add(subSet);
                }
                strings.add(checkedWord);
            }
            
            System.out.println("GOOD SET");
    
        }
    
  • + 0 comments

    c++ solution

    bool isless_m( string str1,  string str2) {
        int N = min(str1.size(), str2.size());
        for (int i = 0; i < N; ++i) {
            if (str1[i] < str2[i]) {
                return true;
            }
            else if (str1[i] > str2[i]) {
                return false;
            }
        }
        return false;
    }
    
    void noPrefix(vector<string> words) {
           
        if (words.size() == 1) {
            cout << "GOOD SET\n";
            return;
        }    
    
        set<string,decltype(&isless_m)> mp(&isless_m);
    
        for (int i = 0; i < words.size(); ++i) {
            auto it=mp.insert(words[i]);
            if (!it.second) {
                 cout << "BAD SET\n";            
                cout << words[i]<<"\n";
                return;
            }
        }
        cout << "GOOD SET\n";
       
    }
    
  • + 0 comments

    Dumb question dumb answer. Passes all tests though.

    def noPrefix(words):
        # Write your code here
        content = {}
        subs = {}
        for word in words:
            content[word] = content.get(word, 0) + 1
            for i in range(len(word)-1):
                subs[word[:i+1]] = 1
            if word in subs:
                print('BAD SET')
                print(word)
                return
            for i in range(len(word)):
                if word[:i+1] in content:
                    if i == len(word)-1:
                        if content[word] > 1:
                            print('BAD SET')
                            print(word)
                            return
                    else:
                        print('BAD SET')
                        print(word)
                        return
        print('GOOD SET')