2 回答
TA贡献1799条经验 获得超6个赞
使用 aString作为廉价地图来跟踪您已经看过哪些元音。另外,记下您遇到的连续辅音的数量。然后,当你击中一个你以前没有见过的元音前有一个辅音时,你就找到了答案。
public static void main(String[] args)
{
String s = "eeaaAOEacafu".toLowerCase();
int consCount = 0;
String seenVowels = "";
for(int i=0; i<s.length(); i++)
{
char c = s.charAt(i);
if("aeiou".indexOf(c) >= 0)
{
if(seenVowels.indexOf(c) == -1)
{
if(consCount == 1)
{
System.out.println("Result: " + c);
break;
}
seenVowels += c;
}
consCount = 0;
}
else consCount++;
}
}
输出:
Result: u
如果我们将 'unique' 理解为我们之前没有见过元音,则上述方法有效。如果元音在输入字符串中必须是唯一的,那么事情就有点复杂了。现在我们必须跟踪满足原始标准的每个元音,但如果我们随后遇到相同元音的另一个实例,则删除解决方案。
下面是一些代码来说明:
public static void main(String[] args)
{
String s = "afuxekozue".toLowerCase();
int consCount = 0;
String seenVowels = "";
String answer = "";
for(int i=0; i<s.length(); i++)
{
char c = s.charAt(i);
if("aeiou".indexOf(c) >= 0)
{
if(seenVowels.indexOf(c) == -1)
{
if(consCount == 1)
{
answer += c;
}
seenVowels += c;
}
else if(answer.indexOf(c) >= 0)
{
answer = answer.replaceAll(String.valueOf(c), "");;
}
consCount = 0;
}
else consCount++;
}
if(answer.length() > 0)
System.out.println("Result: " + answer.charAt(0));
}
输出:
Result: o
添加回答
举报