HSL Colors Combination

 HSL Colors Combination

PROBLEM STATEMENT 

Given string str which consists of only 3 letters representing the color,(H) Hue, (S) Saturation, (L) Lightness, called HSL colors. The task is to count the occurrence of ordered triplet “H, S, L” in a given string and give this count as the output.

Boundary Condition:

1 <= len(str) <= 1000


Example Input/Output 1: 
Input: () 
HHSL 
Output: 2

Explanation: 

There are two triplets of RGB in the given string:

=> H at index 0, S at index 2, and L at index 3 form one triplet of HSL.

=> H at index 1, S at index 2, and L at index 3 forms the second triplet of HSL.


Example Input/Output 2: 
Input: () 
SHL 
Output: 0

Explanation: 

No triplets exist.





                    


            1)    LEARN THRICE 

                                👇 

            2)    THINK TWICE

                                👇 

            3)    APPLY ONCE




SOLUTION :

C++ (CPP) :

       

#include <bits/stdc++.h> using namespace std; int main(int argc, char** argv) { string s; cin>>s; int lightness=0,hue=0,i,result=0; for(i=0;i<s.size();++i) { if(s[i]=='L') lightness++; } for(i=0;i<s.size();++i) { if(s[i]=='L') lightness--; else if(s[i]=='H') hue++; else if(s[i]=='S') result+=(hue*lightness); } cout<<result; }


PYTHON :

       

s=input().strip() lightness=s.count('L') hue=0 result=0 for i in s: if i=='L': lightness-=1 elif i=='H': hue+=1 elif i=='S': result+=(hue*lightness) print(result)


Never Stop Learning !!


Comments

Popular Posts

Infix to Postfix Expression

Trailing zeroes in factorial