Heavy Alphabet

Lily wants to build a beautiful string. She has been adding alphabets together to make this string. …

Click here to read the complete problem statement.


If you need help solving this problem, mention your approach and ask specific questions. Please avoid sharing your code and asking the Community to figure out “what’s wrong”.

what is the problem of my code??

T=int(input())
for x in range(T):
S=input()
a=[str(j) for j in S]
b=[]
for i in range(0, len(a)):
if i==0:
b.append(a[i])
else:
if a[i]==a[i-1]:
b.append(“#”)
else:
b.append(a[i])
print(“”.join(map(str, b)))

string s;

  cin >> s;

  map<char, bool> MAP;

  for (int i = 0; i < (int)s.size(); i++)

     s[i] = tolower(s[i]);

  ///cout << s << endl;

  for (int i = 0; i < (int)s.size(); i++)

     MAP[s[i]] = false;

  for (int i = 0; i < (int)s.size(); ++i)

  {

     if (MAP[s[i]] == true)

        cout << "#";

     else

     {

        cout << s[i];

        MAP[s[i]] = true;

     }

     if (s[i] != s[i + 1])

     {

        MAP[s[i]] = false;

        MAP[s[i + 1]] = false;

     }

  }

  cout << endl;

i think it should work…where is the problem

when i wrote (int)strlen(s) it said tle
but when i wrote int length=strlen(s);
now it said ok ! whats the logic

for (int i = 0; i < (int)strlen(s); i++)

   s[i] = tolower(s[i]);

i tried it first …it said WA at test 3

then i tried to not make it lower at first rather compare like tolower(s[i]) ,
this time it is ok …whats the problem? i think this problem has a many problems inside it…i never faced these problems in any problem

Is there any problem?

https://toph.co/s/671604

(1)
strlen() runs in O(n)
So, every time you call strlen(s), then it finds the length of the string after n ± 1 (size of the string) iterations.
So, if size of the string is 10^5, then your code will need 10^10 iterations( for every iteration in the loop, strlen() is called) . It’s HUGE for 1 second!
But string class in C++ is not just char[]. size() or lenght() function in string class runs in O(1). Because as far as I know string object tracks the length of it’s string in a variable. Every time you call size() or length() function, it just returns the value of that variable.
So strlen(str) is not same as str.size() or str.length().

(2)
Try this input, you’ll understand:

1
aBBBaaAaAAa

output should be:
aB##a######

1 Like

What is the wrong in my code?Getting WA in test 2.

#include<stdio.h>
#include<string.h>
int main()
{
    int t,i,j;
    char str[100001];
    char str1[100001];
    scanf("%d",&t);
    for(i=1;i<=t;i++)
    {
        scanf("%s",&str);
        for(j=0;j<strlen(str);j++)
        {
            if(j==0)
                str1[j]=str[j];
            else if(str[j]==str[j-1])
                str1[j]='#';
            else if(str[j]==(str[j-1]+32) )
                str1[j]='#';
            else if(str[j]+32==str[j-1] )
                str1[j]='#';
            else
                str1[j]=str[j];

        }
            printf("%s\n",str1);
    }
    return 0;
}