2825. Make String a Subsequence Using Cyclic Increments
You are given two 0-indexed strings str1 and str2.
In an operation, you select a set of indices in str1, and for each index i in the set, increment str1[i] to the next character cyclically. That is 'a' becomes 'b', 'b' becomes 'c', and so on, and 'z' becomes 'a'.
Return true if it is possible to make str2 a subsequence of str1 by performing the operation at most once, and false otherwise.
Note: A subsequence of a string is a new string that is formed from the original string by deleting some (possibly none) of the characters without disturbing the relative positions of the remaining characters.
Example 1:
Input: str1 = "abc", str2 = "ad"
Output: true
Explanation: Select index 2 in str1.
Increment str1[2] to become 'd'.
Hence, str1 becomes "abd" and str2 is now a subsequence. Therefore, true is returned.
Example 2:
Input: str1 = "zc", str2 = "ad"
Output: true
Explanation: Select indices 0 and 1 in str1.
Increment str1[0] to become 'a'.
Increment str1[1] to become 'd'.
Hence, str1 becomes "ad" and str2 is now a subsequence. Therefore, true is returned.
Example 3:
Input: str1 = "ab", str2 = "d"
Output: false
Explanation: In this example, it can be shown that it is impossible to make str2 a subsequence of str1 using the operation at most once.
Therefore, false is returned.
这道题可以两种做法 dp和双指针
dp会导致memory溢出 双指针是联合贪心一起的,和第392题判断子序列差不多
class Solution {
public boolean canMakeSubsequence(String str1, String str2) {
int i = 0;
int j = 0;
int len1 = str1.length();
int len2 = str2.length();
while(i < len1 && j < len2) {
if(str1.charAt(i) == str2.charAt(j)) {
i++;
j++;
}else if(str1.charAt(i) + 1 == str2.charAt(j)) {
i++;
j++;
} else if(str1.charAt(i) == 'z' && str2.charAt(j) == 'a') {
i++;
j++;
} else {
i++;
}
}
return j == len2;
}
}
Given a string s representing a valid expression, implement a basic calculator to evaluate it, and return the result of the evaluation.
Note: You are not allowed to use any built-in function which evaluates strings as mathematical expressions, such as eval().
Example 1:
Input: s = "1 + 1"
Output: 2
Example 2:
Input: s = " 2-1 + 2 "
Output: 3
Example 3:
Input: s = "(1+(4+5+2)-3)+(6+8)"
Output: 23
这道题主要是要处理每个数前面的符号问题(是➕还是➖),用一个stack来判断符号
class Solution {
public int calculate(String s) {
Deque<Integer> stack = new ArrayDeque<>();
int sign = 1;
stack.push(sign);
int n = s.length();
int ret = 0;
int i = 0;
while(i < n) {
char ch = s.charAt(i);
if(ch == ' ') {
i++;
} else if(ch == '+') {
sign = stack.peek();
i++;
} else if(ch == '-') {
sign = -stack.peek();
i++;
} else if(ch == '(') {
stack.push(sign);
i++;
} else if(ch == ')') {
stack.poll();
i++;
} else {
int num = 0;
while(i < n && Character.isDigit(s.charAt(i))) {
num = num*10 + (s.charAt(i) - '0');
i++;
}
ret += num * sign;
}
}
return ret;
}
}
You are given an m x n binary matrix grid, where 0 represents a sea cell and 1 represents a land cell.
A move consists of walking from one land cell to another adjacent (4-directionally) land cell or walking off the boundary of the grid.
Return the number of land cells in grid for which we cannot walk off the boundary of the grid in any number of moves.
Example 1:
Input: grid = [[0,0,0,0],[1,0,1,0],[0,1,1,0],[0,0,0,0]]
Output: 3
Explanation: There are three 1s that are enclosed by 0s, and one 1 that is not enclosed because its on the boundary.
Example 2:
Input: grid = [[0,1,1,0],[0,0,1,0],[0,0,1,0],[0,0,0,0]]
Output: 0
Explanation: All 1s are either on the boundary or can reach the boundary.
Constraints:
m == grid.lengthn == grid[i].length1 <= m, n <= 500grid[i][j]is either0or1.
这道题要注意UnionFind不能加visited数组,不然会漏掉一些值
class UnionFind {
int[] parent;
UnionFind(int n) {
parent = new int[n];
for(int i = 0; i < n; i++) {
parent[i] = i;
}
}
int find(int i) {
if(parent[i] == i) {
return i;
} else {
parent[i] = find(parent[i]);
return parent[i];
}
}
void union(int i, int j) {
int rootI = find(i);
int rootJ = find(j);
if(rootI != rootJ) {
parent[rootI] = rootJ;
}
}
}
class Solution {
int[][] dirs = {{1,0}, {-1, 0}, {0, 1}, {0, -1}};
public int numEnclaves(int[][] grid) {
int m = grid.length;
int n = grid[0].length;
UnionFind uf = new UnionFind(m*n + 1);
for(int j = 0; j < n; j++) {
if(grid[0][j] == 1) {
uf.union(j, m*n);
}
if(grid[m-1][j] == 1) {
uf.union((m-1)*n + j, m*n);
}
}
for(int i = 0; i < m; i++) {
if(grid[i][0] == 1) {
uf.union(n*i, m*n);
}
if(grid[i][n-1] == 1) {
uf.union(n*i + n-1, m*n);
}
}
for(int i = 1; i < m-1; i++) {
for(int j = 1; j < n-1; j++) {
if(grid[i][j] == 1) {
for(int[] dir: dirs) {
int r = i + dir[0];
int c = j + dir[1];
if(r < 0 || r >= m || c < 0 || c >= n) {
continue;
} else {
if(grid[r][c] == 1) {
uf.union(r*n+c, i*n+j);
}
}
}
}
}
}
int res = 0;
for(int i = 0; i < m; i++) {
for(int j = 0; j < n; j++) {
if(grid[i][j] == 1 && uf.find(i*n+j) != uf.find(m*n)) {
res++;
System.out.println(i + " " + j);
}
}
}
return res;
}
}