Skip to content

Commit d4d242a

Browse files
Add files via upload
1 parent d8f75e3 commit d4d242a

File tree

2 files changed

+112
-0
lines changed

2 files changed

+112
-0
lines changed

CamelcaseMatching.cpp

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
/*
2+
Given an array of strings queries and a string pattern, return a boolean array answer where answer[i] is true if queries[i] matches pattern, and false otherwise.
3+
A query word queries[i] matches pattern if you can insert lowercase English letters pattern so that it equals the query. You may insert each character at any position and you may not insert any characters.
4+
5+
Example 1:
6+
Input: queries = ["FooBar","FooBarTest","FootBall","FrameBuffer","ForceFeedBack"], pattern = "FB"
7+
Output: [true,false,true,true,false]
8+
Explanation: "FooBar" can be generated like this "F" + "oo" + "B" + "ar".
9+
"FootBall" can be generated like this "F" + "oot" + "B" + "all".
10+
"FrameBuffer" can be generated like this "F" + "rame" + "B" + "uffer".
11+
12+
Example 2:
13+
Input: queries = ["FooBar","FooBarTest","FootBall","FrameBuffer","ForceFeedBack"], pattern = "FoBa"
14+
Output: [true,false,true,false,false]
15+
Explanation: "FooBar" can be generated like this "Fo" + "o" + "Ba" + "r".
16+
"FootBall" can be generated like this "Fo" + "ot" + "Ba" + "ll".
17+
18+
Example 3:
19+
Input: queries = ["FooBar","FooBarTest","FootBall","FrameBuffer","ForceFeedBack"], pattern = "FoBaT"
20+
Output: [false,true,false,false,false]
21+
Explanation: "FooBarTest" can be generated like this "Fo" + "o" + "Ba" + "r" + "T" + "est".
22+
*/
23+
24+
class Solution {
25+
public:
26+
vector<bool> camelMatch(vector<string>& queries, string pattern) {
27+
vector<bool> result;
28+
int m=pattern.size();
29+
for(auto query: queries){
30+
int n=query.size(), i=0,j=0;
31+
while(i<n){
32+
if(query[i]==pattern[j] and j<m){
33+
i++;
34+
j++;
35+
}else if((int)query[i]<97){
36+
break;
37+
}else{
38+
i++;
39+
}
40+
}
41+
if(j==m and i==n)
42+
result.push_back(true);
43+
else
44+
result.push_back(false);
45+
}
46+
return result;
47+
}
48+
};

NumberOfWaysToDivideALongCorridor.cpp

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
Along a long library corridor, there is a line of seats and decorative plants. You are given a 0-indexed string corridor of length n consisting of letters 'S' and 'P' where each 'S' represents a seat and each 'P' represents a plant.
3+
One room divider has already been installed to the left of index 0, and another to the right of index n - 1. Additional room dividers can be installed. For each position between indices i - 1 and i (1 <= i <= n - 1), at most one divider can be installed.
4+
Divide the corridor into non-overlapping sections, where each section has exactly two seats with any number of plants. There may be multiple ways to perform the division. Two ways are different if there is a position with a room divider installed in the first way but not in the second way.
5+
Return the number of ways to divide the corridor. Since the answer may be very large, return it modulo 109 + 7. If there is no way, return 0.
6+
7+
Example 1:
8+
Input: corridor = "SSPPSPS"
9+
Output: 3
10+
Explanation: There are 3 different ways to divide the corridor.
11+
The black bars in the above image indicate the two room dividers already installed.
12+
Note that in each of the ways, each section has exactly two seats.
13+
14+
Example 2:
15+
Input: corridor = "PPSPSP"
16+
Output: 1
17+
Explanation: There is only 1 way to divide the corridor, by not installing any additional dividers.
18+
Installing any would create some section that does not have exactly two seats.
19+
20+
Example 3:
21+
Input: corridor = "S"
22+
Output: 0
23+
Explanation: There is no way to divide the corridor because there will always be a section that does not have exactly two seats.
24+
*/
25+
26+
27+
/// DP Solution
28+
class Solution {
29+
public:
30+
int mod=1e9+7;
31+
long long helper(int index, int n, string &corridor){
32+
if(index==n) return 0;
33+
bool flag=false;
34+
int secondChairIndex=-1;
35+
for(int i=index;i<n;i++){
36+
if(corridor[i]=='S'){
37+
if(flag){
38+
secondChairIndex=i;
39+
break;
40+
}else{
41+
flag=true;
42+
}
43+
}
44+
}
45+
if(secondChairIndex==-1) return 0;
46+
long long counter=1;
47+
for(int i=secondChairIndex+1;i<n;i++){
48+
if(corridor[i]=='P'){
49+
if(i==n-1) return 1;
50+
counter++;
51+
}else{
52+
counter=(counter*helper(i,n,corridor))%mod;
53+
break;
54+
}
55+
}
56+
return counter%mod;
57+
}
58+
int numberOfWays(string corridor) {
59+
int n=corridor.size();
60+
return helper(0,n,corridor);
61+
}
62+
};
63+
64+

0 commit comments

Comments
 (0)