Skip to main content

Featured

say hello world with C++ - Solution in Hacker Rank - hackerranksolutions8

  Objective This is a simple challenge to help you practice printing to  stdout . You may also want to complete  Solve Me First  in C++ before attempting this challenge. We’re starting out by printing the most famous computing phrase of all time! In the editor below, use either  printf  or  cout  to print the string  Hello ,World!  to  stdout . The more popular command form is  cout . It has the following basic form: cout<<value_to_print<<value_to_print; Any number of values can be printed using one command as shown. The  printf  command comes from C language. It accepts an optional format specification and a list of variables. Two examples for printing a string are: printf("%s", string);   printf(string); Note that neither method adds a newline. It only prints what you tell it to. Output Format Print   Hello ,World!   to stdout. Sample Output Hello, World! Solution:- //Say Hello, ...

Java Anagrams Hacker Rank Solution

 Problem:-

Two strings,  and , are called anagrams if they contain all the same characters in the same frequencies. For example, the anagrams of CAT are CATACTTACTCAATC, and CTA.Complete the function in the editor. If  and  are case-insensitive anagrams, print “Anagrams”; otherwise, print “Not Anagrams” instead.

Input Format

The first line contains a string denoting . 
The second line contains a string denoting .

Constraints

  • Strings  and  consist of English alphabetic characters.
  • The comparison should NOT be case sensitive.

Output FormatPrint “Anagrams” if  and  are case-insensitive anagrams of each other; otherwise, print “Not Anagrams” instead.

Sample Input 0

anagram
margana

Sample Output 0

Anagrams

Explanation 0

CharacterFrequency: anagramFrequency: margana
A or a33
G or g11
N or n11
M or m11
R or r11

The two strings contain all the same letters in the same frequencies, so we print “Anagrams”.Sample Input 1

anagramm
marganaa

Sample Output 1

Not Anagrams

Explanation 1

CharacterFrequency: anagrammFrequency: marganaa
A or a34
G or g11
N or n11
M or m21
R or r11

The two strings don’t contain the same number of a‘s and m‘s, so we print “Not Anagrams”.Sample Input 2

Hello
hello

Sample Output 2

Anagrams

Explanation 2

CharacterFrequency: HelloFrequency: hello
E or e11
H or h11
L or l22
O or o11

The two strings contain all the same letters in the same frequencies, so we print “Anagrams”.

Solution:-

import java.util.Scanner;



public class Solution {



  static boolean isAnagram(String a, String b) {



        if (a.length() != b.length()) return false;


        


        a = a.toLowerCase();


        b = b.toLowerCase();


        


        int [] temp = new int [Character.MAX_VALUE]; 


    //sry, too lazy to calculate ASCII idx of a-z rel. i :)


        


        int summ = 0;



        for (int i = 0; i < a.length(); i++){


            summ += ++temp [a.charAt(i)] <= 0 ? -1 : 1;


            summ += --temp [b.charAt(i)] >= 0 ? -1 : 1;


        }



        return summ == 0;


        


}


    public static void main(String[] args) {


    


        Scanner scan = new Scanner(System.in);


        String a = scan.next();


        String b = scan.next();


        scan.close();


        boolean ret = isAnagram(a, b);


        System.out.println( (ret) ? "Anagrams" : "Not Anagrams" );


    }


}

Comments