当前位置:网站首页>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
边栏推荐
- Redis之连接redis服务命令
- Global and Chinese market of electronic tubes 2022-2028: Research Report on technology, participants, trends, market size and share
- 美团二面:为什么 Redis 会有哨兵?
- Mapreduce实例(九):Reduce端join
- Redis分布式锁实现Redisson 15问
- 018. Valid palindromes
- Redis connection redis service command
- Global and Chinese markets of SERS substrates 2022-2028: Research Report on technology, participants, trends, market size and share
- 一文读懂,DDD落地数据库设计实战
- MapReduce instance (x): chainmapreduce
猜你喜欢
发生OOM了,你知道是什么原因吗,又该怎么解决呢?
Redis' performance indicators and monitoring methods
【深度学习】语义分割-源代码汇总
Redis之主从复制
Master slave replication of redis
Redis之核心配置
为拿 Offer,“闭关修炼,相信努力必成大器
Redis分布式锁实现Redisson 15问
leetcode-14. Longest common prefix JS longitudinal scanning method
How to intercept the string correctly (for example, intercepting the stock in operation by applying the error information)
随机推荐
Scoped in webrtc_ refptr
The carousel component of ant design calls prev and next methods in TS (typescript) environment
Mysql database recovery (using mysqlbinlog command)
Mapreduce实例(七):单表join
What is an R-value reference and what is the difference between it and an l-value?
[daily question] Porter (DFS / DP)
Solve the problem of inconsistency between database field name and entity class attribute name (resultmap result set mapping)
英雄联盟轮播图手动轮播
MapReduce工作机制
Basic usage of xargs command
Solve the problem of too many small files
Detailed explanation of cookies and sessions
基于B/S的网上零食销售系统的设计与实现(附:源码 论文 Sql文件)
Lua script of redis
Compilation of libwebsocket
工作流—activiti7环境搭建
Cap theory
Redis之性能指标、监控方式
Design and implementation of online shopping system based on Web (attached: source code paper SQL file)
Design and implementation of film and television creation forum based on b/s (attached: source code paper SQL file project deployment tutorial)