查找字符串中相同的最大字符和个数
来源:互联网 发布:天津商业大学网络登录 编辑:程序博客网 时间:2024/05/16 08:48
/**
* 查找字符串中相同最多的字符个数和字符
* @param str
*/
public static void findMaxCountChars(String str) {
long time = System.currentTimeMillis();
if (str == null || str.length() == 0) {
return;
}
int length = str.length();
List<Character> charList = new ArrayList<Character>(length);
for (int i = 0; i < length; i++) {
charList.add(str.charAt(i));
}
Collections.sort(charList);
int max = 0;
char ch = charList.get(0);
StringBuffer sb = new StringBuffer(10);
sb.append(ch);
int index = 0;
int listSize = charList.size();
while (index < listSize && ch == charList.get(index++))
max++;
int pmax = 0;
while (index < listSize) {
index--;
ch = charList.get(index);
while (index < listSize && ch == charList.get(index++))
pmax++;
if (pmax > max) {
max = pmax;
sb.delete(0, sb.length());
sb.append(ch);
} else if (pmax == max) {
sb.append(ch);
}
pmax = 0;
}
if (max == 1) {
sb.append(charList.get(index - 1));
}
System.out.println("max char:" + sb.toString() + ",count:" + max
+ ",time:" + (System.currentTimeMillis() - time));
}
* 查找字符串中相同最多的字符个数和字符
* @param str
*/
public static void findMaxCountChars(String str) {
long time = System.currentTimeMillis();
if (str == null || str.length() == 0) {
return;
}
int length = str.length();
List<Character> charList = new ArrayList<Character>(length);
for (int i = 0; i < length; i++) {
charList.add(str.charAt(i));
}
Collections.sort(charList);
int max = 0;
char ch = charList.get(0);
StringBuffer sb = new StringBuffer(10);
sb.append(ch);
int index = 0;
int listSize = charList.size();
while (index < listSize && ch == charList.get(index++))
max++;
int pmax = 0;
while (index < listSize) {
index--;
ch = charList.get(index);
while (index < listSize && ch == charList.get(index++))
pmax++;
if (pmax > max) {
max = pmax;
sb.delete(0, sb.length());
sb.append(ch);
} else if (pmax == max) {
sb.append(ch);
}
pmax = 0;
}
if (max == 1) {
sb.append(charList.get(index - 1));
}
System.out.println("max char:" + sb.toString() + ",count:" + max
+ ",time:" + (System.currentTimeMillis() - time));
}
0 0
- 查找字符串中相同的最大字符和个数
- 统计字符串中相同字符的个数
- JS查找字符串中出现最多的字符和个数
- JavaScript-查找字符串中出现最多的字符和个数
- 查找两个字符传中最大的相同字符串
- 递归算法查找字符串中相同字符连续出现的最大次数
- 字符串匹配算法实现(两个字符串中含有的字符和个数都相同)
- 查找两个字符串中最大的相同子字符串长度
- 查找两个字符串中最大相同的字串
- 查找字符串中,相同字符出现的次数!
- 查找字符串中是否包含相同字符
- c# 如何判断字符串中相同字符串的个数 (相同字符在字符串中出现的次数)
- 假设两个字符串中所含有的字符和个数都相同我们就叫这两个字符串匹配, 比如:abcda和adabc,由于出现的字符个数都是相同,只是顺序不同, 所以这两个字符串是匹配的。要求高效!
- 查找一个字符串中最长的字符及个数
- 查找字符串中每个字符出现的个数
- 查找字符串中出现最多次数的字符及其个数
- 查找字符串(sdjksfssscfssdd )中出现最多的字符(s)和个数(7)
- 将字符串中连续相同的字符表达成字符加个数的形式输出
- 关于IHttpModule的相关知识
- android 导出签名APK--混淆文件proguard.cfg详解
- Ubuntu下的Cocos2dx 3.2环境搭建
- 初识数据库
- 高并发数据库分表分库
- 查找字符串中相同的最大字符和个数
- 执行力与正能量
- 老出现这样问题现在做个记录:android.os.NetworkOnMainThreadException
- 库及表的crud
- 利用GoAhead构建嵌入式web应用
- Forward: Verification and validation (software)
- 线程死锁
- htm 添加asp , goform for goahead
- PAT 1040. Longest Symmetric String