Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
String O(n)时间复杂度和O(1)空间复杂度用来判断两个字符串是否是彼此的置换_String_Performance_Big O - Fatal编程技术网

String O(n)时间复杂度和O(1)空间复杂度用来判断两个字符串是否是彼此的置换

String O(n)时间复杂度和O(1)空间复杂度用来判断两个字符串是否是彼此的置换,string,performance,big-o,String,Performance,Big O,有没有一种算法可以判断两个字符串是否是时间复杂度为On,空间复杂度为O1的相互排列?下面是我用java编写的一个简单程序,它给出了时间复杂度为On,空间复杂度为O1的答案。它的工作原理是将每个字符映射到一个素数,然后将字符串素数映射中的所有字符相乘。如果这两个字符串是排列,那么它们应该具有相同的唯一字符,每个字符的出现次数相同 下面是一些实现这一点的示例代码: // maps keys to a corresponding unique prime static Map<Integer,

有没有一种算法可以判断两个字符串是否是时间复杂度为On,空间复杂度为O1的相互排列?

下面是我用java编写的一个简单程序,它给出了时间复杂度为On,空间复杂度为O1的答案。它的工作原理是将每个字符映射到一个素数,然后将字符串素数映射中的所有字符相乘。如果这两个字符串是排列,那么它们应该具有相同的唯一字符,每个字符的出现次数相同

下面是一些实现这一点的示例代码:

// maps keys to a corresponding unique prime
static Map<Integer, Integer> primes = generatePrimes(255); // use 255 for
                                                        // ASCII or the
                                                        // number of
                                                        // possible
                                                        // characters

public static boolean permutations(String s1, String s2) {
    // both strings must be same length
    if (s1.length() != s2.length())
         return false;

     // the corresponding primes for every char in both strings are multiplied together
     int s1Product = 1;
     int s2Product = 1;

     for (char c : s1.toCharArray())
         s1Product *= primes.get((int) c);

     for (char c : s2.toCharArray())
         s2Product *= primes.get((int) c);

     return s1Product == s2Product;

 }

 private static Map<Integer, Integer> generatePrimes(int n) {

     Map<Integer, Integer> primes = new HashMap<Integer, Integer>();

     primes.put(0, 2);

     for (int i = 2; primes.size() < n; i++) {
         boolean divisible = false;

         for (int v : primes.values()) {
             if (i % v == 0) {
                 divisible = true;
                 break;
             }
         }

         if (!divisible) {
             primes.put(primes.size(), i);
             System.out.println(i + " ");
         }
     }

     return primes;

 }

是的,当然有一个很好的方法。对此,您必须使用计数排序。根本没有理由生成素数。下面是一段C代码片段,描述了该算法:

bool is_permutation(string s1, string s2) {
    if(s1.length() != s2.length()) return false;

    int count[256]; //assuming each character fits in one byte, also the authors sample solution seems to have this boundary
    for(int i=0;i<256;i++) count[i]=0;
    for(int i=0;i<s1.length();i++) { //count the digits to see if each digits occur same number of times in both strings
        count[ s1[i] ]++; 
        count[ s2[i] ]--;
    }

    for(int i=0;i<256;i++) { //see if there is any digit that appeared in different frequency
        if(count[i]!=0) return false;
    }
    return true;
}
现在假设m是一个常数,这里的顺序变为

Time complexity: O(n) 
Memory Complexity O(1)

请注意,n是字符串的长度。您可能希望搜索字符串的字谜,而不是排列。我严重怀疑这一点,因为即使Ayon的答案是Ologn。聪明的解决方案,即使它不是simplest@JensRoland谢谢
Time complexity: O(n) 
Memory Complexity O(1)