【问题标题】:How to split a String into an Array if a .contains() condition is met?如果满足 .contains() 条件,如何将字符串拆分为数组?
【发布时间】:2019-03-16 23:19:48
【问题描述】:

我正在为密码破解者进行hackerrank 中等挑战。我希望能够检查给定的字符串attempt 是否包含pass 中的所有单词。 pass 是密码数组,attemptpass 中随机条目的串联。如果attempt 仅包含在pass 中作为条目找到的单词,则认为它是一个好的密码,并打印来自attempt 输入的单词,以空格限制。

示例输入

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

预期输出

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

代码

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();
}
}

焦点部分是 //----------------- 注释部分中的部分。基本上,我的目标是查看尝试是否包含pass 中的正确条目,如果是,则将attempt 的子字符串(或类似地,pass 中的条目)保存到可以打印的新数组中以正确的顺序。如果您检查上面的预期输出,您会看到输出与attempt 相同除了空格

基本上,我需要在attempt 的单词中找到中断并打印出来,如果它满足上述要求(第一段)。

查看更多详情 https://www.hackerrank.com/challenges/password-cracker/problem

【问题讨论】:

    标签: java arrays split passwords contains


    【解决方案1】:

    如果对你有帮助

        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());
        }
    

    【讨论】:

      【解决方案2】:

      您的 for 循环看起来太复杂了,这就是我要处理的部分。

      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
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-03-23
        • 1970-01-01
        • 1970-01-01
        • 2015-07-18
        • 1970-01-01
        相关资源
        最近更新 更多