How to split a String into an Array if a .contains() condition is met?

I'm doing a hackerrank medium challenge for a password cracker. I want to be able to check if a given string, attempt, contains all the words in pass. pass is an array of passwords and attempt is a concatenation of random entries in pass. If attempt contains ONLY words that are found as entries in pass, then it is deemed a good password and the words from the input of attempt, limited with spaces, is printed.

Sample Input

3                                        //3 attempts
6                                        //6 words for attempt 1
because can do must we what              //pass[]
wedowhatwemustbecausewecan               //attempt
2                                        //...
hello planet
helloworld
3
ab abcd cd
abcd

Expected Output

we do what we must because we can
WRONG PASSWORD                           //Because planet is not in pass[]
ab cd

Code

public class Solution {

static String passwordCracker(String[] pass, String attempt) {
    int arrayLength=pass.length;       

    int accuracy=0;
    String trips_array[] = new String[pass.length];

    String [] newWord = new String[20];

    for (int i=0; i<pass.length;i++)
    {
       // int j=0;
       String[] arr = pass[i].split(" ");  
        //-------------------------------
        if (attempt.contains(pass[i]))
        {

            accuracy++;
            newWord[i] = pass[i];
            trips_array[i] = attempt.split(" ");
        }
          //------------------------------
    }

    StringBuilder sb = new StringBuilder();
    for (String words : trips_array) {
            sb.append(words);
        }
    for (int i=0; i<pass.length;i++)
    {
        if (accuracy==pass.length)
                  return sb.toString() + " ";

        else
            return "WRONG PASSWORD";
    }
    return "test";
}

public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    int t = in.nextInt();
    for(int a0 = 0; a0 < t; a0++){
        int n = in.nextInt();
        String[] pass = new String[n];
        for(int pass_i = 0; pass_i < n; pass_i++){
            pass[pass_i] = in.next();
        }
        String attempt = in.next();
        String result = passwordCracker(pass, attempt);
        System.out.println(result);
    }
    in.close();
}
}

The part in focus is the part in the //----------------- comment section. Basically, my goal is to see if the attempt contains the correct entries in pass, and if so, save that substring of the attempt (or similarly, the entry in pass) to a new array which can be printed in the correct order. If you check the expected output above, you'll see that the output is the same as attempt except with spaces.

Essentially, I would need to find the breaks in the words of attempt and print that if it fulfills the above requirements (first paragraph).

See this for more details https://www.hackerrank.com/challenges/password-cracker/problem

2 answers

  • answered 2018-10-11 20:25 Mark

    If it helps you

        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
        int testNumb = Integer.parseInt(reader.readLine());
    
        List<String> passList = new ArrayList<>();
        List<String> attList = new ArrayList<>();
    
        for (int i = 0; i < testNumb; i++) {
            reader.readLine();
            passList.add(reader.readLine());
            attList.add(reader.readLine());
        }
        reader.close();
    
        for (int i = 0; i < testNumb; i++) {
    
            String s1 = passList.get(i);
            String s2 = attList.get(i);
    
            StringBuilder sb = new StringBuilder();
            String[] s1Arr = s1.split(" ");
            while (s2.length() > 0) {
                int s2Lenght = s2.length();
                for (String s : s1Arr) {
                    if (s2.startsWith(s)) {
                        sb.append(s + " ");
                        s2 = s2.substring(s.length());
                    }
                }
                if (s2.length() == s2Lenght) {
                    sb = new StringBuilder("wrong pass");
                    break;
                }
            }
            System.out.println(sb.toString());
        }
    

  • answered 2018-10-11 20:26 Joakim Danielson

    Your for loop looks too complicated, here is how I would approach that part.

    boolean isAllWords = true;
    int checksum = 0;
    for (int j = 0; j < pass.length; j++) {
        if  (!attempt.contains(pass[j]) {
           isAllWords = true;
           break;
        }
        checksum += pass[j].length;
    }
    
    if (isAllWords && checksum == attempt.length) {
       //This means attempt contains all words in pass array and nothing more
       //... handle successful attempt
    } else {
      //... handle bad attempt
    }