当前位置:网站首页>May brush question 02 - string
May brush question 02 - string
2022-07-06 09:38:00 【A Guang chasing dreams】
May brush questions 02—— character string
Today's brush topic content : character string
Preface
- An algorithm opens the way to brush the questions of waste materials , Update the problem solution content of the problem brush every day
- Focus on personal understanding , Look at the difficulty and update the number of questions
- The title comes from Li Kou
- New column , Try to work out at least one question every day =v=
- Language java、python、c\c++
One 、 Today's topic
- 500. Keyboard line *****
- 1160. spell the words *****
- 1047. Delete all adjacent duplicates in the string *****
- 1935. The maximum number of words that can be entered *****
Two 、 Their thinking
1. 500. Keyboard line *****
- Use three strings to record the characters of each keyboard line
- Confirm the character line by the first character of each word
- Verify whether the subsequent characters are on the same character line
- If in the same character line , Add the result to the result list
class Solution:
def findWords(self, words: List[str]) -> List[str]:
str1 = "qwertyuiopQWERTYUIOP"
str2 = "asdfghjklASDFGHJKLK"
str3 = "zxcvbnmZXCVBNM"
ret = []
# Confirm which keyboard line comes from by the beginning of each word
for word in words:
flag = True # Identify whether the current word comes from the same keyboard line
if word[0] in str1:
for i in range(1, len(word)):
if word[i] not in str1:
flag = False
break
elif word[0] in str2:
for i in range(1, len(word)):
if word[i] not in str2:
flag = False
break
elif word[0] in str3:
for i in range(1, len(word)):
if word[i] not in str3:
flag = False
break
if flag:
ret.append(word) # If it comes from the same keyboard line
return ret
2. 1160. spell the words *****
- Use one
hash
Table to record the mastered characters- Traverse each letter of each word , use
flag
To identify the current word- Traverse all characters of the current word , Once a character is not in the mastered character ,
flag
byfalse
, Exit the current loop- The next word needs to be restored
hash
surface , Use deep copy
class Solution {
public int countCharacters(String[] words, String chars) {
int[] hash = new int[256];
// Use one hash Table to store learned vocabulary
for(char c: chars.toCharArray()){
hash[c]++;
}
int ret = 0; // Count the number of letters
int[] temp = new int[256];
for (String word: words){
boolean flag = true;
// Restore the original hash surface
System.arraycopy(hash, 0, temp, 0, hash.length);
for (char w: word.toCharArray()){
if (temp[w] <= 0){
flag = false;
break;
} else{
temp[w]--;
}
}
// If every character in the word is in the mastered string
if (flag){
ret += word.length();
}
}
return ret;
}
}
3. 1047. Delete all adjacent duplicates in the string *****
- Define two adjacent pointers
l, r
, Move two pointers by sliding the window- It should be noted that , When the left pointer
l
Greater than 0 when , At this time, the characters of the left and right pointers are the same , After deleting the same character, the left and right pointers should be returned- Otherwise, move the left and right pointers to the right
class Solution {
public String removeDuplicates(String s) {
int l = 0;
int r = 1;
int n = s.length();
StringBuffer sb = new StringBuffer(s); // Initialization result
while(r < sb.length() && l < sb.length()){
if (sb.charAt(l) == sb.charAt(r)){
sb.deleteCharAt(l);
sb.deleteCharAt(l);
// If the left pointer is greater than 0, The left and right pointers should be returned by one digit at the same time
if (l > 0){
l--;
r--;
}
}else{
l++;
r++;
}
}
return sb.toString();
}
}
4. 1935. The maximum number of words that can be entered *****
- Divide the string into word arrays through spaces
- Traverse the characters of each word in the word array
- Once a character is found to belong to a broken keyboard key , Skip the character , Otherwise, the result will be
count+1
class Solution:
def canBeTypedWords(self, text: str, brokenLetters: str) -> int:
words = text.split(" ")
count = 0
for word in words:
flag = True
for char in word:
if char in brokenLetters:
flag = False
break
if flag:
count += 1
return count
边栏推荐
- Opencv+dlib realizes "matching" glasses for Mona Lisa
- leetcode-14. Longest common prefix JS longitudinal scanning method
- 面渣逆袭:Redis连环五十二问,图文详解,这下面试稳了
- MapReduce instance (x): chainmapreduce
- QML type: overlay
- 五月刷题26——并查集
- 【深度学习】语义分割:论文阅读:(2021-12)Mask2Former
- Redis core configuration
- 英雄联盟轮播图手动轮播
- [deep learning] semantic segmentation: paper reading: (2021-12) mask2former
猜你喜欢
Nacos installation and service registration
What are the models of data modeling
Redis之Bitmap
[Yu Yue education] Wuhan University of science and technology securities investment reference
Withdrawal of wechat applet (enterprise payment to change)
Blue Bridge Cup_ Single chip microcomputer_ PWM output
Redis之主从复制
Compilation of libwebsocket
Redis之哨兵模式
Sqlmap installation tutorial and problem explanation under Windows Environment -- "sqlmap installation | CSDN creation punch in"
随机推荐
[deep learning] semantic segmentation: paper reading: (CVPR 2022) mpvit (cnn+transformer): multipath visual transformer for dense prediction
Redis之Geospatial
Leetcode:608 tree node
The order of include header files and the difference between double quotation marks "and angle brackets < >
QML control type: menu
Connexion d'initialisation pour go redis
MapReduce instance (IX): reduce end join
Withdrawal of wechat applet (enterprise payment to change)
YARN组织架构
美团二面:为什么 Redis 会有哨兵?
IDS cache preheating, avalanche, penetration
[daily question] Porter (DFS / DP)
解决小文件处过多
Design and implementation of online shopping system based on Web (attached: source code paper SQL file)
Lua script of redis
Kratos战神微服务框架(二)
[deep learning] semantic segmentation - source code summary
Design and implementation of online snack sales system based on b/s (attached: source code paper SQL file)
Global and Chinese market of capacitive displacement sensors 2022-2028: Research Report on technology, participants, trends, market size and share
Activiti7工作流的使用