title: Sherlock and the Valid String, giving me wrong results in 5 test cases
body: <p>I am trying to run a program from hackerrank. This is the question:</p>
  
  <p>Sherlock considers a string to be valid if all characters of the string appear the same number of times. It is also valid if he can remove just <em>one</em> character at <em>one</em> index in the string, and the remaining characters will occur the same number of times. Given a string , determine if it is valid.</p>
  
  <p>For example, if s="abc", it is a valid string because frequencies are {a:1,b:1,c:1}. So is abcc because we can remove one c and have 1 of each character in the remaining string. If s='abccc' however, the string is not valid as we can only remove 1 occurrence of c . That would leave character frequencies of {a:1,b:1,c:2}.</p>
  
  <p>This is the link:
  <a href="https://www.hackerrank.com/challenges/sherlock-and-valid-string/problem?h_l=interview&amp;playlist_slugs%5B%5D=interview-preparation-kit&amp;playlist_slugs%5B%5D=strings" rel="nofollow noreferrer">https://www.hackerrank.com/challenges/sherlock-and-valid-string/problem?h_l=interview&amp;playlist_slugs%5B%5D=interview-preparation-kit&amp;playlist_slugs%5B%5D=strings</a></p>
  
  <p>One of the 5 test cases that failed would be:
  aaaabbcc should give false, but it is giving me true.
  aabbc should give true but is giving me false.</p>
  
  <p>But somehow 5 of my test cases are coming to be wrong: 
  Here is the following program.</p>
  
  <pre><code>import java.util.ArrayList;
  import java.util.Collections;
  import java.util.HashMap;
  import java.util.List;
  
  public class SherlokValidString
  {
  
      boolean is_valid(String s)
      {
          int count=0;
          HashMap&lt;Character, Integer&gt; map = new HashMap&lt;&gt;();
          char[] str_arr= s.toCharArray();
          for(char c:str_arr)
          {
              if(map.containsKey(c))
              {
                  map.put(c, map.get(c)+1);
              }
              else
              {
                  map.put(c,1);
              }
          }
          if (map.size()==1)
          {
              return true;
          }
          else {
  
              List ll=new ArrayList&lt;&gt;(map.values());
              System.out.println(ll);
  
              Collections.sort(ll);
              int first_element=(int)ll.get(0);
  
              for(int i=1;i&lt;(ll.size()-1);i++)
              {
                  //System.out.println((int)ll.get(i)+1);
                  if (first_element==(int)ll.get(i+1))
                  {
                      count=0;
                  }
                  else if(first_element!=(int)ll.get(i+1))
                  {
                      count++;
                  }
              }
              if(count&lt;=1)
              {
                  //System.out.println(count);
                  return true;
              }
              else
              {
                  return false;
              }
          }
      }
  
      public static void main(String[] args)
      {
          SherlokValidString svs = new SherlokValidString();
          System.out.println(svs.is_valid("abbccc"));
  
      }
  }
  </code></pre>
  
  <p>It should be returning false, its giving me true.
  Please help. Thanks.</p>