如何计算存在但不重复的字母数量

huangapple go评论101阅读模式
英文:

How to count the number of letters that exist without repeated

问题

以下是您要翻译的内容:

我想要一种方法来统计字符串中的字母,例如:

我的字符串:"Hello my friends "

字符串中的字符:{H,e,l,o, ,m,y,f,r,i,n,d,s}

这些字母在字符串中是不重复的(包括空格)。
因此,我想要的结果是:13

所有这些的目的是,我想要将字符串转换为一个不重复字母的字符表。

例如:我的字符串 = "Hello"

我想要得到的表格是{H,e,l,o}

我的尝试:

public static int numberRep(String txt) {
    int count = 0;
    boolean v = false;
    for (int i = 0; i != txt.length(); i++) {
        char c = txt.charAt(i);
        for (int j = i + 1; j != txt.length(); j++) {
            if (txt.charAt(j) == c) {
                count++;
            }
        }
        if (count == txt.length() - 1) {
            v = true;
        }
    }
    if (v) {
        return 1;
    } else {
        return count;
    }
}
英文:

I want a way to count the letters in an string for example:

My string : "Hello my friends "

The characters in the string : {H,e,l,o, ,m,y,f,r,i,n,d,s}

These letters exist without repeating them (with a blank space)
So the result I want is: 13

The goal of all of this is, I want to convert a string to a table of character without repeating the letters

EX: MY String = "Hello"

The table I want to get {H,e,l,o}

My attempt

 public static int numberRep(String txt) {
    int count = 0;
        boolean v = false;
        for (int i = 0; i != txt.length(); i++) {
            char c = txt.charAt(i);
            for (int j = i + 1; j != txt.length(); j++) {
                if (txt.charAt(j) == c) {
                    count++;
                }
            }
            if(count == txt.length()-1){
                v = true;
            }
        }
        if(v){
            return 1 ;
        }
        else{
        return count;
        }
      
    }

答案1

得分: 2

将字符串拆分为字符并将它们存储在一个 Set 中。Set 仅保留唯一元素。Set 的元素将是所需的字符,而 Set 的大小将给出这些字符的计数。

按以下方式操作:

import java.util.Arrays;
import java.util.LinkedHashSet;
import java.util.Set;
import java.util.stream.Collectors;

public class Testing {
    public static void main(String[] args) {
        String myString = "Hello my friends ";
        Set<String> set = Arrays.stream(myString.split("")).collect(Collectors.toCollection(LinkedHashSet::new));
        System.out.println("Unique characters including space: " + set);
        System.out.println("No. of unique characters including space: " + set.size());
    }
}

输出:

Unique characters including space: [H, e, l, o,  , m, y, f, r, i, n, d, s]
No. of unique characters including space: 13
英文:

Split the string into characters and store them into a Set. A Set keeps only unique elements. The elements of the Set will be the required characters and the size of the Set will give you the count of these characters.

Do it as follows:

import java.util.Arrays;
import java.util.LinkedHashSet;
import java.util.Set;
import java.util.stream.Collectors;

public class Testing {
	public static void main(String[] args) {
		String myString = &quot;Hello my friends &quot;;
		Set&lt;String&gt; set = Arrays.stream(myString.split(&quot;&quot;)).collect(Collectors.toCollection(LinkedHashSet::new));
		System.out.println(&quot;Unique characters including space: &quot; + set);
		System.out.println(&quot;No. of unique characters including space: &quot; + set.size());
	}
}

Output:

Unique characters including space: [H, e, l, o,  , m, y, f, r, i, n, d, s]
No. of unique characters including space: 13

答案2

得分: 0

这可能是其中一种实现方法:

String str = "Hello my friends ";
String noRepeatStr = "";

for(int i = 0; i < str.length; i++) {
    if(noRepeatStr.indexOf(str[i]) == -1) // 检查字符是否已存在,如果不存在则返回-1
        noRepeatStr = noRepeatStr + str[i];      // 添加新字符
}

System.out.println("Letters: " + noRepeatStr.toCharArray());
System.out.println("No of letters: " + noRepeatStr.length);
英文:

Well, this could be one of the approach to do so:

String str = &quot;Hello my friends &quot;
String noRepeatStr = &quot;&quot;;

for(int i = 0; i &lt; str.length; i++) {
    if(noRepeatStr.indexOf(str[i]) == -1) // check if a char already exist, if not exist then return -1
        noRepeatStr = noRepeatStr+str[i];      // add new char
}

System.out.println(&quot;Letters: &quot; + noRepeatStr.toCharArray())
System.out.println(&quot;No of letters: &quot; + noRepeatStr.length)

答案3

得分: 0

你可以这样做:

  1. 创建一个空列表 a
  2. 对于列表中的每个字符:
  3. 如果字符不在列表 a 中:将其添加到列表中

这样,你就不会得到重复的字符。

你也可以像 Arvind 的回答中推荐的那样使用集合,但我认为最好你将其写成一个函数作为任务。
一旦掌握了 Java 的概念,集合是更清晰的实现方式。

英文:

What you could do is:

  1. Create an empty list a
  2. for character in list:
  3. If the character is not your list a: add it to your list

This way you won't get any duplicate characters.

You can also use a set like recommended in Arvind's answer, but I think it is better for you to write a function as a task.
Set is the cleaner way to do it once you grasped the concepts of Java.

答案4

得分: 0

 public static void main(String[] args) {
        final String test = "Hello my friends";

        final int size = IntStream.range(0, test.length())
                .mapToObj(test::charAt)
                .collect(Collectors.toSet()).size();

        System.out.println(size);
    }

What you've done here is iterated over all of the characters in the input string, mapped them to a char object, and then collected them into a Set. Instead of keeping the raw Set, you've used .size() in order to get the output that you were expecting.

英文:
 public static void main(String[] args) {
        final String test = &quot;Hello my friends&quot;;

        final int size = IntStream.range(0, test.length())
                .mapToObj(test::charAt)
                .collect(Collectors.toSet()).size();

        System.out.println(size);
    }

What I've done here is iterated over all of the characters in the input string, mapped them to a char object and then collected them into a Set - instead of keeping the raw Set I've used .size() in order to get the output that you were expecting.

huangapple
  • 本文由 发表于 2020年4月7日 21:16:19
  • 转载请务必保留本文链接:https://go.coder-hub.com/61080894.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定