当前位置:网站首页>339 leetcode word rules
339 leetcode word rules
2022-04-21 23:24:00 【sp_ thirteen billion two hundred and thirty million four hundre】

class Solution
{
public:
bool wordPattern(string pattern, string s)
{
unordered_map<char, string> ch2str;
unordered_map<string, char> str2ch;
int len = s.size();
int i = 0;
for (auto ch : pattern)
{
if (i > len)
{
return false;
}
int j = i;
while (j < len && s[j] != ' ') {
++j; }// Split words
const string& tmp = s.substr(i, j - i);
if((ch2str.count(ch)&& ch2str[ch]!=tmp) || str2ch.count(tmp)&&str2ch[tmp]!=ch)
{
return false;
}
ch2str[ch] = tmp;
str2ch[tmp] = ch;
i = j + 1;
}
return i >= len;
}
};
int main()
{
Solution A;
cout << A.wordPattern("abba","dog dog dog dog") << endl;
return 0;
}
Time complexity :O(n + m), among n by pattern The length of ,m by \str The length of . The average sharing time complexity of inserting and querying hash tables is O(n+m). Each character is traversed at most once
Spatial complexity :O(n+m), among n by pattern The length of ,m by str The length of . In the worst case , We need to store pattern Each character and str Every string in
版权声明
本文为[sp_ thirteen billion two hundred and thirty million four hundre]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204212322223162.html
边栏推荐
- BUUCTF 你(竟)然赶我走
- 【H.264】SPS 计算帧率方法
- (十)瑞芯微rk3568 中qt工程ffmpeg 拉取rtsp视频流
- Golang force buckle leetcode 388 The longest absolute path of the file
- 第2章 MySQL数据库的安装
- 领导们秘而不宣的3款软件,实用强大,办公效率和职场升迁利器
- .101键盘事件
- Custom template problem help, automatically add time and date
- [openh264] timing of SPS_ info_ present_ flag
- MySQL Chapter 5 addition, deletion, modification and query of MySQL table data
猜你喜欢

How to build an observable system that can "evolve continuously"| QCon

Apache Flink series - ④ stateful functions

How does IOT platform realize business configuration center

Buuctf, you drove me away

音视频基本概念和FFmpeg的简单入门

叹为观止,4款惊喜满满的高质量软件,使用起来倍感舒适

自定義登錄成功處理

Finally, someone made it clear that this is the global one-piece network technology with low delay

prompt 你到底行不行?

How does the applet integrate instant messaging with instant messaging
随机推荐
thinkphp开发卡密社区系统
Sélection et évolution des microservices dans l'architecture native du cloud
7.3.1 homogeneous coordinate transformation & homogeneous transformation matrix
系列文章分类汇总(第二期)
Pytorch (V) -- Notes
Changsha good man
【ACM】46. Full Permutation (1. Here, the previous elements need to be used for permutation, so StartIndex is not used (only for combination and division); 2. Pay attention to whether the elements in t
A collection of large factories and face classics. Do you know these knowledge points
Technology, products and brand are not problems. For SAIC Audi, these two points may be life and death
Pytoch framework | torch nn. modules. Module(nn.Module)
Vs2019 configuring opencv4
Go language self-study series | golang structure
Exception handler
Ruffian Heng embedded: talk about the application and influence of system watchdog wdog1 in the startup of i.mxrt1xxx system
Ijcai2022 employment results released! The acceptance rate is 15%. Did you win?
Sélection et évolution des microservices dans l'architecture native du cloud
【速卖通代运营】2022跨境电商怎么做?速卖通今年重点要做三件事
入参有汉字,报错500,服务器内部错误
Golang force buckle leetcode 380 O (1) time insertion, deletion and acquisition of random elements
PP语义检索系统