【问题标题】:Creating new String with sorted letters from a String word in Java用Java中字符串单词的排序字母创建新字符串
【发布时间】:2011-09-10 03:05:25
【问题描述】:

如何使用取自另一个字符串的字母顺序创建一个字符串?

假设我有这样的东西

String theWord = "Hello World";

如何计算新字符串以使其看起来像“

dehlloorw

这是单词,但按字母顺序逐个字符排序。

提前致谢

【问题讨论】:

  • 到目前为止您尝试过什么?您认为这样做的核心逻辑是什么?

标签: java string character alphabetical sorted


【解决方案1】:

【讨论】:

    【解决方案2】:
    char[] chars = theWord.toCharArray();
    Arrays.sort(chars);
    String newWord = new String(chars);
    

    【讨论】:

    • 另一个由 Stack Overflow 完成的家庭作业,用时 4 分钟。
    • @Paul 这是我工作保障计划的一部分。通过阻止未来的开发者思考他们的问题,我也阻止他们改进和消除潜在的竞争:-)
    【解决方案3】:

    同意,我偷了解决方案。但显然,去除空格并将所有内容变为小写也很重要:

    char[] array = theWord.replaceAll("\\s+", "").toLowerCase().toCharArray();
    Arrays.sort(array);
    System.out.println(new String(array));
    

    【讨论】:

      【解决方案4】:

      上述解决方案都不是特定于语言环境的,因此我提出了这个解决方案,它效率不高,但效果很好..

      public static String localeSpecificStringSort(String str, Locale locale) {
      
              String[] strArr = new String[str.length()];
      
              for(int i=0;i<str.length();i++){
                  strArr[i] =  str.substring(i,i+1);
              }
              Collator collator = Collator.getInstance(locale);
              Arrays.sort(strArr, collator);
              StringBuffer strBuf = new StringBuffer();
              for (String string : strArr) {
                  strBuf.append(string);
              }
              return strBuf.toString();
          }
      

      【讨论】:

        【解决方案5】:
        char[] arr = new char[theWord.length()];
        for(int i=0;i<theWord.length;i++)
        {
            arr[i]=theWord.charAt(i);
        }
        for(int i=0;i<theWord.length();i++)
          for(int j=0;j<theWord.length();j++)
        {
            char temp = arr[i];
            arr[i]=arr[j];
            arr[j]=temp;
        }
        int size=theWord.length();
        theWord="";
        for(int i=0;i<size;i++)
        {
            theWord+=arr[i];
        }
        

        【讨论】:

          【解决方案6】:
          import java.util.Arrays;
          import java.util.Scanner;
          
          // re arrange alphabets in order
          public class RearrangeAlphabets {
          
              @SuppressWarnings("resource")
              public static void main(String[] args) {
          
                  String theWord;
                  Scanner in = new Scanner(System.in);
          
                  System.out.println("Enter a string to rearrange: \n");
          
                  theWord = in.nextLine();
                  int length = theWord.length();
                  System.out.println("Length of string: "+length);
                  char[] chars=theWord.toCharArray();
                  Arrays.sort(chars);
                  String newWord=new String(chars);
          
                  System.out.println("The Re-Arranged word : "+newWord);
          
              }
          
          }
          

          【讨论】:

            【解决方案7】:
            char[]  chars2  = b.toLowerCase().toCharArray();
            Arrays.sort(chars1);
            String  Ns1   = new String(chars1);
            

            【讨论】:

              【解决方案8】:

              所有解决方案都是 O(nlogn),因为它们正在对数组进行排序。相反,我们可以使用 array[26] 并在 O(n) 中执行此操作。 在将其转换为小写并删除 O(n) 的空格后, int[] ar=新的 int[26]; for(char c:s.toCharArray()) ar[c-'a']++; 然后形成所需的字符串 O(n)。

              【讨论】:

                猜你喜欢
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 1970-01-01
                • 2015-05-07
                • 1970-01-01
                相关资源
                最近更新 更多