You are given a license key represented as a string S which consists only alphanumeric character and dashes. The string is separated into N+1 groups by N dashes.
Given a number K, we would want to reformat the strings such that each group contains exactly K characters, except for the first group which could be shorter than K, but still must contain at least one character. Furthermore, there must be a dash inserted between two groups and all lowercase letters should be converted to uppercase.
Given a non-empty string S and a number K, format the string according to the rules described above.
Example 1:
Input: S = "5F3Z-2e-9-w", K = 4
Output: "5F3Z-2E9W"
Explanation: The string S has been split into two parts, each part has 4 characters.
Note that the two extra dashes are not needed and can be removed.
Example 2:
Input: S = "2-5g-3-J", K = 2
Output: "2-5G-3J"
Explanation: The string S has been split into three parts, each part has 2 characters except the first part as it could be shorter as mentioned above.
Note:
The length of string S will not exceed 12,000, and K is a positive integer.
String S consists only of alphanumerical characters (a-z and/or A-Z and/or 0-9) and dashes(-).
Your car starts at position 0 and speed +1 on an infinite number line. (Your car can go into negative positions.)
Your car drives automatically according to a sequence of instructions A (accelerate) and R (reverse).
When you get an instruction “A”, your car does the following: position += speed, speed *= 2.
When you get an instruction “R”, your car does the following: if your speed is positive then speed = -1 , otherwise speed = 1. (Your position stays the same.)
For example, after commands “AAR”, your car goes to positions 0->1->3->3, and your speed goes to 1->2->4->-1.
Now for some target position, say the length of the shortest sequence of instructions to get there.
Example 1:Input:
target = 3
Output: 2
Explanation:
The shortest instruction sequence is "AA".
Your position goes from 0->1->3.
Example 2:Input:
target = 6
Output: 5
Explanation:
The shortest instruction sequence is "AAARA".
Your position goes from 0->1->3->7->7->6.
Note:
1 <= target <= 10000.
Visualization of the Solution
Solution 1: BFS
C++/Str
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
// Author: Huahua
// Running time: 221 ms
classSolution{
public:
intracecar(inttarget){
queue<pair<int,int>>q;
q.push({0,1});
unordered_set<string>v;
v.insert("0_1");
v.insert("0_-1");
intsteps=0;
while(!q.empty()){
intsize=q.size();
while(size--){
autop=q.front();q.pop();
intpos=p.first;
intspeed=p.second;
{
intpos1=pos+speed;
intspeed1=speed*2;
pair<int,int>p1{pos1,speed1};
if(pos1==target)returnsteps+1;
if(p1.first>0&&p1.first<2*target)
q.push(p1);
}
{
intspeed2=speed>0?-1:1;
pair<int,int>p2{pos,speed2};
stringkey2=to_string(pos)+"_"+to_string(speed2);
if(!v.count(key2)){
q.push(p2);
v.insert(key2);
}
}
}
++steps;
}
return-1;
}
};
C++/Int
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
// Author: Huahua
// Running time: 16 ms
classSolution{
public:
intracecar(inttarget){
queue<pair<int,int>>q;
q.push({0,1});
unordered_set<int>v;
v.insert({0,2});
intsteps=0;
while(!q.empty()){
intsize=q.size();
while(size--){
autop=q.front();q.pop();
intpos=p.first;
intspeed=p.second;
pair<int,int>p1={pos+speed,speed*2};
if(p1.first==target)returnsteps+1;
if(p1.first>0&&p1.first+speed<2*target)
q.push(p1);
intspeed2=speed>0?-1:1;
pair<int,int>p2={pos,speed2};
intkey=(pos<<2)|(speed2+1);
if(!v.count(key)&&p2.first>=target/2){
q.push(p2);
v.insert(key);
}
}
++steps;
}
return-1;
}
};
Solution 2: DP O(TlogT)
C++
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
// Author: Huahua
// Running time: 4 ms
classSolution{
public:
intracecar(inttarget){
m_=vector<int>(target+1,0);
returndp(target);
}
private:
vector<int>m_;
intdp(intt){
if(m_[t]>0)returnm_[t];
intn=ceil(log2(t+1));
// AA...A (nA) best case
if(1<<n==t+1)returnm_[t]=n;
// AA...AR (nA + 1R) + dp(left)
m_[t]=n+1+dp((1<<n)-1-t);
for(intm=0;m<n-1;++m){
intcur=(1<<(n-1))-(1<<m);
//AA...ARA...AR (n-1A + 1R + mA + 1R) + dp(left)
m_[t]=min(m_[t],n+m+1+dp(t-cur));
}
returnm_[t];
}
};
Solution 3: DP O(T^2)
m[t][d] : min steps to reach t and facing d (0 = right, 1 = left)
We are given head, the head node of a linked list containing unique integer values.
We are also given the list G, a subset of the values in the linked list.
Return the number of connected components in G, where two values are connected if they appear consecutively in the linked list.
Example 1:
Input:
head: 0->1->2->3
G = [0, 1, 3]
Output: 2
Explanation:
0 and 1 are connected, so [0, 1] and [3] are the two connected components.
Example 2:
Input:
head: 0->1->2->3->4
G = [0, 3, 1, 4]
Output: 2
Explanation:
0 and 1 are connected, 3 and 4 are connected, so [0, 1] and [3, 4] are the two connected components.
Note:
If N is the length of the linked list given by head, 1 <= N <= 10000.
The value of each node in the linked list will be in the range [0, N - 1].
Given a paragraph and a list of banned words, return the most frequent word that is not in the list of banned words. It is guaranteed there is at least one word that isn’t banned, and that the answer is unique.
Words in the list of banned words are given in lowercase, and free of punctuation. Words in the paragraph are not case sensitive. The answer is in lowercase.
Example:Input:
paragraph = "Bob hit a ball, the hit BALL flew far after it was hit."
banned = ["hit"]
Output: "ball"
Explanation:
"hit" occurs 3 times, but it is a banned word.
"ball" occurs twice (and no other word does), so it is the most frequent non-banned word in the paragraph.
Note that words in the paragraph are not case sensitive,
that punctuation is ignored (even if adjacent to words, such as "ball,"),
and that "hit" isn't the answer even though it occurs more because it is banned.
Note:
1 <= paragraph.length <= 1000.
1 <= banned.length <= 100.
1 <= banned[i].length <= 10.
The answer is unique, and written in lowercase (even if its occurrences in paragraph may have uppercase symbols, and even if it is a proper noun.)
paragraph only consists of letters, spaces, or the punctuation symbols !?',;.
Different words in paragraph are always separated by a space.
There are no hyphens or hyphenated words.
Words only consist of letters, never apostrophes or other punctuation symbols.