力扣第十天

本文探讨了两个核心问题:一是如何判断字符串是否遵循特定模式,二是如何将字符串划分成尽可能多的部分,使得每个字母最多出现在一个部分中。针对第一个问题,提供了一种使用哈希映射的方法,并给出了一种更简洁高效的解决方案;对于第二个问题,则介绍了一个通过记录字符出现位置并进行区间合并的算法。

session Ⅱ Data Structure

problem Ⅰ

290. Word Pattern
Given a pattern and a string s, find if s follows the same pattern.

Here follow means a full match, such that there is a bijection between a letter in pattern and a non-empty word in s.

Example 1:

Input: pattern = “abba”, s = “dog cat cat dog”
Output: true

Example 2:

Input: pattern = “abba”, s = “dog cat cat fish”
Output: false

Example 3:

Input: pattern = “aaaa”, s = “dog cat cat dog”
Output: false

my solution

class Solution {
public:
    bool wordPattern(string pattern, string s) {
        stringstream ss(s);
        vector<string> v;
        string word;
        while(ss >> word)
            v.push_back(word);
        if(pattern.size() != v.size())return false;
        map<string, char> hashmap;
        unordered_set<char> hashset;
        for(int i=0; i<pattern.size(); i++){
            char ch = pattern[i];
            if(hashmap.find(v[i]) != hashmap.end())
                if(hashmap[v[i]] != ch)return false;
            else{
                if(hashset.find(ch) != hashset.end())
                    return false;
                hashset.insert(ch);
                hashmap.insert(pair<string, char>(v[i], ch));
            }
        }
        return true;   
    }
};

faster solution

class Solution {
public:
    bool wordPattern(string pattern, string s) {
        unordered_map<char, int> p2i;
        unordered_map<string, int> w2i;
        istringstream in(s); string word;
        int i = 0, n = pattern.size();
        for(word; in>>word; i++){
            if(i==n || p2i[pattern[i]] != w2i[word]) return false; 
            p2i[pattern[i]] = w2i[word] = i+1; 
        }
        return i==n;
    }
};

problem Ⅱ

763. Partition Labels
You are given a string s. We want to partition the string into as many parts as possible so that each letter appears in at most one part.

Note that the partition is done so that after concatenating all the parts in order, the resultant string should be s.

Return a list of integers representing the size of these parts.

Example 1:

Input: s = “ababcbacadefegdehijhklij”
Output: [9,7,8]
Explanation:
The partition is “ababcbaca”, “defegde”, “hijhklij”.
This is a partition so that each letter appears in at most one part.
A partition like “ababcbacadefegde”, “hijhklij” is incorrect, because it splits s into less parts.

Example 2:

Input: s = “eccbbbbdec”
Output: [10]

my solution

class Solution {
public:
    vector<int> partitionLabels(string s) {
        int record[26][2] = {0};
        vector<vector<int>> vec, merged;
        for(int i=0; i<s.size(); i++){
            if(!record[s[i]-'a'][0]) record[s[i]-'a'][0] = i+1;
            else record[s[i]-'a'][1] = i+1; 
        }
        for(int i=0; i<26; i++){
            if(record[i][0] || record[i][1]){
                if(!record[i][1]) vec.push_back({record[i][0]-1, record[i][0]-1});
                else vec.push_back({record[i][0]-1, record[i][1]-1});
            }
        }
        sort(vec.begin(), vec.end());
        for(auto it : vec){
            if(merged.empty() || merged.back()[1] < it[0]) 
                merged.push_back(it);
            else merged.back()[1] = max(merged.back()[1], it[1]);
        }
        vector<int> res;
        for(auto row : merged)
            res.push_back(row[1] - row[0] + 1);
        return res;
    }
};

·note:·
my solution is not good, i just use iterate through the string and get a 26x2 table, which record the letter’s start index and end index, and convert it in to intervals, and then i use the algorithm ti merge the interval, and finally get the ans

faster solution

vector<int> partitionLabels(string s)
{
	 int M[256]{0};   
	 vector<int> out;
	 for(int i{0}; i<size(s); ++i) M[s[i]]=i;
	 for(int i{0}, j{-1}, MM{0}; i<size(s); ++i)
		 if((MM = max(MM, M[s[i]]))==i)
			 out.push_back(i-j), j=i; 
	 return out;
}

在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值