在java中如何获得字符串中重复字符的个数 比如:输入 String str="abc ace cet"; 输出 String str="abcet" 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 好像题目和题意不符。怎么获得重复的个数,====遍历 +Map 提供一个算法:遍历字符串查找出重复的字符 然后替换除第一个之外的其他重复字符为空最后把空格全部替换掉--->str.replaceAll(" ", "") 内容写错了,就是String str="abc acf bfe",输出的是重复字符的个数。 public static void counts(String str) { int[] nums = new int[26]; for(char i : str.toCharArray()) { if (i < 97 || i > 123) continue; nums[i - 97] = nums[i - 97] + 1; } for (int i = 0; i < nums.length; i++) { System.out.println((char)(97+i) + ":" + nums[i]); } } //test: String str="abc ace cet"; counts(str); //result: a:2b:1c:3d:0e:2f:0g:0h:0i:0j:0k:0l:0m:0n:0o:0p:0q:0r:0s:0t:1u:0v:0w:0x:0y:0z:1a:2b:1c:3d:0e:2f:0g:0h:0i:0j:0k:0l:0m:0n:0o:0p:0q:0r:0s:0t:1u:0v:0w:0x:0y:0z:1 超级玛丽的相关问题 我面试时遇到的一道题: 请问jmf入门有哪些书值得推荐呢 有哪个知道多线程组的 新手遇到的问题2! 帮我看看Vector的操作错误。。 HQL 语句模糊查询问题 ,参数怎么写 请教各位老大!! 急!急!!!高分求助jdk1.2.2的中文问题的解答! 求看一个代码练习题 编译不过 关于java适配器模式 500分求Java正态分布算法
怎么获得重复的个数,====遍历 +Map
遍历字符串查找出重复的字符 然后替换除第一个之外的其他重复字符为空
最后把空格全部替换掉--->str.replaceAll(" ", "")
int[] nums = new int[26];
for(char i : str.toCharArray()) {
if (i < 97 || i > 123) continue;
nums[i - 97] = nums[i - 97] + 1;
} for (int i = 0; i < nums.length; i++) {
System.out.println((char)(97+i) + ":" + nums[i]);
}
}
//test:
String str="abc ace cet";
counts(str);
//result:
a:2
b:1
c:3
d:0
e:2
f:0
g:0
h:0
i:0
j:0
k:0
l:0
m:0
n:0
o:0
p:0
q:0
r:0
s:0
t:1
u:0
v:0
w:0
x:0
y:0
z:1
a:2
b:1
c:3
d:0
e:2
f:0
g:0
h:0
i:0
j:0
k:0
l:0
m:0
n:0
o:0
p:0
q:0
r:0
s:0
t:1
u:0
v:0
w:0
x:0
y:0
z:1