Skip to content

Commit c28a128

Browse files
committed
[Function add]
1. Add leetcode solutions.
1 parent d856b3b commit c28a128

4 files changed

+256
-0
lines changed

leetcode/392. Is Subsequence.md

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
## 392. Is Subsequence
2+
3+
### Question
4+
Given a string s and a string t, check if s is subsequence of t.
5+
6+
You may assume that there is only lower case English letters in both s and t. t is potentially a very long (length ~= 500,000) string, and s is a short string (<=100).
7+
8+
A subsequence of a string is a new string which is formed from the original string by deleting some (can be none) of the characters without disturbing the relative positions of the remaining characters. (ie, "ace" is a subsequence of "abcde" while "aec" is not).
9+
10+
```
11+
Example 1:
12+
s = "abc", t = "ahbgdc"
13+
14+
Return true.
15+
16+
Example 2:
17+
s = "axc", t = "ahbgdc"
18+
19+
Return false.
20+
```
21+
22+
Follow up:
23+
If there are lots of incoming S, say S1, S2, ... , Sk where k >= 1B, and you want to check one by one to see if T has its subsequence. In this scenario, how would you change your code?
24+
25+
Credits:
26+
Special thanks to @pbrother for adding this problem and creating all test cases.
27+
28+
### Solutions
29+
* Method 1: 2 pointers
30+
```Java
31+
class Solution {
32+
public boolean isSubsequence(String s, String t) {
33+
int sLen = s.length(), tLen = t.length();
34+
if(sLen > tLen) return false;
35+
else if(sLen == 0) return true;
36+
int index1 = 0, index2 = 0;
37+
while(index1 < sLen && index2 < tLen){
38+
if(s.charAt(index1) == t.charAt(index2)){
39+
index1++;
40+
index2++;
41+
}else{
42+
index2++;
43+
}
44+
}
45+
return index1 == sLen;
46+
}
47+
}
48+
```
49+
50+
* Method 2: dp
51+
```Java
52+
class Solution {
53+
public boolean isSubsequence(String s, String t) {
54+
int sLen = s.length(), tLen = t.length();
55+
boolean[][] dp = new boolean[tLen + 1][sLen + 1];
56+
for(int i = 0; i <= tLen; i++){
57+
dp[i][0] = true;
58+
}
59+
for(int i = 1; i <= tLen; i++){
60+
for(int j = 1; j <= sLen; j++){
61+
if(s.charAt(j - 1) == t.charAt(i - 1)){
62+
dp[i][j] = dp[i - 1][j - 1];
63+
}else{
64+
dp[i][j] |= dp[i - 1][j];
65+
}
66+
}
67+
}
68+
return dp[tLen][sLen];
69+
}
70+
}
71+
```
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
## 717. 1-bit and 2-bit Characters
2+
3+
### Question
4+
We have two special characters. The first character can be represented by one bit 0. The second character can be represented by two bits (10 or 11).
5+
6+
Now given a string represented by several bits. Return whether the last character must be a one-bit character or not. The given string will always end with a zero.
7+
8+
```
9+
Example 1:
10+
11+
Input:
12+
bits = [1, 0, 0]
13+
Output: True
14+
Explanation:
15+
The only way to decode it is two-bit character and one-bit character. So the last character is one-bit character.
16+
17+
Example 2:
18+
19+
Input:
20+
bits = [1, 1, 1, 0]
21+
Output: False
22+
Explanation:
23+
The only way to decode it is two-bit character and two-bit character. So the last character is NOT one-bit character.
24+
```
25+
26+
Note:
27+
1. 1 <= len(bits) <= 1000.
28+
2. bits[i] is always 0 or 1.
29+
30+
### Solutions:
31+
* Method 1: Array 7m50s
32+
```Java
33+
class Solution {
34+
public boolean isOneBitCharacter(int[] bits) {
35+
int index = 0;
36+
while(index < bits.length){
37+
if(index == bits.length - 1) return true;
38+
if(bits[index] == 0) index++;
39+
else index += 2;
40+
}
41+
return false;
42+
}
43+
}
44+
```
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
## 792. Number of Matching Subsequences
2+
3+
### Question
4+
Given string S and a dictionary of words words, find the number of words[i] that is a subsequence of S.
5+
6+
```
7+
Example :
8+
Input:
9+
S = "abcde"
10+
words = ["a", "bb", "acd", "ace"]
11+
Output: 3
12+
Explanation: There are three words in words that are a subsequence of S: "a", "acd", "ace".
13+
```
14+
15+
Note:
16+
1. All words in words and S will only consists of lowercase letters.
17+
2. The length of S will be in the range of [1, 50000].
18+
3. The length of words will be in the range of [1, 5000].
19+
4. The length of words[i] will be in the range of [1, 50].
20+
21+
### Solutions:
22+
* Method 1:
23+
```Java
24+
class Solution {
25+
private int sLen;
26+
private String s;
27+
public int numMatchingSubseq(String S, String[] words) {
28+
sLen = S.length();
29+
s = S;
30+
int res = 0;
31+
for(String t : words){
32+
if(isSub(t)) res++;
33+
}
34+
return res;
35+
}
36+
private boolean isSub(String t){
37+
int tLen = t.length();
38+
int index1 = 0, index2 = 0;
39+
while(index1 < tLen && index2 < sLen){
40+
if(s.charAt(index2) == t.charAt(index1)){
41+
index1++;
42+
}
43+
index2++;
44+
}
45+
return index1 == tLen;
46+
}
47+
}
48+
```
49+
50+
* Method 2: HashMap + Binary search
51+
```Java
52+
class Solution {
53+
private Map<Character, List<Integer>> map;
54+
public int numMatchingSubseq(String S, String[] words) {
55+
this.map = new HashMap<>();
56+
for(int i = 0; i < S.length(); i++){
57+
char c = S.charAt(i);
58+
List<Integer> list = map.containsKey(c) ? map.get(c): new ArrayList<>();
59+
list.add(i);
60+
map.put(c, list);
61+
}
62+
int res = 0;
63+
for(String word : words){
64+
if(match(word)) res++;
65+
}
66+
return res;
67+
}
68+
private boolean match(String s){
69+
char[] arr = s.toCharArray();
70+
int pre = -1;
71+
for(char c : arr){
72+
if(!map.containsKey(c)) return false;
73+
List<Integer> list = map.get(c);
74+
int index = Collections.binarySearch(list, pre + 1);
75+
if(index < 0) index = -index - 1;
76+
if(index >= list.size()) return false;
77+
pre = list.get(index);
78+
}
79+
return true;
80+
}
81+
}
82+
```
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
## 833. Find And Replace in String
2+
3+
### Question
4+
To some string S, we will perform some replacement operations that replace groups of letters with new ones (not necessarily the same size).
5+
6+
Each replacement operation has 3 parameters: a starting index i, a source word x and a target word y. The rule is that if x starts at position i in the original string S, then we will replace that occurrence of x with y. If not, we do nothing.
7+
8+
For example, if we have S = "abcd" and we have some replacement operation i = 2, x = "cd", y = "ffff", then because "cd" starts at position 2 in the original string S, we will replace it with "ffff".
9+
10+
Using another example on S = "abcd", if we have both the replacement operation i = 0, x = "ab", y = "eee", as well as another replacement operation i = 2, x = "ec", y = "ffff", this second operation does nothing because in the original string S[2] = 'c', which doesn't match x[0] = 'e'.
11+
12+
All these operations occur simultaneously. It's guaranteed that there won't be any overlap in replacement: for example, S = "abc", indexes = [0, 1], sources = ["ab","bc"] is not a valid test case.
13+
14+
```
15+
Example 1:
16+
17+
Input: S = "abcd", indexes = [0,2], sources = ["a","cd"], targets = ["eee","ffff"]
18+
Output: "eeebffff"
19+
Explanation: "a" starts at index 0 in S, so it's replaced by "eee".
20+
"cd" starts at index 2 in S, so it's replaced by "ffff".
21+
22+
Example 2:
23+
24+
Input: S = "abcd", indexes = [0,2], sources = ["ab","ec"], targets = ["eee","ffff"]
25+
Output: "eeecd"
26+
Explanation: "ab" starts at index 0 in S, so it's replaced by "eee".
27+
"ec" doesn't starts at index 2 in the original S, so we do nothing.
28+
```
29+
30+
Notes:
31+
1. 0 <= indexes.length = sources.length = targets.length <= 100
32+
2. 0 < indexes[i] < S.length <= 1000
33+
3. All characters in given inputs are lowercase letters.
34+
35+
36+
### Solutions:
37+
* Method 1:
38+
```Java
39+
class Solution {
40+
public String findReplaceString(String S, int[] indexes, String[] sources, String[] targets) {
41+
List<int[]> list = new ArrayList<>();
42+
for(int i = 0; i < indexes.length; i++){
43+
list.add(new int[]{indexes[i], i});
44+
}
45+
Collections.sort(list, new Comparator<int[]>(){
46+
@Override
47+
public int compare(int[] a, int[] b){
48+
return b[0] - a[0];
49+
}
50+
});
51+
for(int[] l : list){
52+
if(S.substring(l[0]).startsWith(sources[l[1]])){
53+
S = S.substring(0, l[0]) + targets[l[1]] + S.substring(l[0] + sources[l[1]].length());
54+
}
55+
}
56+
return S;
57+
}
58+
}
59+
```

0 commit comments

Comments
 (0)