首页 > 其他分享 >[LeetCode]953. 验证外星语词典

[LeetCode]953. 验证外星语词典

时间:2024-07-13 11:18:57浏览次数:3  
标签:false 953 LeetCode words size true order 外星

/*953. 验证外星语词典
已解答
简单

某种外星语也使用英文小写字母,但可能顺序 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' > '∅',其中 '∅' 是空白字符,定义为比任何其他字符都小(更多信息)。

提示:

1 <= words.length <= 100
1 <= words[i].length <= 20
order.length == 26
在 words[i] 和 order 中的所有字符都是英文小写字母。
*/

class Solution {
public:
    bool isAlienSorted(vector<string>& words, string order) {
        vector<int> index(26);
        //首先需要找到字母对应的order大小的索引,即获取当前word对应的顺序大小,然后计算出对应当前word和下一个word出现升序的情况,若出现一次逆序,则返回false,若出现true,则跳过当前,遍历所有word直至最后依然为true
        for (int i = 0; i < order.size(); i++) {
            index[order[i] - 'a'] = i;
        }
        for (int i = 1; i < words.size(); i++) {
            bool valid = false;
            for (int j = 0; j < words[i - 1].size() && j < words[i].size(); j++) {
                int prev = index[words[i - 1][j] - 'a'];
                int curr = index[words[i][j] - 'a'];
                if (prev < curr) {
                    valid = true;
                    break;
                } else if (prev > curr) {
                    return false;
                }
            }
            if (!valid) {
                /* 比较两个字符串的长度 */
                if (words[i - 1].size() > words[i].size()) {
                    return false;
                }
            }
        }
        return true;
    }
};

标签:false,953,LeetCode,words,size,true,order,外星
From: https://www.cnblogs.com/AmyMoJianJun/p/18299854

相关文章

  • 代码随想录——不同路径(Leetcode LCR98)
    题目链接动态规划classSolution{publicintuniquePaths(intm,intn){int[][]dp=newint[m][n];//从(0,0)到(i,0)路径只有一条for(inti=0;i<m;i++){dp[i][0]=1;}//从(0,0)到(0,j)路......
  • 代码随想录——不同路径Ⅱ(Leetcode 63)
    题目链接动态规划classSolution{publicintuniquePathsWithObstacles(int[][]obstacleGrid){intm=obstacleGrid.length;intn=obstacleGrid[0].length;int[][]dp=newint[m][n];//遇到障碍则从(0,0)到达......
  • 代码随想录——监控二叉树(Leetcode968)不会
    题目链接贪心/***Definitionforabinarytreenode.*publicclassTreeNode{*intval;*TreeNodeleft;*TreeNoderight;*TreeNode(){}*TreeNode(intval){this.val=val;}*TreeNode(intval,TreeNodeleft,Tr......
  • 【Leetcode--旋转矩阵】
    解题思路:先进行矩阵上下交换,接着对矩阵进行主对角线交换,就可以从上述左图变换为右图。classSolution{  publicvoidrotate(int[][]matrix){    //上下交换    for(inti=0;i<matrix.length/2;i++){      int[]temp=matrix[......
  • 代码随想录算法训练营第八天| leetcode 344、541、卡码网54
    反转字符串 leetcode344classSolution{public:voidreverseString(vector<char>&s){intindex1=0,index2=s.size()-1;chartmp;while(index1<index2){tmp=s[index1];s[index1]=s[index2];......
  • leetcode简单题21 N.104 二叉树的最大深度 rust描述
     //[3,9,20,null,null,15,7]3//[1,null,2]2usestd::rc::Rc;usestd::cell::RefCell;//Definitionforabinarytreenode.#[derive(Debug,PartialEq,Eq)]pubstructTreeNode{pubval:i32,publeft:Option<Rc<RefCell<TreeNode>>......
  • LeetCode - #93 复原 IP 地址
    文章目录前言1.描述2.示例3.答案关于我们前言本题由于没有合适答案为以往遗留问题,最近有时间将以往遗留问题一一完善。我们社区陆续会将顾毅(Netflix增长黑客,《iOS面试之道》作者,ACE职业健身教练。)的Swift算法题题解整理为文字版以方便大家学习与阅读。......
  • LeetCode 2974. 最小数字游戏(排序)
    题目:2974.最小数字游戏思路:排序后,两个两个取出来进行操作即可classSolution{public:vector<int>numberGame(vector<int>&nums){sort(nums.begin(),nums.end());vector<int>v;for(inti=1;i<nums.size();i+=2){v.pu......
  • 代码随想录算法训练营Day22 | Leetcode 77. 组合 | 216.组合总和III | 17.电话号码的
    今日任务77.组合题目链接:https://leetcode.cn/problems/combinations/description/题目描述:CodeclassSolution{vector<vector<int>>ans;vector<int>path;public:vector<vector<int>>combine(intn,intk){//intst......
  • LeetCode 2950. 可整除子串的数量
    2950.可整除子串的数量每个英文字母都被映射到一个数字,如下所示。如果字符串的字符的映射值的总和可以被字符串的长度整除,则该字符串是 可整除 的。给定一个字符串 s,请返回 s 的 可整除子串 的数量。子串 是字符串内的一个连续的非空字符序列。示例1:Substrin......