Laptops & WI-FI Hotspots

Laptops & WI-FI Hotspots

PROBLEM STATEMENT :

The program must accept a string S as the input. The string S contains only the asterisks(*), the hash symbols(#), the hyphens(-), and the alphabet(L).

- Each asterisk represents a WI-FI hotspot.

- Each hash symbol represents an obstacle.

- Each hyphen represents empty space.

- Each alphabet L represents a laptop.

Each laptop has a special feature of connecting to any Wi-Fi network from any distance away, as long as there are not any obstructions between the hotspot and the laptop.

For each laptop, the program must print the number of WI-FI hotspots that the laptop can connect as the output.

Note: The alphabet L always occurs at least once in the string.

Boundary Condition(s):

1 <= Length of S <= 100

Input Format:

The first line contains S.

Output Format:

The first line contains the integer values representing the number of WI-FI hotspots that the laptop can connect.

Example Input/Output 1: 
Input: () 
*---L--*---* 
Output: 3

Explanation: 

Here the given string is *---L--*---*

There is only one laptop that can connect to all the three WI-FI hotspots.So 3 is printed as the output.

Example Input/Output 2: 
Input: () 
L----*--*-#--*-L-#-*-#--L 
Output: 2 1 0


Example Input/Output 3: 
Input: () 
L***#L#***L*L* 
Output: 3 0 5 5





                    


            1)    LEARN THRICE 

                                👇 

            2)    THINK TWICE

                                👇 

            3)    APPLY ONCE


SOLUTION :

C (Programming Language)


      

#include<stdio.h> #include<stdlib.h> int main() { char S[101]; int l,ind,leftInd,rightInd,cnt; scanf("%s%n",S,&l); for(ind=0;ind<l;++ind) { if(S[ind]=='L') { leftInd=ind; rightInd=ind; cnt=0; while(leftInd>=0&&S[leftInd]!='#') { if(S[leftInd]=='*') cnt++; leftInd--; } while(rightInd<l&&S[rightInd]!='#') { if(S[rightInd]=='*') cnt++; rightInd++; } printf("%d ",cnt); } } }



C++ (CPP)

       

#include <bits/stdc++.h> using namespace std; int main(int argc, char** argv) { string S; int l,ind,leftInd,rightInd,cnt; cin>>S; l=S.size(); for(ind=0;ind<l;++ind) { if(S[ind]=='L') { leftInd=ind-1; rightInd=ind+1; cnt=0; while(leftInd>=0&&S[leftInd]!='#') { if(S[leftInd]=='*') cnt++; leftInd--; } while(rightInd<l&& S[rightInd]!='#') { if(S[rightInd]=='*') cnt++; rightInd++; } cout<<cnt<<" "; } } }



JAVA

       

import java.util.*; public class Hello { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int l,ind,leftInd,rightInd,cnt; Boolean leftFlag,rightFlag; char[] S = sc.next().trim().toCharArray(); l = S.length; for(ind = 0 ; ind < l ; ++ind) { if(S[ind] == 'L') { leftInd = ind-1; rightInd = ind+1; cnt = 0; while(leftInd>=0&&S[leftInd]!='#') { if(S[leftInd]=='*') { cnt++; } leftInd--; } while(rightInd<l&&S[rightInd]!='#') { if(S[rightInd]=='*') { cnt++; } rightInd++; } System.out.print(cnt+" "); } } } }



PYTHON SOLUTION 1

       

S=input().strip() l=len(S) for i in range(l): if S[i]=='L': left=i-1 right=i+1 cnt=0 while left>=0 and S[left]!='#': if S[left]=='*': cnt+=1 left-=1 while right<l and S[right]!='#': if S[right]=='*': cnt+=1 right+=1 print(cnt,end=' ')


PYTHON SOLUTION 2

       

print(*[i.count("*") for i in input().split("#")for j in range(i.count("L"))])



Never Stop Learning !!


Comments

Popular posts from this blog

DP (1) - Count number of ways to cover a distance

Zero Insert After K Times One

Left Number Twice Right