当前位置:网站首页>Related programming problems of string
Related programming problems of string
2022-07-07 01:45:00 【Hu Yangyang y】
Number of string occurrences
character string A, Another string B, Calculation B The string is in A Several times in the string
Example : dsabdnabdsnabeabiwpabekabd **ab**
- Implementation process
- Index the string to find indexOf
- Index record of the found string , Intercept the string
- Until you find the unknown , indexOf The method is -1
- Once you find , Counter ++
/**
* @param str Original string
* @param sub String to find
* @return Number of occurrences
*/
public static int stringCount(String str ,String sub){
// Defining variables , Counter
int count = 0;
// Defining variables , Record the index after string search
int index = 0;
// For the position where the string appears , The query
// Search over and over again , Using a loop while
// The cyclic condition is indexOf Method returns -1
while ( (index=str.indexOf(sub)) != -1 ) {
// Cycle executed index !=-1 The string appears
count ++;
// Intercepting string , Start index index+ The length of the searched string
str = str.substring(index + sub.length());
}
return count;
}
Which character appears most
requirement : The specified string can be ( A lowercase letter ) Letter abeegewff , Calculate which character appears the most
The letters in the qualified string can only have 26 individual
> Find out how many times each character appears , Find the maximum
- Implementation process :
- String to array ( Single character operation )
- Create a length of 26 Array of , The counter uses
- Take out the characters in the array , ( character -97) The index of the corresponding array , Counter ++
- Find the maximum value in the array
/**
* Look up the string , Which character appears the most
* @param str To find a string
* @return Returns the character with the most occurrences
*/
public static char charCount(String str){
// String to array
char[] chars = str.toCharArray();
// Definition 26 An array of lengths , Save the number of occurrences of each character
int[] count = new int[26];
// Traversal array
for (int i = 0 ; i < chars.length; i++){
// Take out a single character
char ch = chars[i];
// character - 97 Use as an index of an array ( Array , Counter array )
count[ ch - 97 ] ++;
}
//System.out.println("Arrays.toString(count) = " + Arrays.toString(count));
// Take out count Array , Index of maximum value
int index = 0 ; // Array maximum index
int max = count[0];
for(int i = 1 ; i < count.length ; i++){
if (max < count[i]){
index = i;
max = count[i];
}
}
//index Indexes , Exactly different from the character 97
return (char) (index+97);
}
边栏推荐
- 各种语言,软件,系统的国内镜像,收藏这一个仓库就够了: Thanks-Mirror
- Long press the button to execute the function
- WCF Foundation
- 454-百度面经1
- DS-5/RVDS4.0变量初始化错误
- Add PDF Title floating window
- Comparison of picture beds of free white whoring
- Appium foundation - appium inspector positioning tool (I)
- Machine learning: the difference between random gradient descent (SGD) and gradient descent (GD) and code implementation.
- ZOJ problem set – 2563 long dominoes [e.g. pressure DP]
猜你喜欢
AI 从代码中自动生成注释文档
【C语言进阶篇】指针的8道笔试题
Reptile practice (VI): novel of climbing pen interesting Pavilion
405 method not allowed appears when the third party jumps to the website
从底层结构开始学习FPGA----FIFO IP的定制与测试
修改px4飞控的系统时间
JS how to quickly create an array with length n
Set WordPress pseudo static connection (no pagoda)
Transplant DAC chip mcp4725 to nuc980
新工作感悟~辞旧迎新~
随机推荐
C语言实例_4
New job insights ~ leave the old and welcome the new~
Mongodb checks whether the table is imported successfully
新工作感悟~辞旧迎新~
WCF基金会
AcWing 346. 走廊泼水节 题解(推公式、最小生成树)
Can't you understand the code of linked list in C language? An article allows you to grasp the secondary pointer and deeply understand the various forms of parameter passing in the function parameter
Gin introduction practice
uva 1401 dp+Trie
Match VIM from zero (0) -- Introduction to vimscript
C language instance_ four
454 Baidu Mianjing 1
The difference between Tansig and logsig. Why does BP like to use Tansig
What does front-end processor mean? What is the main function? What is the difference with fortress machine?
Telnet,SSH1,SSH2,Telnet/SSL,Rlogin,Serial,TAPI,RAW
JS reverse -- ob confusion and accelerated music that poked the [hornet's nest]
数据手册中的词汇
ZOJ problem set – 2563 long dominoes [e.g. pressure DP]
交叉验证如何防止过拟合
golang 基础 —— 数据类型