2

Input - "sumit mitsu"

How to find if permutation of anyone of the String is matching the other. Please find my code below on my attempt and help me if my approach is right. I also tried to use Arrays.sort to sort the string but hackerEarth editor is not accepting Arrays.sort. Is there any other approach to solve this? TIA.

Note - This is a hackerEarth two strings problem.

I had split the input two string array. And then I converted each keyword into character array. With for loop i ran through each keyword in S1 to match with S2 array.

class TestClass {
public static void main(String args[] ) throws Exception {

    Scanner s = new Scanner(System.in);
    int cases=s.nextInt();

    for(int i=0;i<cases;i++){   //for multiple lines


        String name1=s.nextLine();
        String name2=s.nextLine();

        char[] n1=name1.toCharArray();
        char[] n2=name2.toCharArray();

        boolean match=false;

            for(int j=0;j<n1.length;j++){

                for(int k=0;k<n2.length;k++){

                    if(n1[j]==n2[k]){
                        match=true;
                        break;
                    }
                    else{
                        match=false;
                    }

                }
            }
        System.out.println(match);
    }
  }
}

Input - majnu jamnu

Expected - True Actual - False

Harish Kannan
  • 465
  • 2
  • 11
  • 22

2 Answers2

2

There are multiple issues with your code:

  1. You probably have a problem in the reading of the input. If you hard code the inputs you tested (i.e. String name1="majnu"; String name2="jamnu";) your program will output true. This is not surprising if you attempt to read each String with nextLine() even though they are entered in the same line.

  2. You don't account for multiple occurrences of the same character. Your program will return true for abbbb and baa.

  3. Your program has quadratic (i.e. O(n^2)) time complexity. It can be done with linear complexity if you maintain a HashMap that counts the occurrences of each character in the first word, and checks the second word against that Map.

Eran
  • 387,369
  • 54
  • 702
  • 768
  • Thanks for pointing the issues. For point 1, i have to use split method to split the input and proceed further. For point 2, i have given break; to come out of the loop if it matches at least once. please let me know if i am wrong. Point 3 - i am not aware of the complexity types you have mentioned, i will learn about it and try to use Hashmap here. – Harish Kannan May 19 '19 at 09:59
  • @HarishKannan the break won't help you. Suppose the inputs are "aaaaaa" and "a". You will match the first and only character of the second String to all the characters of the first String, and wrongly return true. – Eran May 19 '19 at 10:19
  • Yes, that scenario is accepted. I am trying to find out only the non matching characters. – Harish Kannan May 20 '19 at 10:06
0

in one line:

String s1 = "majnu", s2 = "jamnu";
Arrays.equals( s1.chars().sorted().toArray(), s2.chars().sorted().toArray() );  // <— true


for the task apparently a solution with a map is sought:

countMap maps the ASCII codes to the associated number of occurrences: *

Function<String,Map<Integer, Integer>> countMap = (s) -> s.codePoints().boxed().collect(
    Collectors.toMap( Function.identity(), value -> 1, Integer::sum ) );

*) a similar task is perfectly described here

countMap.apply( "majnu" ).equals( countMap.apply( "jamnu" ) ); // <— true

Kaplan
  • 2,572
  • 13
  • 14