使用 Java 中字符串单词中排序的字母创建新字符串

发布于 2024-11-14 07:07:55 字数 219 浏览 4 评论 0原文

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

假设我有这样的东西

String theWord = "Hello World";

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

德赫洛尔w

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

提前致谢

How do I create a String with alphabetical order letters taken from another String?

Let's say I have something like this

String theWord = "Hello World";

How do I compute the new String to make it look like"

dehllloorw

Which is theWord but sorted character by character in alphabetical order.

Thanks in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

慕巷 2024-11-21 07:07:55
char[] chars = theWord.toCharArray();
Arrays.sort(chars);
String newWord = new String(chars);
char[] chars = theWord.toCharArray();
Arrays.sort(chars);
String newWord = new String(chars);
得不到的就毁灭 2024-11-21 07:07:55

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

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

Agreed, I stole the solution. But apparently, it's also important to strip whitespace and make everything lowercase:

char[] array = theWord.replaceAll("\\s+", "").toLowerCase().toCharArray();
Arrays.sort(array);
System.out.println(new String(array));
榆西 2024-11-21 07:07:55

上述解决方案都不是特定于区域设置的,因此我采用了这个解决方案,它效率不高,但效果很好。

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();
    }

None of the above solutions are locale specific , therefore I came with this solution, it is not efficient , but it works very well..

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();
    }
等你爱我 2024-11-21 07:07:55
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];
}
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];
}
妄断弥空 2024-11-21 07:07:55
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);

    }

}
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);

    }

}
岁月静好 2024-11-21 07:07:55
char[]  chars2  = b.toLowerCase().toCharArray();
Arrays.sort(chars1);
String  Ns1   = new String(chars1);
char[]  chars2  = b.toLowerCase().toCharArray();
Arrays.sort(chars1);
String  Ns1   = new String(chars1);
清风疏影 2024-11-21 07:07:55

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

All the solutions were O(nlogn) as they are sorting the array. Instead we can take array[26] and do this in O(n).
after you covert it into lowercase and remove spaces which are O(n),
int[] ar=new int[26];
for(char c:s.toCharArray()) ar[c-'a']++;
and then form the required string O(n).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文