admin管理员组文章数量:1794759
LintCode 1531: Automatic Proofreading Program
1531. Automatic Proofreading Program
There are some words spelt wrong. Please write a program to proofread the spell, and return the right spell.
The rules is listed below:
If three same charactors connected together, it's a wrong spell, delete one of the charators, e.g, ooops -> oopsooops−>oops.
If two pairs of same charactors (AABB) connected together, it's a wrong spell, delete one charactor of the second pair, e.g. helloo -> hellohelloo−>hello.
The rules follow the priority from left to right, e.g. the aabbaabb and bbccbbcc are both wrong spell in aabbccaabbcc, you should fix aabbaabb firstly, so the final result is aabccaabcc.
Example
Sample Input 1:
str = "helloo"
Sample Output 1:
“hello”
"lloo" is a wrong spell, delete 'o'.
Sample Input 2:
str = ”woooow“
Sample Output 2:
“woow”
"oooo" is a wrong spell, delete an 'o' firstly, "ooo" is still wrong, delete an 'o'.
Notice
The length of input string is n, 1 <= n <= 10^5 .
字符串均由小写字符组成。
解法1:同向双指针。
class Solution { public: /** * @param str: The string before proofreading. * @return: Return the string after proofreading. */ string automaticProofreadingProgram(string &str) { string res = str; int len = str.size(); int p1 = 0, p2 = 0; for (int p1 = 0; p1 < len; ++p1) { res[p2++] = str[p1]; //AABB if ((p2 >= 3 && res[p2 - 1] == res[p2 - 2] && res[p2 - 1] == res[p2 - 3]) || //WOOOOOOOW (p2 >= 4 && res[p2 - 1] == res[p2 - 2] && res[p2 -3] == res[p2 - 4])) { p2--; } } return res.substr(0, p2); } };
本文标签: LintCodeAutomaticProgramProofreading
版权声明:本文标题:LintCode 1531: Automatic Proofreading Program 内容由林淑君副主任自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.xiehuijuan.com/baike/1686990193a126102.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论