我正试图用Java编写这样一个算法我正在测试字符串输入“abaab”可以安全地假设字符串输入是小写的。
我不知道我的算法哪里出错了(它只为这个输入输出“a a”,而不是“ab”和“abaab”)。有什么想法吗?
static void SmallestAndLargestSubstring(String input) {
char[] vowels = { 'a', 'e', 'i', 'o', 'u' };
char[] cons = { 'b', 'c', 'd', 'f', 'g', 'h', 'j', 'k', 'l', 'm', 'n', 'p', 'q', 'r', 's', 't', 'v', 'w', 'x',
'y', 'z' };
char[] charArray = input.toLowerCase().toCharArray();
int startIndex = 0;
int shortEndIndex = 0;
int longEndIndex = 0;
int large = longEndIndex - startIndex;
int small = shortEndIndex - startIndex;
ArrayList<Integer> start = new ArrayList<Integer>();
ArrayList<Integer> end = new ArrayList<Integer>();
outerloop: for (int i = 0; i < charArray.length; i++) {
for (int z = 0; z < vowels.length; z++) {
if (charArray[i] == vowels[z]) {
startIndex = i;
start.add(startIndex);
if (longEndIndex - startIndex > large) {
large = longEndIndex - startIndex;
}
if(longEndIndex - startIndex <= large){
shortEndIndex=start.get(start.size()-1);
}
if (shortEndIndex - startIndex < small) {
small = shortEndIndex - startIndex;
}
if(shortEndIndex - startIndex >=small){
shortEndIndex=start.get(start.size()-1);
}
continue outerloop;
}
}
for (int j = 0; j < cons.length; j++) {
if (charArray[i] == cons[j]) {
longEndIndex = i;
shortEndIndex = i;
end.add(longEndIndex);
if (longEndIndex - startIndex > large) {
large = longEndIndex - startIndex;
}if(longEndIndex - startIndex <= large){
longEndIndex=end.get(end.size()-1);
}
if (shortEndIndex - startIndex < small) {
small = shortEndIndex - startIndex;
}
if(shortEndIndex - startIndex >=small) {
shortEndIndex=end.get(end.size()-1);
}
continue outerloop;
}
}
}
System.out.println(input.substring(startIndex, shortEndIndex));
System.out.println(input.substring(startIndex, longEndIndex));
}
最佳答案
我的解决办法是:最长的子串总是以第一个元音开始,以最后一个辅音结束。
最短的是,每次我读辅音的时候,我都会看看与前一个元音的距离,看是否更好。
你至少要读一个元音才能做任何事。
static void SmallestAndLargestSubstring(String input) {
char[] vowels = { 'a', 'e', 'i', 'o', 'u' };
char[] cons = { 'b', 'c', 'd', 'f', 'g', 'h', 'j', 'k', 'l', 'm', 'n', 'p', 'q', 'r', 's', 't', 'v', 'w', 'x',
'y', 'z' };
char[] charArray = input.toLowerCase().toCharArray();
int longStartIndex=0;
int shortStartIndex=0;
int shortEndIndex=0;
int longEndIndex=0;
boolean findVowel = false;
int bestStart = 0;
int bestEnd = 0;
int shortest =Integer.MAX_VALUE;
for (int i = 0; i < charArray.length; i++) {
for (int z = 0; z < vowels.length; z++) {
if (charArray[i] == vowels[z]) {
if (!findVowel){
// if this is the first vowel we see
longStartIndex = i;
shortStartIndex=i;
findVowel = true;
}
else {
shortStartIndex = i;
}
}
}
for (int j = 0; j < cons.length; j++) {
if (charArray[i] == cons[j]) {
if (findVowel){
// if we have seen any vowel, this consonant is useless
longEndIndex = i; // this one is always than the previous for the largest
shortEndIndex = i; // we have to check if this one is better or not
if (shortEndIndex-shortStartIndex<shortest){
bestStart = shortStartIndex;
bestEnd = shortEndIndex;
shortest = shortEndIndex-shortStartIndex;
}
}
}
}
}
System.out.println(input.substring(bestStart, bestEnd+1));
System.out.println(input.substring(longStartIndex, longEndIndex+1));
}
关于java - 从元音开始并以辅音结尾的最小和最大子字符串的算法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35749994/