简体   繁体   English

确定两个字符串是否是另一个字符串的置换的子字符串

[英]Determining if two strings are a substring of a permutation of another String

So I am trying to figure out if two strings when combined together are a substring of a permutation of another string. 因此,我试图找出两个字符串组合在一起时是否是另一个字符串排列的子字符串。

I have what I believe to be a working solution but it is failing some of the JUnit test cases and I dont have access to the ones that it is failing on. 我有一个可行的解决方案,但是它在某些JUnit测试用例中失败了,并且我无法访问在其上失败的那些。

here is my code with one test case 这是我的一个测试用例的代码

String a="tommarvoloriddle";
String b="lord";
String c="voldemort";
String b= b+c; 
char[] w= a.toCharArray();
char[] k= b.toCharArray();
Arrays.sort(k);
Arrays.sort(w);
pw.println(isPermuation(w,k)?"YES":"NO");


static boolean isPermuation(char[] w, char[] k)
{
    boolean found=false;
    for(int i=0; i<k.length; i++)
    {
        for(int j=i; j<w.length; j++)
        {
            if(k[i]==w[j])
            {
                j=w.length;
                found=true;
            }
            else
                found=false;
        }
    }


    return found;
}

any help getting this to always produce the correct answer would be awesome and help making it more efficient would be great too 使它始终产生正确答案的任何帮助都将是很棒的,并且使其更有效的帮助也将是很棒的

What you have is not a working solution. 您拥有的不是可行的解决方案。 However, you don't explain why you thought it might be, so it's hard to figure out what you intended. 但是,您没有解释为什么会这样,因此很难弄清您的意图。 I will point out that your code updates found unconditionally for each inner loop, so isPermutation() will always return the result of the last comparison (which is certainly not what you want). 我将指出,您的代码更新是found每个内部循环中无条件found ,因此isPermutation()将始终返回上一次比较的结果(这当然不是您想要的结果)。

You did the right thing in sorting the two arrays in the first place -- this is a classic step which should allow you to efficiently evaluate them in one pass. 首先,您对两个数组进行了排序是正确的事情-这是一个经典步骤,可以让您一次有效地评估它们。 But then, instead of a single pass, you use a nested loop -- what did you intend here? 但是,然后,您将使用嵌套循环而不是单次通过-您打算在这里做什么?

A single pass implementation might be something like: 单遍实现可能类似于:

static boolean isPermutation(char[] w, char[] k) {
  int k_idx=0;
  for(w_idx=0; w_idx < w.length; ++w_idx) {
    if(k_idx == k.length)
      return true; // all characters in k are present in w
    if( w[w_idx] > k[k_idx] )
      return false;  // found character in k not present in w
    if( w[w_idx] == k[k_idx] )
      ++k_idx;  // character from k corresponds to character from w
  }
  // any remaining characters in k are not present in w
  return k_idx == k.length;
}

So we are only interested in whether the two combined strings are a subset of a permutation of another string, meaning that the lengths can in fact differ. 因此,我们仅对两个组合的字符串是否是另一个字符串的置换的子集感兴趣,这意味着长度实际上可以不同。 So let's say we have: 假设我们有:

String a = "tommarvoloriddle";
String b = "lord";
String c = "voldemort";

char[] master = a.ToCharArray();
char[] combined = (b + c).ToCharArray();

Arrays.Sort(master);
Arrays.Sort(combined);

System.out.println(IsPermutation(master, combined) ? "YES" : "NO");

Then our method is: 那么我们的方法是:

static boolean IsPermutation(char[] masterString, char[] combinedString)
{
    int combinedStringIndex = 0;
    int charsFound = 0;
    int result = 0;

    for (int i = 0; i < masterString.Length; ++i) {
        result = combinedString[combinedStringIndex].CompareTo(masterString[i]);
        if (result == 0) {
            charsFound++;
            combinedStringIndex++;
        }
        else if (result < 0) {
            return false;
        }
    }

    return (charsFound == combinedString.Length);
}

What the above method does: it starts comparing characters of the two strings. 上面的方法做什么:它开始比较两个字符串的字符。 If we have a mismatch, that is, the character at the current masterString index does not match the character at the current combinedString index, then we simply look at the next character of masterString and see if that matches. 如果我们有一个不匹配,也就是说,目前在字符masterString指数没有性格的电流匹配combinedString指数,那么我们只需看看下一个字符masterString ,看看是否匹配。 At the end, we tally the total number of characters matched from our combinedString , and, if they are equal to the total number of characters in combinedString (its length), then we have established that it is indeed a permutation of masterString . 最后,我们计算从combinedString匹配的字符总数,如果它们等于combinedString的字符总数(其长度),那么我们已经确定它确实是masterString的排列。 If at any point, the current character in masterString is numerically greater than the current character in combinedString then it means that we will never be able to match the current character, so we give up. 如果在任何时候,在当前字符masterString在数值上比当前字符更大combinedString那么就意味着我们将永远无法当前字符匹配,所以我们放弃了。 Hope that helps. 希望能有所帮助。

If two Strings are a permuation of the other you should be able to do this 如果两个字符串是另一个的置换,则应该可以做到这一点

public static boolean isPermuted(Strign s1, String s2) {
     if (s1.length() != s2.length()) return false;

     char[] chars1 = s1.toCharArray();
     char[] chars2 = s2.toCharArray();
     Arrays.sort(chars1);
     Arrays.sort(chars2);
     return Arrays.equals(chars1, chars2);
}

This means that when sorted the characters are the same, in the same number. 这意味着排序时,字符相同,编号相同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM