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:
Example Input/Output 2:
Input: ()
SHL
Output:
0Explanation:
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)

Comments
Post a Comment