【问题标题】:Count of distinct duplicated characters in JavaJava中不同重复字符的计数
【发布时间】:2018-06-26 14:56:43
【问题描述】:

我编写了代码来查找字符串输入中重复字符的数量。但是,在阅读规范后,我意识到这不是所要求的。是的,我需要计算重复字符,但不是该特定字符的数量,我需要所有重复字符的数量。

我已经给出了输入和输出以帮助更好地解释。我不认为我做得很好。

输入: "abcdea" => "a" 重复 => 输出: 1

输入: "abcdeae" => "a" 和 "e" 重复 => 输出: 2

输入: "abcdeaed" => "a", "d" 和 "e" 重复 => 输出: 3

我已经把我的代码放在下面了。任何人都可以帮助调整我的代码吗?

public static int duplicatesCount(String text) 
    {
        Map<Character,Integer> map = new HashMap<Character,Integer>();
        char[] carray = text.toCharArray();
        for (char c : carray)
        {
            if (map.containsKey(c))
            {
                map.put(c, map.get(c) +1);
            }
            else
            {
                map.put(c, 1);
            }
        }
        Set <Character> setChar = map.keySet();
        int returnC = 1;
        for (Character c : setChar)
        {
            if (map.get(c) > 1)
            {
                returnC = map.get(c);
            }
        }
        return returnC;

【问题讨论】:

  • returnC 当前对应于最后一个字母多次出现的次数。而不是每次都覆盖returnC,而是添加它(但首先将其初始化为0而不是1

标签: java hashmap duplicates


【解决方案1】:

执行此操作的更好方法是对字符串进行排序,然后对其进行迭代。如果前一个字符=当前字符,则增加重复数字并且不要再次增加它,直到您看到字符更改。

这不需要额外的存储空间(例如哈希映射)。

这也可以用来计算每个字母的重复次数,只要有微小的变化:)。

【讨论】:

    【解决方案2】:

    在你的 sn-p 结束时...

    int returnC = 0;
    for (Character c : setChar)
        {
            if (map.get(c) > 1)
            {
                returnC ++;  //for each which has more than one instance
            }
        }
        return returnC;
    

    【讨论】:

    • 是的!粘贴时忘记更改
    【解决方案3】:

    每次在 hashmap 中遇到值大于 1 的字符时计数

    int returnC = 0;
    for (Character c : setChar)
    {
        if (map.get(c) > 1)
            returnC++;
    }
    

    或者您可以在像这样创建哈希图时这样做

    Map<Character,Integer> map = new HashMap<Character,Integer>();
    char[] carray = text.toCharArray();
    Set <Character> setChar = new Set<Character>(); //initialize the set up here
    for (char c : carray)
    {
        if (map.containsKey(c))
        {
            map.put(c, map.get(c) +1);
            setChar.add(c); //just add to set when a char already exists
        }
        else
        {
            map.put(c, 1);
        }
    }
    
    return setChar.size(); //then simply return the size of the set
    

    【讨论】:

      【解决方案4】:

      这是另一种方法 - 不使用 Map 或排序算法。

      public static int duplicatesCount(String text) {
          int cnt = 0;
          while (text.length() > 1) {
              char firstLetter = text.charAt(0);
              if (text.lastIndexOf(firstLetter) > 0) {
                  cnt++;
                  text = text.replaceAll(("" + firstLetter), "");
              } else {
                  text = text.substring(1, text.length());
              }
          }
      
          return cnt;
      }
      

      基本思想是逐个字符地检查字符串并检查该字符是否存在于字符串中的某个位置(if (text.lastIndexOf(firstLetter) &gt; 0))。如果存在,则递增 cnt,然后删除字符串中所有出现的字符。
      否则,只需删除第一个字符(text = text.substring(1, text.length());

      【讨论】:

      • 感谢您的帮助 :) 我最终根本没有使用 HashMaps。当我运行它时,它导致 IDE 超时。再次感谢你:)
      • 没问题。 “当我运行它时,它导致 IDE 超时。” - 也许你有一个不定式循环。再次检查代码。
      • 如果您发现其中一些答案对您的解决方案有帮助和有用,您应该投票或/并接受该解决方案。
      • 我确实为您的解决方案投了票,但因为我的声誉低于 15 分,所以弹出窗口说我的投票不会显示。不知道如何选择解决方案..
      • 我的意思不仅仅是在我的回答中,而是在一般情况下:任何对您有帮助的答案。此外,如果您接受该解决方案,您将获得声誉。
      【解决方案5】:

      您可以使用String 方法

      int count = 0;
      String s = "abcdeabc";
      while(s.length() > 0) {
          String c = s.substring(0, 1);
          if(s.lastIndexOf(c) != 0) {
              count++;
          }
          s = s.replaceAll(c, "");
      }
      

      【讨论】:

        【解决方案6】:

        在您当前的代码中,您返回的是值而不是计数。

        例如:

        1. 如果您的输入是“abcdea”,则 returnC 保存值 2,即 键 a 重复的次数。

        2. 如果您的输入是“abcdabeb”,其中 a 重复两次,b 重复 三次。您的输出将为 3,因为 returnC 将保存键的值 b.

        你可以像这样修改你的代码。

         Set <Character> setChar = map.keySet();
         int returnC = 0;
         for (Character c : setChar) {
               if (map.get(c) > 1) { 
                     returnC++;
               } 
         }
         return returnC;
        

        【讨论】:

          【解决方案7】:

          我们还可以使用 String Buffer 类来创建另一个重复字符的字符串--然后我们可以显示它--

          public static void main(String arghya[])
          {
              String name="ARGHYA MUKHERJEE";
              StringBuffer duplicate=new StringBuffer();
          
              Set<Character> charlist=new HashSet<Character>();
          
              for(int i=0;i<name.length();i++) {
                  Character c=name.charAt(i);
                  if(charlist.contains(c))
                  { 
                      duplicate.append(c);
                      //System.out.println(c+ " is repeatitive character in the string");
                  }
                  else {
                      charlist.add(c);
                  }
              }
              System.out.print("Duplicate characters which has appeared are as follows: "+duplicate);     
          }
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2014-07-04
            • 2017-10-14
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2012-09-26
            • 2020-08-17
            相关资源
            最近更新 更多