【发布时间】:2015-07-31 10:32:25
【问题描述】:
给定一个字符串输入:
aaabbbcccdddeee
输出应该是:
abcde
我是如何制作这个程序的?
给定一个输入字符串,我使用冒泡排序[现在不需要高效算法]按升序对该字符串的字符进行排序,然后删除重复项。
但问题是我不能将这种方法应用于像bbkjhiaa 这样的字符串,因为它改变了我原始字符串的顺序。
我只需要使用数组和字符串的概念来编写代码。
class SingleOccurence
{
static String singleoccurence(String p)
{
char current = ch[0];
boolean found = false; //what is the use of boolean variable
for (int i = 0; i < ch.length; i++)
{
if (current == ch[i] && !found)
{
found = true;
}
else if (current != ch[i])
{
System.out.print(current);
current = ch[i];
found = false;
}
}
System.out.println(current);
String s4=new String(ch);
return s4;
}
public static void main(String s[])
{
String s1=new String("qwwnniitootiinn");
String s6=SortingDemo.bubble1(s1);
String s5=singleoccurence(s6);
}
}
【问题讨论】:
-
您需要子字符串还是具有不同字符的字符串?就像@RealSkeptic 所说,你没有在这里做一个子字符串
-
@TheLostMind 是的,我需要一个不同字符的子字符串
-
@RealSkeptic 是的,我需要一个具有不同字符的子字符串
-
@javaCoderMakeSimple - 所以你需要所有字符串的唯一字符?。如果字符串是
aaabbbcccdddeee,那么 valid 子字符串将是aaabbb或bcccd等,而不是abcde -
@TheLostMind 我想那我就不需要子字符串了。我只想在字符串中打印 abcde 或 bkjhia 非重复元素