单词排序 -LintCode

204 篇文章 0 订阅

给一个新的字母表,如{c,b,a,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z},根据新的字母表排序字符串数组。

 注意事项
输入的单词长度不超过100。
输入的单词总数不超过10000。
可以认为,输入的新字母表即一个长度为26的字符串。
保证题目只会出现小写字母。

样例
给出 Alphabet = {c,b,a,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z}, String array = {cab,cba,abc}, 返回 {cba,cab,abc}。

解释:
根据新的字典序,排序输出{cba,cab,abc}

给出 Alphabet = {z,b,a,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,c}, String array = {bca,czb,za,zba,ade}, 返回 {zba,za,bca,ade,czb}。

解释:
根据新的字典序,排序输出{zba,za,bca,ade,czb}

思路:
现将所有元素由新字典转到旧字典,利用sort进行排序,之后再由旧字典转到新字典。

#ifndef C819_H
#define C819_H
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
using namespace std;
class Solution {
public:
    /**
    * @param alphabet: the new alphabet
    * @param words: the original string array
    * @return: the string array after sorting
    */
    vector<string> wordSort(string &alphabet, vector<string> &words) {
        // Write your code here
        if (alphabet.empty() || words.empty() || words[0].empty())
            return words;
        vector<int> aTob(26), bToa(26);
        //由原字典到新字典
        for (int i = 0; i < 26; ++i)
            aTob[i] = alphabet[i];
        //由新字典到原字典
        for (int i = 0; i < 26; ++i)
            bToa[alphabet[i] - 'a'] = 'a' + i;
        //先将元素都转到旧字典,进行排序,之后再转到新字典
        for (auto &c : words)
        {
            for (auto &t : c)
                t = bToa[t - 'a'];
        }
        sort(words.begin(), words.end());
        for (auto &c : words)
        {
            for (auto &t : c)
                t = aTob[t - 'a'];
        }
        return words;
    }
};
#endif
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值