当前位置:网站首页>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);
}
边栏推荐
猜你喜欢

百度飞将BMN时序动作定位框架 | 数据准备与训练指南 (上)

对C语言数组的再认识

Mongodb checks whether the table is imported successfully
![[advanced C language] 8 written questions of pointer](/img/d4/c9bb2c8c9fd8f54a36e463e3eb2fe0.png)
[advanced C language] 8 written questions of pointer

AcWing 1148. 秘密的牛奶运输 题解(最小生成树)

新工作感悟~辞旧迎新~

LeetCode:1175. Prime permutation

Dark horse notes - exception handling

AcWing 361. Sightseeing cow problem solution (SPFA seeking positive ring)

Blue Bridge Cup 2022 13th provincial competition real topic - block painting
随机推荐
Compile command line terminal swift
json学习初体验–第三者jar包实现bean、List、map创json格式
Appium foundation - appium inspector positioning tool (I)
C语言关于链表的代码看不懂?一篇文章让你拿捏二级指针并深入理解函数参数列表中传参的多种形式
Appium自动化测试基础 — uiautomatorviewer定位工具
C language instance_ five
Add PDF Title floating window
C语言实例_3
AcWing 344. 观光之旅题解(floyd求无向图的最小环问题)
Use nodejs to determine which projects are packaged + released
Taro2.* applet configuration sharing wechat circle of friends
Curl command
Right mouse button customization
grep查找进程时,忽略grep进程本身
【唯一】的“万字配图“ | 讲透【链式存储结构】是什么?
uva 1401 dp+Trie
【C语言进阶篇】指针的8道笔试题
AcWing 1142. 繁忙的都市 题解(最小生成树)
1123. 最深叶节点的最近公共祖先
mongodb查看表是否导入成功