Find Case Sensitive Pattern

 Find Case Sensitive Pattern 

PROBLEM STATEMENT :

The program must accept two string values S and P containing only alphabets as the input. The program must print all possible substrings of S that match the pattern P. If a substring matches the pattern P, then the case of each alphabet in the pattern P matches with the corresponding alphabet in the substring. The substrings must be printed in the order of their occurrence. If there is no such substring in S, then the program must print -1 as the output. 

Boundary Condition(s): 
1 <= Length of P <= Length of S <= 1000 

Input Format: 
The first line contains S. 
The second line contains P. 

Output Format: 

The lines, each contains a substring matches the pattern P or the first line contains -1. 

Example Input/Output 1: 
Input: () 
SkillRack
Do
Output: Sk Ra

Explanation: 
Here the given pattern is Do. 
The case of each alphabet in the pattern Do matches the substrings Sk and Ra. 
Hence the output is 
Sk
Ra 

Example Input/Output 2: Input: ()
AtAAnTheIsWasWere
IoT
Output: AtA AnT IsW

Example Input/Output 3: Input: ()
Mountain 
HI
Output: -1




SOLUTION :

C (Programming Language)


      

#include<stdio.h> #include<stdlib.h> #include<string.h> //For String length function #include<ctype.h> //For islower function int main() { char s[1001],p[1001]; scanf("%s\n%s",s,p); int l1=strlen(s),l2=strlen(p),i,j,flag,printed=0; for(i=0;i<=l1-l2;++i) { int flag=1; for(j=0;j<l2;++j) { if(islower(p[j])!=islower(s[i+j])) { flag=0; } } if(flag==1) { for(j=i;j<i+l2;++j) { printf("%c",s[j]); } printf("\n"); printed=1; } } if(printed==0) printf("-1"); }



C++ (CPP)

       

#include <bits/stdc++.h> using namespace std; int main(int argc, char** argv) { string s,p; cin>>s>>p; int l1=s.size(),l2=p.size(),i,j,printed=0; for(i=0;i<=l1-l2;++i) { int flag=1; for(j=0;j<l2;++j) { if(islower(p[j])!=islower(s[i+j])) { flag=0; } } if(flag==1) { cout<<s.substr(i,l2)<<"\n"; printed=1; } } if(printed==0) cout<<"-1"; }



JAVA

       

import java.util.*; public class Hello { public static void main(String[] args) { Scanner sc=new Scanner(System.in); String s=sc.nextLine().trim(); String p=sc.next(); int l1=s.length(),l2=p.length(),i,j,flag,printed=0; for(i=0;i<=l1-l2;++i) { flag=1; for(j=0;j<l2;++j) { if(Character.isLowerCase(p.charAt(j))!=Character.isLowerCase(s.charAt(j+i))) { flag=0; break; } } if(flag==1) { System.out.println(s.substring(i,i+l2)); printed=1; } } if(printed==0) System.out.println("-1"); } }



PYTHON

       

def caseValue(ch): v=ord(ch) if v>=65 and v<=90: return '1' if v>=97 and v<=122: return '0' s=input().strip() p=input() s1=''.join(list(map(caseValue,s))) p1=''.join(list(map(caseValue,p))) l1=len(s1) l2=len(p1) for i in range(l1-l2+1): if s1[i:i+l2]==p1: print(s[i:i+l2]) if s1.count(p1)==0: print("-1")



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