2015-02-11 62 views

回答

2

Regular expressions可能适合你。像

Pattern p = Pattern.compile("([a-zA-Z])*([a-zA-Z])\\2([a-zA-Z])*"); 
Matcher m = p.matcher("zoo"); 
System.out.println(m.matches()); 

东西只需添加一个循环,试图在文件中的每一个字,如果m.matches() == true - 删除。

顺便说一句,如果你像这样输入

+0

将[Oo]更改为[a-zA-Z]。他说重复字符不重复o。 – 2015-02-11 19:06:36

+0

感谢您的更正 – miraclefoxx 2015-02-11 19:08:05

+0

没问题的小家伙。 +1为你的努力:) – 2015-02-11 19:09:12

0

这这不会工作,是使用正则表达式和流API的例子:

package demo; 

import java.util.Arrays; 
import java.util.List; 
import java.util.stream.Collectors; 

public class Demonstration 
{ 
    public static void main(String[] args) 
    { 
     List<String> input = Arrays.asList(// 
      new String[] {"a", "bb", "ccc", "ded", "ff", "ghi", "jkll"}); 

     // Prints [a, ded, ghi] 
     System.out.println(removeWordsWithRepetitiveCharacters(input)); 
    } 

    private static List<String> removeWordsWithRepetitiveCharacters(List<String> words) 
    { 
     return words.stream() // 
      .filter(word -> !word.matches(".*(\\w)\\1+.*")) // 
      .collect(Collectors.toList()); 
    } 
} 
相关问题