Given a string S, find the number of different non-empty palindromic subsequences in S, and return that number modulo 10^9 + 7.
A subsequence of a string S is obtained by deleting 0 or more characters from S.
A sequence is palindromic if it is equal to the sequence reversed.
Two sequences A_1, A_2, ... and B_1, B_2, ... are different if there is some i for which A_i != B_i.
Example 1:
Input:
S = 'bccb'
Output: 6
Explanation:
The 6 different non-empty palindromic subsequences are 'b', 'c', 'bb', 'cc', 'bcb', 'bccb'.
Note that 'bcb' is counted only once, even though it occurs twice.
Example 2:
Input:
S = 'abcdabcdabcdabcdabcdabcdabcdabcddcbadcbadcbadcbadcbadcbadcbadcba'
Output: 104860361
Explanation:
There are 3104860382 different non-empty palindromic subsequences, which is 104860361 modulo 10^9 + 7.
Note:
The length of S will be in the range [1, 1000].
Each character S[i] will be in the set {'a', 'b', 'c', 'd'}.
Implement a MyCalendarTwo class to store your events. A new event can be added if adding the event will not cause a triple booking.
Your class will have one method, book(int start, int end). Formally, this represents a booking on the half open interval [start, end), the range of real numbers x such that start <= x < end.
A triple booking happens when three events have some non-empty intersection (ie., there is some time that is common to all 3 events.)
For each call to the method MyCalendar.book, return true if the event can be added to the calendar successfully without causing a triple booking. Otherwise, return false and do not add the event to the calendar.
Your class will be called like this: MyCalendar cal = new MyCalendar();MyCalendar.book(start, end)
Example 1:
1
2
3
4
5
6
7
8
9
10
11
12
13
MyCalendar();
MyCalendar.book(10,20);// returns true
MyCalendar.book(50,60);// returns true
MyCalendar.book(10,40);// returns true
MyCalendar.book(5,15);// returns false
MyCalendar.book(5,10);// returns true
MyCalendar.book(25,55);// returns true
Explanation<b>:</b>
The first two events can be booked.The third event can be doublebooked.
The fourth event(5,15)can'tbe booked,because it would result inatriple booking.
The fifth event(5,10)can be booked,asit does notusetime10which isalready doublebooked.
The sixth event(25,55)can be booked,asthe time in[25,40)will be doublebooked with the third event;
the time[40,50)will be single booked,andthe time[50,55)will be doublebooked with the second event.
Note:
The number of calls to MyCalendar.book per test case will be at most 1000.
In calls to MyCalendar.book(start, end), start and end are integers in the range [0, 10^9].
Implement a MyCalendar class to store your events. A new event can be added if adding the event will not cause a double booking.
Your class will have the method, book(int start, int end). Formally, this represents a booking on the half open interval [start, end), the range of real numbers x such that start <= x < end.
A double booking happens when two events have some non-empty intersection (ie., there is some time that is common to both events.)
For each call to the method MyCalendar.book, return true if the event can be added to the calendar successfully without causing a double booking. Otherwise, return false and do not add the event to the calendar.
Your class will be called like this: MyCalendar cal = new MyCalendar();MyCalendar.book(start, end)
Example 1:
1
2
3
4
5
6
7
MyCalendar();
MyCalendar.book(10,20);// returns true
MyCalendar.book(15,25);// returns false
MyCalendar.book(20,30);// returns true
Explanation:
The first event can be booked.The second can'tbecause time15isalready booked by another event.
The third event can be booked,asthe first event takes every time less than20,but notincluding20.
Note:
The number of calls to MyCalendar.book per test case will be at most 1000.
In calls to MyCalendar.book(start, end), start and end are integers in the range [0, 10^9].