zoukankan      html  css  js  c++  java
  • LintCode-乱序字符串

    题目描述:

      给出一个字符串数组S,找到其中所有的乱序字符串(Anagram)。如果一个字符串是乱序字符串,那么他存在一个字母集合相同,但顺序不同的字符串也在S中。

     注意事项

      所有的字符串都只包含小写字母

    样例

      对于字符串数组 ["lint","intl","inlt","code"]

      返回 ["lint","inlt","intl"]

     1 public class Solution {
     2     /**
     3      * @param strs: A list of strings
     4      * @return: A list of strings
     5      */
     6      
     7      public static 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     
    24     public List<String> anagrams(String[] strs) {
    25         // write your code here
    26     List<String> rStr = new LinkedList<String>();
    27         int length = strs.length;
    28         for(int i=0;i<length;++i){
    29             String str = strs[i];
    30             if(str!=null){
    31                 for(int j=i+1;j<length;j++){
    32                     String strToCom = strs[j];
    33                     if(strToCom != null){
    34                         if(anagram(str,strToCom)){
    35                             if(strs[i]!=null){
    36                                 rStr.add(str);
    37                             }
    38                             if(strs[j]!=null){
    39                                 rStr.add(strToCom);
    40                             }
    41                             
    42                             strs[i] = null;
    43                             strs[j] = null;
    44                         }
    45                     }
    46                     
    47                 }
    48             }
    49             
    50         }
    51         return rStr;
    52         
    53     }
    54 }
  • 相关阅读:
    禁用aspx页面的客户端缓存
    水晶报表的自动换行(转)
    ORACLE锁的管理
    同时使用有线和无线
    Oracle系统表的查询
    Oracle中临时表的深入研究
    我的My Life Rate
    [学习笔记]c#Primer中文版命名空间
    出差兰州·火车上
    [学习笔记]c#Primer中文版类设计、static成员、const和readonly数据成员
  • 原文地址:https://www.cnblogs.com/xiaocainiao2hao/p/5364472.html
Copyright © 2011-2022 走看看