好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

Java 判断两个字符串是否由相同的字符组成的实例

问题:由相同的 字符 组成是指两个 字符串 ,字母以及各个字母的个数是相同的,只是顺序不同。如:[aaaabbc]与[abcbaaa]是由相同字符组成。

方法一:排序法 ,也是最容易想到的方法,将两个字符串转换为字节数组,分别排序后,判断是否相同即可。

方法二:集合法 (空间换时间),利用map集合key的唯一性,遍历第一个字符串,将字符作为key,字符出现的次数作为value,若遇到重复字符则将value+1。之后遍历第二个字符串,遇到字符就将对应的value-1,若value值为1时,就将该字符remove掉。最后判断map是否为空,为空则说明两字符串相同。

方法三:数组法 (空间换时间):由于ascii字符共266个,,申请一个字节数组byte[256],初始化为0,然后遍历第一个字符串,将对应的ascii下标+1,然后遍历第二个字符串,将对应标-1。若最后数组中各个元素的值都是0,则说明字符串相等,反之不等。

先给出方法二,方法三源码:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

package charstring;

 

import java.util.hashmap;

import java.util.map;

 

public class comparestring {

 

     public static boolean compare( char [] a, char [] b){

         int alen = a.length;

         int blen = b.length;

         if (alen!=blen){

             return false ;

         }

         map<character,integer> map = new hashmap<character,integer>();

         for ( int i= 0 ;i<alen;i++){

             if (map.containskey(a[i])){

                 map.put(a[i], map.get(a[i])+ 1 );

             } else {

                 map.put(a[i], 1 );

             }

         }

         for ( int j= 0 ;j<blen;j++){

             if (map.containskey(a[j])&&map.get(a[j])== 1 ){

                 map.remove(a[j]);

             } else {

                 map.put(a[j], map.get(a[j])- 1 );

             }

         }

         return map.isempty();

        

     }

    

     public static boolean compare2(string a,string b){

         byte [] b1 = a.getbytes();

         byte [] b2 = b.getbytes();

         int [] bcount = new int [ 256 ];

         for ( int i= 0 ;i< 256 ;i++){

             bcount[i] = 0 ;

         }

         for ( int i= 0 ;i<b1.length;i++)

             bcount[b1[i]- '0' ]++;

         for ( int i= 0 ;i<b2.length;i++)

             bcount[b2[i]- '0' ]--;

         for ( int i= 0 ;i< 256 ;i++){

             if (bcount[i]!= 0 )

                 return false ;

         }

         return true ;

     }

    

     public static void main(string[] args) {

         // todo auto-generated method stub

 

         stringbuffer s11 = new stringbuffer();

         stringbuffer s22 = new stringbuffer();

         for ( int i= 0 ;i< 10000 ;i++){

             s11.append( "aybcybayaatt" );

         }

         for ( int i= 0 ;i< 10000 ;i++){

             s22.append( "aybcybayaatt" );

         }

         string s1 = s11.tostring();

         string s2 = s22.tostring();

         char [] a = s1.tochararray();

         char [] b = s2.tochararray();

        

         long l3=system.currenttimemillis();

         system.out.println(compare(a,b));

         long l4=system.currenttimemillis();

         system.out.println( "集合用时:" +(l4-l3));

        

         long l1=system.currenttimemillis();

         system.out.println(compare2(s1,s2));

         long l2=system.currenttimemillis();

         system.out.println( "数组用时:" +(l2-l1));

     }

 

}

为了测试,两个方法的运行时间,构造较大的字符串,运行结果:

?

1

2

3

4

true

集合用时: 54 毫秒

true

数组用时: 17 毫秒

由此可见,数组法的运行效率更高,若在对空间没要求的情况下,推荐使用第三种方法。

以上这篇java 判断两个字符串是否由相同的字符组成的实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。

原文链接:https://blog.csdn.net/wutongyu0123wutongyu/article/details/51702271

查看更多关于Java 判断两个字符串是否由相同的字符组成的实例的详细内容...

  阅读:40次