如何在O(n)时间内检查两个字符串是否相互排列?(Java)的

Hum*_*ons 1 java algorithm

我写了这个类,可以检查两个给定的字符串是否是彼此的排列.但是,据我所知,这是在O(n ^ 2)时间运行,因为string.indexOf()在O(n)时间运行.

如何提高这项计划的效率?

import java.util.*;

public class IsPermutation{
   public void IsPermutation(){
      System.out.println("Checks if two strings are permutations of each other.");
      System.out.println("Call the check() method");
   }

   public boolean check(){
      Scanner console = new Scanner(System.in);
      System.out.print("Insert first string: ");
      String first = console.nextLine();
      System.out.print("Insert second string: ");
      String second = console.nextLine();

      if (first.length() != second.length()){
         System.out.println("Not permutations");
         return false;
      }

      for (int i = 0; i < first.length(); i++){
         if (second.indexOf(first.charAt(i)) == -1){
            System.out.println("Not permutations");
            return false;
         } 
      }
      System.out.println("permutations");
      return true;
   }
}
Run Code Online (Sandbox Code Playgroud)

ami*_*mit 8

首先,可以O(nlogn)通过对两个字符串进行排序(将它们转换为char[])来完成,然后简单的相等性测试将告诉您原始字符串是否是排列.

O(n)溶液平均情况下,可以通过创建来实现HashMap<Character, Integer>,其中每个键是字符串中的字符,并且该值是它的出现的次数(这被称为直方图).拥有它之后,再次对两个映射进行简单的相等检查将告诉您原始字符串是否是排列.

  • 您可以使用`int [65536]`代替HashMap来获取常量内存.(或`int [128]`for ASCII)+1 for text它通常被称为频率计数."直方图"更常用于数字的分布.(字符是我知道的数字但不是通常认为的那些) (2认同)

Mei*_*ame 6

归档O(n)的一种方法是计算每个字符的频率.

我会使用HashMap,其中字符为键,频率为值.

//create a HashMap containing the frequencys of every character of the String  (runtime O(n) )
public HashMap<Character, Integer> getFrequencys(String s){
    HashMap<Character, Integer> map = new HashMap<>();

    for(int i=0; i<s.length(); i++){
        //get character at position i
        char c = s.charAt(i);

        //get old frequency (edited: if the character is added for the 
        //first time, the old frequency is 0)
        int frequency;
        if(map.containsKey(c)){
            frequency = map.get(c);
        }else{
            frequency = 0;
        }
        //increment frequency by 1
        map.put(c, frequency+1 );
    }

    return map;
}
Run Code Online (Sandbox Code Playgroud)

现在你可以为两个字符串创建一个HashMap,并比较每个字符的频率是否相同

//runtime O(3*n) = O(n)
public boolean compare(String s1, String s2){
    if(s1.length() != s2.length()){
        return false;
    }

    //runtime O(n)
    HashMap<Character, Integer> map1 = getFrequencys(s1);
    HashMap<Character, Integer> map2 = getFrequencys(s2);

    //Iterate over every character in map1 (every character contained in s1)  (runtime O(n) )
    for(Character c : map1.keySet()){
        //if the characters frequencys are different, the strings arent permutations
        if( map2.get(c) != map1.get(c)){
            return false;
        }
    }

    //since every character in s1 has the same frequency in s2,
    //and the number of characters is equal => s2 must be a permutation of s1

    return true;
}
Run Code Online (Sandbox Code Playgroud)

编辑:(未经测试的)代码中存在nullpointer错误