当前位置:网站首页>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
hashTable to record the mastered characters- Traverse each letter of each word , use
flagTo identify the current word- Traverse all characters of the current word , Once a character is not in the mastered character ,
flagbyfalse, Exit the current loop- The next word needs to be restored
hashsurface , 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
lGreater 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
边栏推荐
- 五月刷题02——字符串
- 【深度學習】語義分割-源代碼匯總
- Activiti7工作流的使用
- Design and implementation of online snack sales system based on b/s (attached: source code paper SQL file)
- Kratos ares microservice framework (I)
- Blue Bridge Cup_ Single chip microcomputer_ Measure the frequency of 555
- Master slave replication of redis
- Cap theory
- Reids之缓存预热、雪崩、穿透
- Seven layer network architecture
猜你喜欢

What are the models of data modeling

In order to get an offer, "I believe that hard work will make great achievements

Heap (priority queue) topic

CAP理论

Full stack development of quartz distributed timed task scheduling cluster

Cap theory

基于WEB的网上购物系统的设计与实现(附:源码 论文 sql文件)

IDS' deletion policy

Take you back to spark ecosystem!

Redis' bitmap
随机推荐
Five layer network architecture
Connexion d'initialisation pour go redis
Basic concepts of libuv
DCDC power ripple test
Vs All comments and uncomments
Design and implementation of online snack sales system based on b/s (attached: source code paper SQL file)
Redis之主从复制
小白带你重游Spark生态圈!
[deep learning] semantic segmentation - source code summary
Global and Chinese market of appointment reminder software 2022-2028: Research Report on technology, participants, trends, market size and share
Workflow - activiti7 environment setup
Global and Chinese market of bank smart cards 2022-2028: Research Report on technology, participants, trends, market size and share
Global and Chinese markets of SERS substrates 2022-2028: Research Report on technology, participants, trends, market size and share
基于B/S的影视创作论坛的设计与实现(附:源码 论文 sql文件 项目部署教程)
数据建模有哪些模型
Solve the problem of inconsistency between database field name and entity class attribute name (resultmap result set mapping)
Kratos ares microservice framework (I)
MapReduce instance (IV): natural sorting
Solve the problem of too many small files
【深度学习】语义分割:论文阅读:(2021-12)Mask2Former