当前位置:网站首页>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);
}
边栏推荐
- Typical problems of subnet division and super network construction
- 糊涂工具类(hutool)post请求设置body参数为json数据
- AI automatically generates annotation documents from code
- mysqlbackup 还原特定的表
- Yunna - work order management system and process, work order management specification
- AcWing 904. Wormhole solution (SPFA for negative rings)
- [signal and system]
- 新工作感悟~辞旧迎新~
- 爬虫实战(六):爬笔趣阁小说
- 7.6 simulation summary
猜你喜欢
刨析《C语言》【进阶】付费知识【完结】
黑马笔记---异常处理
Instructions for using the domain analysis tool bloodhound
Transplant DAC chip mcp4725 to nuc980
1123. The nearest common ancestor of the deepest leaf node
The difference between Tansig and logsig. Why does BP like to use Tansig
LeetCode:1175. Prime permutation
移植DAC芯片MCP4725驱动到NUC980
Right mouse button customization
JS reverse -- ob confusion and accelerated music that poked the [hornet's nest]
随机推荐
json学习初体验–第三者jar包实现bean、List、map创json格式
AcWing 1142. Busy urban problem solving (minimum spanning tree)
[advanced C language] 8 written questions of pointer
Mysqlbackup restores specific tables
uva 1401 dp+Trie
Your cache folder contains root-owned files, due to a bug in npm ERR! previous versions of npm which
使用nodejs完成判断哪些项目打包+发版
C language instance_ five
AcWing 1141. 局域网 题解(kruskalkruskal 求最小生成树)
THREE. AxesHelper is not a constructor
Gin introduction practice
7.6 simulation summary
HDU 4661 message passing (wood DP & amp; Combinatorics)
Compile command line terminal swift
拖拽改变顺序
安利一波C2工具
JS es5 peut également créer des constantes?
【唯一】的“万字配图“ | 讲透【链式存储结构】是什么?
数据手册中的词汇
增加 pdf 标题浮窗