【发布时间】:2019-03-16 23:19:48
【问题描述】:
我正在为密码破解者进行hackerrank 中等挑战。我希望能够检查给定的字符串attempt 是否包含pass 中的所有单词。 pass 是密码数组,attempt 是 pass 中随机条目的串联。如果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