- 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.