LeetCode953

问题:验证外星语词典

  • 用户通过次数170
  • 用户尝试次数187
  • 通过次数173
  • 提交次数384
  • 题目难度Easy

某种外星语也使用英文小写字母,但可能顺序 order 不同。字母表的顺序(order)是一些小写字母的排列。

给定一组用外星语书写的单词 words,以及其字母表的顺序 order,只有当给定的单词在这种外星语中按字典序排列时,返回 true;否则,返回 false

示例 1:

输入:words = ["hello","leetcode"], order = "hlabcdefgijkmnopqrstuvwxyz"
输出:true
解释:在该语言的字母表中,'h' 位于 'l' 之前,所以单词序列是按字典序排列的。

示例 2:

输入:words = ["word","world","row"], order = "worldabcefghijkmnpqstuvxyz"
输出:false
解释:在该语言的字母表中,'d' 位于 'l' 之后,那么 words[0] > words[1],因此单词序列不是按字典序排列的。

示例 3:

输入:words = ["apple","app"], order = "abcdefghijklmnopqrstuvwxyz"
输出:false
解释:当前三个字符 "app" 匹配时,第二个字符串相对短一些,然后根据词典编纂规则 "apple" > "app",因为 'l' > '∅',其中 '∅' 是空白字符,定义为比任何其他

链接:https://leetcode-cn.com/contest/weekly-contest-114/problems/verifying-an-alien-dictionary/

分析:

1.新的order也是26个字母,只是顺序打乱了,那么如果重新映射,就可以得到正常顺序的字符串了

2.如果将外星词典字符串直接转换为正常的字符串,那么如果正常字符串是有序的,原来的外星词典也是有序的

3.如果一个字符串向量排序后和原来字符串向量一样,则说明这个字符串向量是有序的。

AC Code:

 1 class Solution {
 2 public:
 3     bool isAlienSorted(vector<string>& words, string order) {
 4         bool ret = false;
 5 
 6         vector<string> newwords;
 7         for (unsigned int i = 0; i < words.size(); i++)
 8         {
 9             newwords.emplace_back(ChangeToNormal(words[i], order));
10         }
11         vector<string> orderedwords(newwords);
12         sort(orderedwords.begin(), orderedwords.end());
13         ret = true;
14         for (int i = 0; i < orderedwords.size(); i++)  //TODO  判断是否有序的更好办法
15         {
16             if (orderedwords[i] != newwords[i])
17             {
18                 ret = false;
19                 break;
20             }
21         }
22 
23         return ret;
24     }
25     string ChangeToNormal(string str, string order)
26     {
27         string ret = "";
28         string normalorder = "abcdefghijklmnopqrstuvwxyz";
29         for (unsigned int i = 0; i < str.size(); i++)
30         {
31             int index = order.find(str[i]);
32             ret += normalorder[index];
33         }
34         return ret;
35     }
36 };

其他:

1.判断一个列表是否有序,一种方法是看和排序后的是否一致,其实还可以比较字符串大小,比如这道题里面,想要的是递增顺序,那么对于字符串向量A中,只要存在i使的A[i]>A[i+1],就可以说明是无须的,这样就不需要额外的空间存储排序了。

2.第一code:

 1 #define FILES freopen("input.txt", "r", stdin); freopen("output.txt", "w", stdout)
 2 #define FAST ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
 3 #define FIXED cout << fixed << setprecision(20)
 4 #define RANDOM srand(time(nullptr))
 5 #define int long long
 6 #define MOD 1000000007
 7 #define sz(a) (ll)a.size()
 8 #define pll pair<ll,ll>
 9 #define rep(i,a,b) for(int i=(int)a;i<(int)b;i++)
10 #define sep(i,a,b) for(int i=(int)a;i>=(int)b;i--)
11 #define mll map<int,int>
12 #define vl vector<int>
13 #define pb push_back
14 #define lb lower_bound
15 #define ub upper_bound
16 #define all(a) a.begin(),a.end()
17 #define F first
18 #define S second
19 #define endl "\n"
20 #define MAXN6 3000005
21 #define MAXN3 3005
22 #define MAXN5 300005
23 
24 #define watch(...) ZZ(#__VA_ARGS__, __VA_ARGS__)
25 template <typename Arg1> void ZZ(const char* name, Arg1&& arg1){std::cerr << name << " = " << arg1 << endl;}
26 template <typename Arg1, typename... Args>void ZZ(const char* names, Arg1&& arg1, Args&&... args)
27 {
28     const char* comma = strchr(names + 1, ',');
29     std::cerr.write(names, comma - names) << " = " << arg1;
30     ZZ(comma, args...);
31 }
32  
33 
34 class Solution {
35 public:
36     bool isAlienSorted(vector<string>& words, string order) {
37         int orderMap[26];
38         for (int i = 0; i < 26; i++) {
39             orderMap[order[i] - 'a'] = i;
40         }
41         vector<string> mappedWords;
42         for (string word: words) {
43             string x = word;
44             for (int i = 0; i < x.length(); i++)
45                 x[i] = orderMap[x[i] - 'a'] + 'a';
46             mappedWords.push_back(x);
47         }
48         for (int i = 1; i < mappedWords.size(); i++)
49             if (mappedWords[i] < mappedWords[i-1])
50                 return false;
51         return true;
52     }
53 };

和赛后思路差不多,不需要排序,直接找到一个不符合顺序的结束。不过一些C++的新特性,比如foreach in 等,之前尝试过,VS下好像并不那么好用,也可能是自己没用好,前两天看到C++ 20都要出了,自己貌似用的还是C++11之前的,甚至C++11都没用到太多特性,虽然未必就要用到最新的,但是至少得学习下,至少得知道,多了解一些总是好的。

猜你喜欢

转载自www.cnblogs.com/youdias/p/9942558.html