We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Loading...
  • Practice
  • Compete
  • Jobs
  • Leaderboard
  • Hiring developers?
  1. Practice
  2. Algorithms
  3. Strings
  4. Anagram
  5. Discussions

Anagram

  • Problem
  • Submissions
  • Leaderboard
  • Discussions
  • Editorial
  • Topics

    You are viewing a single comment's thread. Return to all comments →

  • udayshankar911 3 years ago+ 7 comments
        int diff = 0,t;
        t = s.nextInt();
        String str = s.next();
        int len = str.length();
        for(int k = 0; k < t;k++)
        {    
        if(len%2!=0)
            System.out.println("-1");
        else
        {
            String str1 = str.substring(0,(len/2));
            String str2 = str.substring(len/2);        
            for(int i=0;i<str1.length();i++)
            {
    
                    if(!str2.contains(str1.charAt(i)+""))
                        diff++;                       
    
            }
                       System.out.println(diff);
        }
       }
    
    -1|
    ParentPermalink
    • udayshankar911 3 years ago+ 2 comments

      Guys TC 1 an 14 executed but others failed. when i tested in Netbeans IDE i dont find any prob. can anyone look at it and leem know the issue .

      1|
      ParentPermalink
      • DanLuo 3 years ago+ 1 comment

        if there is 2 "a" in str1, but only 1 "a" in str2, diff will not increase

        2|
        ParentPermalink
        • cherry_9 3 years ago+ 1 comment

          can you please explain this further

          0|
          ParentPermalink
          • omtonape 3 years ago+ 1 comment

            take second string into stringbuilder and if char from first string is present in second string remove char at that position from stringbuilder if char is not present in second string then increment count final answer is count.... thanks DanLuo

            1|
            ParentPermalink
            • mayurnagdev123 9 months ago+ 0 comments

              I did something similar.Converted String to StringBuffers sb1 and sb2. Upon iterating characters through sb1: if the character is found in sb2,then change that character value in sb2 to some other value(say '/') so that it couldn't be found in further iterations. This is essential as sb1 may have repeating characters.

              0|
              ParentPermalink
      • sivach 3 years ago+ 3 comments

        Use stringbuilder for first string, remove the char from builder if exist and at the end, answer = string builder length.

        0|
        ParentPermalink
        • udayshankar911 3 years ago+ 1 comment

          Got it. thnq :)

          -2|
          ParentPermalink
          • dsaxena 3 years ago+ 0 comments

            I understood the problem in your code but I a, little bit confused about the mentioned solution. actually I am not much familier with Java, I am currently learning, So can you please explain me the solution??

            0|
            ParentPermalink
        • omtonape 3 years ago+ 0 comments

          but in that case also mine are passing test cases 1 and 14 but rest are failing

          0|
          ParentPermalink
        • andadeacu 2 years ago+ 0 comments

          static int anagram(String s) { int result = 0; Map map = new HashMap();

              if (s.length() % 2 != 0) {
                  return -1;
              } else {
                  int half = s.length() / 2;
                  for (int k = 0; k < half; k++) {
                      char letter = s.charAt(k);
          
                      if (!map.containsKey(letter)) {
                          map.put(letter, 1);
                      } else {
                          Integer frequency = map.get(letter);
                          map.put(letter, ++frequency);
                      }
                  }
          
                  for (Map.Entry<Character, Integer> entry : map.entrySet()) {
                      System.out.println(entry.getKey() + " " + entry.getValue());
                  }
          
                  for (int k = half; k < s.length(); k++) {
                      char letter = s.charAt(k);
                      //System.out.println("a doua jumatate este:");
                      //System.out.println(letter);
          
                      if (!map.containsKey(letter)) {
                          result++;
                      } else {
                          Integer freq = map.get(letter);
                          freq--;
                          map.put(letter, freq);
          
                      }
          
                  }
          
          
          
                  for (Map.Entry<Character, Integer> entry : map.entrySet()) {
                      System.out.println(entry.getKey() + " " + entry.getValue());
                  }
          
                  return result;
          
              }
          }
          

          for String xaxbbbxx I obtain 0.

          Can someone one say me why?

          0|
          ParentPermalink
    • 19soumikrakshi96 3 years ago+ 0 comments

      you should take the string input inside the testcase loop.

      0|
      ParentPermalink
    • 13h61a05f3 3 years ago+ 0 comments

      Your code don't work for input aabacb

      2|
      ParentPermalink
    • deepa_navaneeth 3 years ago+ 1 comment

      The last test case fails. Try with xaxbbbxx.

      0|
      ParentPermalink
      • iainws 3 years ago+ 0 comments

        I failed with c++ because of map.count(element). when the element count is 0, it still registers as a hit. Somewhat confusing if you choose to use this language reference. its better to check with if(map.count(element) && map.count(element) != 0). good problem.

        0|
        ParentPermalink
    • shaik_jabivulla 12 months ago+ 0 comments

      its not full solution

      0|
      ParentPermalink
    • mayurnagdev123 9 months ago+ 0 comments

      This wont work for: "fdhlvosfpafhalll" which is the input string in the first test case

      0|
      ParentPermalink
    • knaman1009 9 months ago+ 0 comments

      Plz send the modified code

      -1|
      ParentPermalink
  • Contest Calendar
  • Blog
  • Scoring
  • Environment
  • FAQ
  • About Us
  • Support
  • Careers
  • Terms Of Service
  • Privacy Policy
  • Request a Feature