211. String Permutation【LintCode by java】

來源:互聯網
上載者:User

標籤:HERE   TE   數組   length   eth   end   line   https   red   

Description

Given two strings, write a method to decide if one is a permutation of the other.

Example

abcd is a permutation of bcad, but abbe is not a permutation of abe

解題:遇到過類似的題目,比較簡單的方法是,把字串轉化為字元數組,然後排序、比較每一位的數是否相等。這樣做效率比較低,代碼如下:

 1 public class Solution { 2     /** 3      * @param A: a string 4      * @param B: a string 5      * @return: a boolean 6      */ 7     public boolean Permutation(String A, String B) { 8         // write your code here 9         if(A.length() != B.length())10             return false;11         char[]a = A.toCharArray();12         char[]b = B.toCharArray();13         Arrays.sort(a);14         Arrays.sort(b);15         for(int i = 0; i < a.length; i++){16             if(a[i] != b[i])17                return false;18         }19         return true;20     }21 }

也可以通過雜湊表來做:如果每個元素的個數都相等,並且總個數相同,則字串完全相等,參考:51212982。

211. String Permutation【LintCode by java】

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.