0
  1. Verifying an Alien Dictionary

In an alien language, surprisingly they also use english lowercase letters, but possibly in a different order. The order of the alphabet is some permutation of lowercase letters.

Given a sequence of words written in the alien language, and the order of the alphabet, return true if and only if the given words are sorted lexicographicaly in this alien language.

code

class Solution
{
public:
    static vector<pair<char, int>> dict;

public:
    static bool compare(string tst1, string tst2)
    {
        if (tst1 == tst2)
        {
            return true;
        }
        int l1 = tst1.size();
        int l2 = tst2.size();
        int n = min(l1, l2);
        int flag = 0;
        for (int i = 0; i < n; i++)
        {
            if (tst1[i] == tst2[i])
                continue;
            if (tst1[i] != tst2[i])
            {
                flag = 1;
                int a = 0, b = 0;
                for (int j = 0; j < 26; j++)
                {
                    if (tst1[i] == dict[j].first)
                        a = dict[j].second;
                    if (tst2[i] == dict[j].first)
                        b = dict[j].second;
                    if (a != 0 && b != 0)
                        break;
                }
                if (a > b)
                {
                    return false;
                }
                else
                {
                    return true;
                }
            }
        }
        if (flag == 0)
        {
            return true;
        }
        return true;
    }

public:
    bool isAlienSorted(vector<string> &words, string order)
    {
        int n = words.size();
        vector<string> comp;
        for (int i = 0; i < n; i++)
        {
            comp.push_back(words[i]);
        }
        for (int i = 0; i < 26; i++)
        {
            dict.push_back(make_pair(order[i], i + 1));
        }
        sort(words.begin(), words.end(), compare);
        int flag = 1;
        for (int i = 0; i < n; i++)
        {
            if (words[i] != comp[i])
            {
                flag = 0;
                break;
            }
        }
        if (flag)
            return true;
        else
            return false;
    }
};

error->

ld.lld: error: undefined symbol: Solution::dict
>>> referenced by prog_joined.cpp
>>>               /tmp/prog_joined-0fc27f.o:(Solution::isAlienSorted(std::vector<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::allocator<std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > > >&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >))
>>> referenced by prog_joined.cpp
>>>               /tmp/prog_joined-0fc27f.o:(Solution::compare(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >))
>>> referenced by prog_joined.cpp
>>>               /tmp/prog_joined-0fc27f.o:(Solution::compare(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> >))
>>> referenced 1 more times
clang-11: error: linker command failed with exit code 1 (use -v to see invocation)

Why am I getting this error? I am not able to understand.

anatolyg
  • 26,506
  • 9
  • 60
  • 134
  • https://leetcode.com/problems/verifying-an-alien-dictionary/ – Mooing Duck Jun 07 '21 at 15:46
  • why did you make all members `static`? If this is for LeetCode, they will create only one object of your `Solution` anyhow and you also don't create other instances – 463035818_is_not_an_ai Jun 07 '21 at 16:48
  • Btw despite your comparator is wrong and you need to fix it, you do not need a copy of vector and use `std::sort()` as there is `std::is_sorted()` algo that does what you need with the same comparator (when you implement it properly) – Slava Jun 07 '21 at 16:50
  • @Slava thanks for the info. I got your point.. – Mridul Joshi Jun 09 '21 at 03:33

0 Answers0