文章目录
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;
}

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

413

被折叠的 条评论
为什么被折叠?



