当前位置:网站首页>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);
}
边栏推荐
猜你喜欢
鼠标右键 自定义
Appium foundation - appium inspector positioning tool (I)
Your cache folder contains root-owned files, due to a bug in npm ERR! previous versions of npm which
Baidu flying general BMN timing action positioning framework | data preparation and training guide (Part 1)
【信号与系统】
Reptile practice (VI): novel of climbing pen interesting Pavilion
一起看看matlab工具箱内部是如何实现BP神经网络的
对C语言数组的再认识
Gin 入门实战
Make Jar, Not War
随机推荐
C language instance_ four
Telnet,SSH1,SSH2,Telnet/SSL,Rlogin,Serial,TAPI,RAW
C语言实例_3
Appium foundation - appium inspector positioning tool (I)
Baidu flying general BMN timing action positioning framework | data preparation and training guide (Part 1)
Add PDF Title floating window
长按按钮执行函数
Use nodejs to determine which projects are packaged + released
JS ES5也可以创建常量?
Yunna | work order management measures, how to carry out work order management
AcWing 904. 虫洞 题解(spfa求负环)
Blue Bridge Cup 2022 13th provincial competition real topic - block painting
Google发布安全更新,修复Chrome中已被利用的0 day
AcWing 1142. 繁忙的都市 题解(最小生成树)
一文带你走进【内存泄漏】
7.6 simulation summary
拖拽改变顺序
AcWing 1141. LAN problem solving (kruskalkruskal finding the minimum spanning tree)
When grep looks for a process, it ignores the grep process itself
Typical problems of subnet division and super network construction