String
344.Reverse String
Write a function that reverses a string. The input string is given as an array of characters s.
Example 1:
Input: s = ["h","e","l","l","o"]
Output: ["o","l","l","e","h"]
class Solution {
public void reverseString(char[] s) {
int left = 0, right = s.length - 1;
while(left < right) {
char temp = s[left];
s[left] = s[right];
s[right] = temp;
left ++;
right --;
}
}
}
541. Reverse String II
Given a string s and an integer k, reverse the first k characters for every 2k characters counting from the start of the string. If there are fewer than k characters left, reverse all of them. If there are less than 2k but greater than or equal to k characters, then reverse the first k characters and leave the other as original.
Example 1:
Input: s = "abcdefg", k = 2
Output: "bacdfeg"
class Solution {
public String reverseStr(String s, int k) {
char[] arr = s.toCharArray();
for(int i = 0; i < arr.length; i += 2*k) { // 每2K个,反转前K个,所以步长是2K
int start = i; // 开始位置
int end = i + k - 1; // 这里求得反转的末位置
// 判断边界,如果末位置超过了数组最大长度,最后一个元素就是数组最后一个元素
if(end > arr.length - 1) end = arr.length - 1;
reverseArray(arr,start,end);
}
return new String(arr);
}
private void reverseArray(char[] arr, int start, int end) {
while(start < end) {
char temp = arr[start];
arr[start] = arr[end];
arr[end] = temp;
start ++;
end --;
}
}
}
1768. Merge Strings Alternately
You are given two strings word1 and word2. Merge the strings by adding letters in alternating order, starting with word1. If a string is longer than the other, append the additional letters onto the end of the merged string.
Example 1:
Input: word1 = "abc", word2 = "pqr"
Output: "apbqcr"
Explanation: The merged string will be merged as so:
word1: a b c
word2: p q r
merged: a p b q c r
class Solution {
public String mergeAlternately(String word1, String word2) {
StringBuilder sb = new StringBuilder();
int len1 = word1.length();
int len2 = word2.length();
int index1 = 0;
int index2 = 0;
while(index1 < len1 || index2 < len2) { // 如果串1和串2有一个不为空,就继续
if(index1 < len1) sb.append(word1.charAt(index1)); // 但是这里就要判断是否越界
if(index2 < len2) sb.append(word2.charAt(index2));
index1++;
index2++;
}
return sb.toString();
}
}
2299. Strong Password Checker II
A password is said to be strong if it satisfies all the following criteria(准则):
It has at least 8 characters. It contains at least one lowercase letter. It contains at least one uppercase letter. It contains at least one digit. It contains at least one special character. The special characters are the characters in the following string: "!@#$%^&*()-+". It does not contain 2 of the same character in adjacent(相邻) positions (i.e., "aab" violates(违反) this condition, but "aba" does not). Given a string password, return true if it is a strong password. Otherwise, return false. Example 1:
Input: password = "IloveLe3tcode!"
Output: true
Explanation: The password meets all the requirements. Therefore, we return true.
Example 2:
Input: password = "Me+You--IsMyDream"
Output: false
Explanation: The password does not contain a digit and also contains 2 of the same character in adjacent positions. Therefore, we return false.
Example 3:
Input: password = "1aB!"
Output: false
Explanation: The password does not meet the length requirement. Therefore, we return false.
class Solution {
public boolean strongPasswordCheckerII(String password) {
char[] keys = "!@#$%^&*()-+".toCharArray();
Set<Character> set = new HashSet<>(keys.length);
for(char key : keys) {
set.add(key);
}
int len = password.length();
if(len < 8) return false;
boolean containsLowerChar = false;
boolean containsUpperChar = false;
boolean containsDigit = false;
boolean containsSymbol = false;
char[] arr = password.toCharArray();
for(int i = 0; i < arr.length; i++) {
char c = arr[i];
if(i + 1 < arr.length && arr[i] == arr[i + 1]) return false;
if(Character.isLowerCase(c)) {
containsLowerChar = true;
}else if(Character.isUpperCase(c)) {
containsUpperChar = true;
}else if(Character.isDigit(c)) {
containsDigit = true;
}else if(set.contains(c)) {
containsSymbol = true;
}
}
return containsLowerChar && containsUpperChar && containsDigit && containsSymbol;
}
}
2710. Remove Trailing Zeros From a String
Given a positive integer num represented as a string, return the integer num without trailing zeros as a string.
Example 1:
Input: num = "51230100"
Output: "512301"
Explanation: Integer "51230100" has 2 trailing zeros, we remove them and return integer "512301".
Example 2:
Input: num = "123"
Output: "123"
Explanation: Integer "123" has no trailing zeros, we return integer "123".
class Solution {
public String removeTrailingZeros(String num) {
char[] chars = num.toCharArray();
int startZeroIndex = -1;
for(int i = chars.length - 1; i >= 0; i--) {
if(chars[i] != '0') break;
startZeroIndex = i;
}
if(startZeroIndex == -1) return num;
return num.substring(0, startZeroIndex);
}
}