当前位置:网站首页>June brush question 02 - string
June brush question 02 - string
2022-07-06 09:38:00 【A Guang chasing dreams】
Brush questions in June 02—— character string
Today's brush topic content : character string
Preface
- 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
- Try to work out at least one question every day
- Language java、python、c\c++
One 、 Today's topic
- 2278. Percentage of letters in string ||
- 551. Student attendance records I||
- 2255. Statistics is the number of strings for a given string prefix ||
- 1071. The greatest common factor of a string ||
Two 、 Their thinking
1. 2278. Percentage of letters in string
- The number of times the letter appears is obtained by one iteration
- Use the number of times
*100Divide by string lengthnthat will do
class Solution {
public int percentageLetter(String s, char letter) {
int[] hash = new int[256];
int n = s.length();
for (char c: s.toCharArray()){
hash[c]++;
}
return (int)(hash[letter] * 100 / n);
}
}
2. 551. Student attendance records I
- The sliding window , With left and right pointers
- The right pointer is used to mark the encounter
LThe length of , The left pointer increases automatically- If the characters
AThe number of times is less than 2 alsoLIt is true if it does not appear three times in a row
class Solution {
public boolean checkRecord(String s) {
int[] hash = new int[256];
boolean flag = true;
int count = 0;
int l = 0, r = -1, n = s.length();
char[] arr = s.toCharArray();
for (char c: arr){
hash[c]++;
}
while(l < n){
r = l;
count = 0;
while (r < n && arr[r] == 'L'){
count++;
r++;
if (count == 3) {
flag = false;
break;
}
}
l++;
}
if(hash['A'] < 2 && flag){
return true;
}
return false;
}
}
3. 2255. Statistics is the number of strings for a given string prefix
- General simulation , Traverse each word , Find the words that meet the conditions
- Count the times
class Solution {
public int countPrefixes(String[] words, String s) {
char[] arr = s.toCharArray();
int i, count = 0;
boolean flag;
for (String word: words){
i = 0;
flag = true;
for (char c: word.toCharArray()){
if (i < arr.length && arr[i] == c) i++;
else{
flag = false;
break;
}
}
if (flag) count++;
}
return count;
}
}
4. 1071. The greatest common factor of a string
- When I did it, I only thought of violence
There are many solutions to this problem Answer key
class Solution {
public String gcdOfStrings(String str1, String str2) {
if (str1.length() > str2.length()){
return gcdOfStrings(str2, str1); // Make the first string shorter
}
StringBuffer sb = new StringBuffer();
String ret;
int i;
if (str1.length() == 0) return "";
if (str2.contains(str1)){
char[] arr1 = str1.toCharArray();
char[] arr2 = str2.toCharArray();
for (i = 0; i < arr1.length; i++){
if (arr1[i] == arr2[i]){
sb.append(arr1[i]);
}
else{
break;
}
}
int len = sb.length();
if (len == 0) return "";
for (i = len; i < arr2.length; i++){
if (arr2[i] != sb.charAt(i % len)){
return "";
}
}
ret = sb.toString();
while (arr2.length % ret.length() != 0 || arr1.length % ret.length() != 0){
ret = sb.substring(0, len-1);
len -= 1;
}
return ret;
}
return "";
}
}
边栏推荐
- leetcode-14. Longest common prefix JS longitudinal scanning method
- Solve the problem of inconsistency between database field name and entity class attribute name (resultmap result set mapping)
- Workflow - activiti7 environment setup
- Blue Bridge Cup_ Single chip microcomputer_ PWM output
- Seven layer network architecture
- [deep learning] semantic segmentation: paper reading: (2021-12) mask2former
- Withdrawal of wechat applet (enterprise payment to change)
- 基于WEB的网上购物系统的设计与实现(附:源码 论文 sql文件)
- Design and implementation of online shopping system based on Web (attached: source code paper SQL file)
- Hard core! One configuration center for 8 classes!
猜你喜欢

Redis' bitmap

Oom happened. Do you know the reason and how to solve it?
![[shell script] - archive file script](/img/50/1bef6576902890dfd5771500414876.png)
[shell script] - archive file script

Redis core configuration

英雄联盟轮播图自动轮播

Reids之缓存预热、雪崩、穿透

Nacos installation and service registration

MapReduce instance (VII): single table join
![[three storage methods of graph] just use adjacency matrix to go out](/img/79/337ee452d12ad477e6b7cb6b359027.png)
[three storage methods of graph] just use adjacency matrix to go out

Redis分布式锁实现Redisson 15问
随机推荐
运维,放过监控-也放过自己吧
Blue Bridge Cup_ Single chip microcomputer_ PWM output
Global and Chinese market of electric pruners 2022-2028: Research Report on technology, participants, trends, market size and share
Design and implementation of online snack sales system based on b/s (attached: source code paper SQL file)
Compilation of libwebsocket
MapReduce working mechanism
Design and implementation of film and television creation forum based on b/s (attached: source code paper SQL file project deployment tutorial)
Design and implementation of online shopping system based on Web (attached: source code paper SQL file)
【深度学习】语义分割:论文阅读:(CVPR 2022) MPViT(CNN+Transformer):用于密集预测的多路径视觉Transformer
一文读懂,DDD落地数据库设计实战
[Chongqing Guangdong education] reference materials for nine lectures on the essence of Marxist Philosophy in Wuhan University
Hero League rotation map automatic rotation
发生OOM了,你知道是什么原因吗,又该怎么解决呢?
[shell script] - archive file script
Redis之连接redis服务命令
Libuv thread
Global and Chinese market of capacitive displacement sensors 2022-2028: Research Report on technology, participants, trends, market size and share
Why data Tiering
Sqlmap installation tutorial and problem explanation under Windows Environment -- "sqlmap installation | CSDN creation punch in"
MySQL数据库优化的几种方式(笔面试必问)