LintCode-两个字符串是变位词

题目描述:

  写出一个函数 anagram(s, t) 去判断两个字符串是否是颠倒字母顺序构成的

样例

  给出 s="abcd",t="dcab",返回 true

 
 1 public class Solution {
 2     /**
 3      * @param s: The first string
 4      * @param b: The second string
 5      * @return true or false
 6      */
 7     public boolean anagram(String s, String t) {
 8         if(s.length() != t.length())
 9             return false;
10         else{
11             for(int i=0;i<t.length();i++){
12                 if(s.indexOf(t.charAt(i))!=-1){
13                     int j = s.indexOf(t.charAt(i));
14                     s = s.substring(0, j)+s.substring(j+1);
15                 }
16                 else{
17                     return false;
18                 }
19             }
20             return true;
21         }
22     }
23 };
原文地址:https://www.cnblogs.com/xiaocainiao2hao/p/5364442.html