1

I already searched every question related to this error on StackOverflow but trust me my case is different here. actually, I am preparing for competitive level programming skills and I solved this question successfully on the laptop and the output is 100%true without any type of error for this question of hacker earth (link below)-

https://www.hackerearth.com/practice/basic-programming/input-output/basics-of-input-output/practice-problems/algorithm/two-strings-4/#c198374

but when I try to submit my code on hacker earth it throws an error and now I am really confused that why error comes when it successfully ran on my laptop. the error screenshot below - enter image description here here is my code -

import java.util.*;

class nalin{
    public static void main(String args[]){
        Scanner bob = new Scanner(System.in);
        int bobs = bob.nextInt();
        String result[] = new String[bobs];
        for(int g = 0; g<bobs; g++){
        Scanner s = new Scanner(System.in);
        String x = s.nextLine();
        String arr[] = x.split("\\s+");
        int coun = 0;
        char v1[] = arr[0].toCharArray();
        char v2[] = arr[1].toCharArray();

        for(int i = 0; i<v1.length; i++){
            for(int j = 0; j<v2.length; j++){
                if(v1[i] == v2[j]){
                    coun = coun+1;
                    break;
                }
            }
        }
        if(coun == v1.length){
            result[g] = "YES";
        }else{
            result[g] = "NO";
        }
     }
    for(int l = 0; l<result.length; l++){
        System.out.println(result[l]);
    }
    }
}
Nalin Nishant
  • 716
  • 1
  • 5
  • 20
  • Note : Use Hashing Concept Only . Try to do it in O(string length). Mention in question itself. and you logic is also wrong. check nested loop which you used for string comparision. Also you have decelared scanner 2 times. remove 1 inside for loop. – SSP Sep 18 '19 at 18:25
  • You declared to scanners to the same ressource, you should never do that since they conflict with each other. Scanners buffer input, they read more than you tell them to read, in order to increase performance. Also, you are mixing `nextInt` with `nextLine`, that will give you some trouble as `nextXXX` does not do what you think (see the corresponding SO thread). – Zabuzard Sep 18 '19 at 18:29
  • with Scanner you need to check if there is a next line with hasNextLine() https://stackoverflow.com/questions/7209110/java-util-nosuchelementexception-no-line-found – Sarel Foyerlicht Sep 18 '19 at 18:30
  • @SSP if I removed when scanner inside of loops then I throw an error - indexoutofbound that why I included it twice ... yes i know my logic is little bit wrong but approx 70% cases have true answer when i compiled this code – Nalin Nishant Sep 18 '19 at 18:33
  • @sarel then why this code is working on my laptop? – Nalin Nishant Sep 18 '19 at 18:37
  • it depends on the input if you use different input it may crash... – Sarel Foyerlicht Sep 18 '19 at 19:40

2 Answers2

1

Note : Use Hashing Concept Only . Try to do it in O(string length). Mention in question itself. and you logic is also wrong( check nested loop which you used for string comparision.) Also you have decelared scanner 2 times. remove 1 inside for loop

You are using nextLine method of scanner class. Below is nextline() description

nextLine
public String nextLine()


    Advances this scanner past the current line and returns the inputthat was skipped.This method returns the rest of the current line, excluding any lineseparator at the end. The position is set to the beginning of the nextline. 
    Since this method continues to search through the input lookingfor a line separator, it may buffer all of the input searching forthe line to skip if no line separators are present.

    Returns:the line that was skipped

Throws:NoSuchElementException 
 1 - if no line was foundIllegalStateException 
 2 - if this scanner is closed.

Below is working code - But I have not corrected your logic.

package Array;

import java.util.*;

class nalin {
    public static void main(String args[]) {
        Scanner bob = new Scanner(System.in);
        int bobs = bob.nextInt();
        String result[] = new String[bobs];
        for (int g = 0; g < bobs; g++) {
            String x = bob.next();
            String y = bob.next();
            //String arr[] = x.split("\\s+");
            int coun = 0;
            char v1[] = x.toCharArray();
            char v2[] = y.toCharArray();

            for (int i = 0; i < v1.length; i++) {
                for (int j = 0; j < v2.length; j++) {
                    if (v1[i] == v2[j]) {
                        coun = coun + 1;
                        break;
                    }
                }
            }
            if (coun == v1.length) {
                result[g] = "YES";
            } else {
                result[g] = "NO";
            }
        }
        for (int l = 0; l < result.length; l++) {
            System.out.println(result[l]);
        }
    }
}
SSP
  • 2,650
  • 5
  • 31
  • 49
  • can you show me some example or link of 0(String length) – Nalin Nishant Sep 18 '19 at 18:34
  • if I removed scanner inside of loops then I throw an error - indexoutofbound that why I included it twice ... yes i know my logic is little bit wrong but approx 70% cases have true answer when i compiled this code – Nalin Nishant Sep 18 '19 at 18:34
  • my problem is of compiling not with the logic bro... i will correct my logic later – Nalin Nishant Sep 18 '19 at 18:35
  • Accept answer if its help – SSP Sep 18 '19 at 18:49
  • I appreciate your work and it's ok I do not need logic but you removed some line from code and pasted here ... 1) how do I get the string you removed that line without including anywhere in a code to get a string... bro i can also correct my answer by removing some line :-) i need correction bro not removal – Nalin Nishant Sep 18 '19 at 19:59
0

Put the scanner declaration outside of the loop:

    Scanner s = new Scanner(System.in);
    int bobs = s.nextInt();
    for(int g = 0; g<bobs; g++)
    {
    }

Use a single scanner throughout the execution

mangusta
  • 3,470
  • 5
  • 24
  • 47