博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
superReducedString-hankerrank
阅读量:7002 次
发布时间:2019-06-27

本文共 814 字,大约阅读时间需要 2 分钟。

Steve has a string of lowercase characters in range ascii[‘a’..’z’]. He wants to reduce the string to its shortest length by doing a series of operations. In each operation he selects a pair of adjacent lowercase letters that match, and he deletes them. For instance, the string aab could be shortened to b in one operation.

Steve’s task is to delete as many characters as possible using this method and print the resulting string. If the final string is empty, print Empty String

aaabccddd → abccddd → abddd → abd
aa → Empty String
baab → bb → Empty String
string superReducedString(string s) {
for(int i=0;i<s.size()-1;)
{
  if(s.size()==0)
   { return "Empty String";}
  if(s[i]==s[i+1])
  {
    s.erase(i,2);
    i=0;
   }
  else
  {
    i++;
   }
}
return s;

转载于:https://www.cnblogs.com/wzhtql/p/10539651.html

你可能感兴趣的文章