Checking if 2 strings contain the same characters?

前端 未结 10 954
孤城傲影
孤城傲影 2020-12-10 04:49

Is there a way to check if two strings contain the same characters. For example,

abc, bca -> true
aaa, aaa -> true
aab, bba -> false
abc, def ->          


        
10条回答
  •  没有蜡笔的小新
    2020-12-10 05:23

    Consider creating a signature for a given String. Using count and character.

    a-count:b-count:c-count:.....:z-count: (extend for upper case if you want ).

    Then compare the signature. This should scale better for very large Strings.

    As a shortcut, check the length. If they are not matching, return false anyway.

提交回复
热议问题