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”.
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)))
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
(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().