当前位置:网站首页>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
边栏推荐
- Kratos战神微服务框架(一)
- Redis' bitmap
- 基于B/S的影视创作论坛的设计与实现(附:源码 论文 sql文件 项目部署教程)
- Segmentation sémantique de l'apprentissage profond - résumé du code source
- IDS cache preheating, avalanche, penetration
- DCDC power ripple test
- Five layer network architecture
- The five basic data structures of redis are in-depth and application scenarios
- What are the models of data modeling
- Redis之Lua脚本
猜你喜欢
Multivariate cluster analysis
Design and implementation of film and television creation forum based on b/s (attached: source code paper SQL file project deployment tutorial)
MapReduce instance (VIII): Map end join
Chapter 1 :Application of Artificial intelligence in Drug Design:Opportunity and Challenges
QML control type: Popup
Kratos战神微服务框架(二)
英雄联盟轮播图自动轮播
The five basic data structures of redis are in-depth and application scenarios
Publish and subscribe to redis
QDialog
随机推荐
Design and implementation of online snack sales system based on b/s (attached: source code paper SQL file)
Oom happened. Do you know the reason and how to solve it?
Servlet learning diary 8 - servlet life cycle and thread safety
小白带你重游Spark生态圈!
Redis之连接redis服务命令
How to intercept the string correctly (for example, intercepting the stock in operation by applying the error information)
Multivariate cluster analysis
Connexion d'initialisation pour go redis
Reids之删除策略
[Yu Yue education] reference materials of power electronics technology of Jiangxi University of science and technology
IDS cache preheating, avalanche, penetration
Redis之cluster集群
MapReduce instance (VII): single table join
Leetcode problem solving 2.1.1
五月刷题27——图
Mapreduce实例(十):ChainMapReduce
Lua script of redis
What is an R-value reference and what is the difference between it and an l-value?
Mapreduce实例(六):倒排索引
Go redis initialization connection