当前位置:网站首页>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);
}
边栏推荐
- AcWing 345. Cattle station solution (nature and multiplication of Floyd)
- 字符串的相关编程题
- Ds-5/rvds4.0 variable initialization error
- AcWing 344. 观光之旅题解(floyd求无向图的最小环问题)
- 交叉验证如何防止过拟合
- Baidu flying general BMN timing action positioning framework | data preparation and training guide (Part 1)
- Box stretch and pull (left-right mode)
- AcWing 346. Solution to the problem of water splashing festival in the corridor (deduction formula, minimum spanning tree)
- hdu 4661 Message Passing(木DP&amp;组合数学)
- 移植DAC芯片MCP4725驱动到NUC980
猜你喜欢
The difference between Tansig and logsig. Why does BP like to use Tansig
According to the analysis of the Internet industry in 2022, how to choose a suitable position?
Dark horse notes - create immutable sets and streams
Mongodb checks whether the table is imported successfully
AcWing 361. Sightseeing cow problem solution (SPFA seeking positive ring)
子网划分、构造超网 典型题
Appium automation test foundation uiautomatorviewer positioning tool
Today's question -2022/7/4 modify string reference type variables in lambda body
黑马笔记---异常处理
Clickhouse fields are grouped and aggregated, and SQL is queried according to the granularity of any time period
随机推荐
ZOJ problem set – 2563 long dominoes [e.g. pressure DP]
Telnet,SSH1,SSH2,Telnet/SSL,Rlogin,Serial,TAPI,RAW
C language instance_ two
[signal and system]
JVM 内存模型
1123. 最深叶节点的最近公共祖先
字符串的相关编程题
黑马笔记---异常处理
The difference between Tansig and logsig. Why does BP like to use Tansig
Match VIM from zero (0) -- Introduction to vimscript
AcWing 346. 走廊泼水节 题解(推公式、最小生成树)
机器学习:随机梯度下降(SGD)与梯度下降(GD)的区别与代码实现。
Dark horse notes - exception handling
初识MySQL
使用nodejs完成判断哪些项目打包+发版
拖拽改变顺序
Yunna - work order management system and process, work order management specification
THREE. AxesHelper is not a constructor
图片打水印 缩放 和一个输入流的转换
Box stretch and pull (left-right mode)