简体   繁体   English

在java中的字符串下打印星号(*)

[英]Print stars(*) under the characters of string in java

I got a java question asked in an interview. 我在接受采访时得到了一个java问题。 Print distinct characters in a string and print stars (*) under each character which shows how many times the character repeated in that string. 在字符串中打印不同的字符,并在每个字符下打印星号(*),显示字符在字符串中重复的次数。

For eg: my string is "GOOGLE", then the output should be 例如:我的字符串是“GOOGLE”,那么输出应该是

G O L E
* * * *
* *

I tried in java and I was able to create a HashMap which will store the Character and Number of repetitions in the string. 我在java中尝试过,我能够创建一个HashMap,它将存储字符串中的字符和重复次数。 But the HashMap is not based on the Insertion order of the string. 但是HashMap不是基于字符串的Insertion顺序。 Also I don't know what should be my next step. 我也不知道下一步应该是什么。 Can someone help me? 有人能帮我吗? Thanks in advance 提前致谢

public void myFunction(String str) {
    int length = str.length();
    HashMap<Character, Integer> hm = new HashMap<>();
    for(int i=0;i<length;i++){
        char ch = str.charAt(i);
        if(hm.containsKey(ch)){
            hm.put(ch, hm.get(ch)+1);               
        }
        else {
            hm.put(ch, 1);
        }


    }
        System.out.println(hm);
}

OUTPUT - Enter a String: 
GOOGLE
{E=1, G=2, L=1, O=2}

If you use a LinkedHashMap it will keep the order of the insertion. 如果使用LinkedHashMap ,它将保持插入的顺序。 You can do something like this. 你可以做这样的事情。 Also add in a max variable since we will need it later when printing. 还要添加一个max变量,因为我们稍后会在打印时需要它。

String input = "GOOGLE";
int max = 0;
LinkedHashMap<Character, Integer> map = new LinkedHashMap<>();
for (char c: input.toCharArray()){
    if (map.containsKey(c)){
        map.put(c, map.get(c) + 1);
    }else{
        map.put(c, 1);
    }
    max = Math.max(max, map.get(c));
}
System.out.println(map);

Output: 输出:

{G=2, O=2, L=1, E=1}

Then just iterate through how many lines you have to print and iterate through each character. 然后只需迭代你需要打印的行数并遍历每个字符。 Something like this should do the trick. 这样的事情应该可以解决问题。

for (int i=0; i<=max; i++){
    for (char c: map.keySet()){
        if (i==0){
            System.out.print(c);
        }else if (i<= map.get(c)){
            System.out.print("*");
        }else{
            System.out.print(" ");
        }
    }
    System.out.println();
}

Output: 输出:

GOLE
****
** 

That's a good start. 这是一个好的开始。

What I would do next is to change the HashMap to a LinkedHashMap so that we can maintain the order of the characters and add a long to know the maximum number of times a character appears. 接下来我要做的是将HashMap更改为LinkedHashMap以便我们可以维护字符的顺序并添加一个long以了解字符出现的最大次数。 Thus I would change your current code to something like: 因此,我会将您当前的代码更改为:

public void myFunction(String str) {
int length = str.length();
long maxOcurrences = 0;
LinkedHashMap<Character, Integer> hm = new LinkedHashMap<>();
for(int i=0;i<length;i++){
    char ch = str.charAt(i);
    long nextValue;
    if(hm.containsKey(ch)){
        nextValue = hm.get(ch)+1
        hm.put(ch, nextValue);               
    }
    else {
        nextValue = 1;
        hm.put(ch, nextValue);
    }

    if(nextValue > maxOcurrences)
    {maxOcurrences = nextValue;}


}
    System.out.println(hm);
}

Next I would print the characters in order by iterating through the LinkedHashMap . 接下来,我将通过迭代LinkedHashMap按顺序打印字符。 Something like: 就像是:

for (Map.Entry<Character, Integer> entry : hm.entrySet()) {
    System.out.print(entry.getKey());
}
System.out.println();

Finally I would create a loop that iterates maxOcurrences times and prints a * if needed. 最后,我将创建一个迭代maxOcurrences次数的循环,并在需要时打印*

for(int i = 0; i < maxOcurrences; i++)
{
    //Iterate over each character again
    for (Map.Entry<Character, Integer> entry : hm.entrySet()) {
        if(entry.getValue() > i)
        {
            //Print Star
            System.out.print("*");
        }else{
            //Print space
            System.out.print(" ");
        }
        System.out.println();
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM