我想返回重复的字符及其发生的次数,但是我的输出与我期望的输出不一致。
它应输出e
6倍,应为4倍,应输出j
1倍,应为2倍。我知道我也会以错误的方式返回它。
我在做什么错,我该如何解决?
public static String solution(String s) {
int i, j, count = 0;
for(i = 0; i < s.length(); i++) {
for(j = i + 1; j < s.length(); j++) {
if(s.charAt(i) == s.charAt(j)) {
System.out.print(s.charAt(i) + " ");
count++;
}
}
}
System.out.println();
System.out.println("no duplicates");
System.out.println("There are " + count + " repetitions");
return s;
}
public static void main(String args[]) {
String s = "eeejiofewnj";
solution(s);
}
输出:
e e e e e e j
no duplicates
There are 7 repititions
答案 0 :(得分:2)
因此,您做错的是对字符串中的每个字母进行计数,然后再匹配该字符串中的其他几个字母。
因此对于第一个e,您的循环找到3个匹配项,对于第二个e,您的循环找到2个匹配项,依此类推,然后将所有这些相加。
您想要做的是计算一个字符串中有多少个char实例,然后仅显示高于1的那些实例。我的操作方式是使用地图...如下所示:
public static String solution(String s) {
Map<Character, Integer> counts = new HashMap<Character, Integer>();
// Go through each char and make a map of char to their counts.
for (char c : s.toCharArray()) {
// See if the char is already in the map
Integer count = counts.get(c);
// if it wasn't then start counting from 1
if (count == null) {
count = 0;
}
count++;
// update the count
counts.put(c, count);
}
// now go through the map and print out any chars if their counts are higher than 1 (meaning there's a duplicate)
for (Entry<Character, Integer> entry : counts.entrySet()) {
if (entry.getValue() > 1) {
System.out.println(MessageFormat.format("there are {0} {1}s",
entry.getValue(), entry.getKey()));
}
}
return s;
}
public static void main(String args[]) {
String s = "eeejiofewnj";
solution(s);
}
答案 1 :(得分:1)
带有正则表达式的另一种选择(更详细地讨论here)。
public static void solutioniseThis(final String str)
{
Matcher repeatedMatcher = Pattern.compile("(\\w)\\1+").matcher(str);
while (repeatedMatcher.find())
{
int count = 0;
Matcher countMatcher = Pattern.compile(Matcher.quoteReplacement(repeatedMatcher.group(1))).matcher(str);
while (countMatcher.find())
{
count++;
}
System.out.println(MessageFormat.format("Repeated Character \"{0}\" - found {2} repetitions, {1} sequentially", repeatedMatcher.group(1),
repeatedMatcher.group(0).length(), count));
}
}
public static void main(String args[])
{
solutioniseThis("eeejiofewnj");
}
产生以下输出:
Repeated Character "e" - found 4 repetitions, 3 sequentially
答案 2 :(得分:0)
您正在计算每个匹配的组合。对于e(伪代码):
CharAt(0) == CharAt(1)
CharAt(0) == CharAt(2)
CharAt(0) == CharAt(7)
CharAt(1) == CharAt(2)
CharAt(1) == CharAt(7)
CharAt(2) == CharAt(7)
对于j,只有一个:
CharAt(3) == CharAt(10)
答案 3 :(得分:0)
另一种使用递归的解决方案。
public Map<Character, Integer> countRecursive(final String s)
{
final Map<Character, Integer> counts = new HashMap<Character, Integer>();
if(!s.isEmpty())
{
counts.putAll(countRecursive(s.substring(1)));
final char c = s.charAt(0);
if(counts.containsKey(c))
{
counts.put(c, counts.get(c) + 1);
}
else
{
counts.put(c, 1);
}
}
return counts;
}
public static void main(String args[])
{
final String s = "eeejiofewnj";
final Map<Character, Integer> counts = new CountCharacters().countRecursive(s);
for(Map.Entry<Character, Integer> count : counts.entrySet())
{
if (count.getValue() > 1)
{
System.out.println(MessageFormat.format("There are {0} {1}s",
count.getValue(), count.getKey()));
}
}
}
答案 4 :(得分:0)
您好,这个简单的代码也可以工作:
public static void solution(String s) {
int[] repetitons = new int[128];
for (int i=0; i<s.length(); i++){
repetitons[(int)s.charAt(i)]++;
}
int count = 0;
for (int i=0; i<128; i++){
if (repetitons[i]>1){
count+=repetitons[i];
for (int j=0; j<repetitons[i]; j++){
System.out.print((char)i+" ");
}
}
}
System.out.println();
if (count == 0){
System.out.println("no duplicates");
} else {
System.out.println("There are " + count + " repetitions");
}
}
public static void main(String args[]) {
solution("eeejiofewnj");
}
答案 5 :(得分:0)
Java 8和Apache Utils的另一种选择。
final String s = "eeejiofewnj";
new HashSet<>(s.chars().mapToObj(e->(char)e).collect(Collectors.toList())).stream().map(c -> Pair.of(c, StringUtils.countOccurrencesOf(s, "" + "" + c))).filter(count -> count.getRight() > 0).forEach(count -> System.out.println("There are " + count.getRight() + " repetitions of " + count.getLeft()));