Java 删除字符串中每个单词的每个重复项
我必须编写一个代码,删除字符串中单个单词中重复的每个字符。 换言之,如果它被重复,那就无关紧要了。 我只能使用数组和字符串(没有哈希、sb.append、其他函数..) 例如: 输入=推荐专业|输出=推荐 我必须删除重复的内容和字母本身(在同一个单词中重复)。Java 删除字符串中每个单词的每个重复项,java,arrays,string,duplicates,Java,Arrays,String,Duplicates,我必须编写一个代码,删除字符串中单个单词中重复的每个字符。 换言之,如果它被重复,那就无关紧要了。 我只能使用数组和字符串(没有哈希、sb.append、其他函数..) 例如: 输入=推荐专业|输出=推荐 我必须删除重复的内容和字母本身(在同一个单词中重复)。 有什么想法吗?帮忙 使用string.Split方法在word中拆分字符串。使用空格字符分割字符串,您将得到该字符串的一个片段数组,对于数组中的每个元素,请保存单词的长度,并尝试使用a对单词中的每个字符进行迭代,每次使用一个计数器,告诉您
有什么想法吗?帮忙 使用string.Split方法在word中拆分字符串。使用空格字符分割字符串,您将得到该字符串的一个片段数组,对于数组中的每个元素,请保存单词的长度,并尝试使用a对单词中的每个字符进行迭代,每次使用一个计数器,告诉您是否重复并删除该字符。请尝试以下代码:
public static void main(String[] args){
String str = "recommend that profession";
String output ="";
// Separating word from string
String[] splited = str.split("\\s+");
for(int i=0;i<splited.length;i++){
//removing repeated char and its original from each word
String word = splited[i];
for(int j = 0;j<word.length();j++){
char c = word.charAt(j);
for(int k =j+1;k<word.length();k++){
char d = word.charAt(k);
String deleted = String.valueOf(d);
// Checking if the characters are repeated
if(c==d){
word = word.replaceAll(deleted,"");
}
}
}
// Concatenating the main string
output+=word+" ";
}
System.out.println(output);
}
publicstaticvoidmain(字符串[]args){
String str=“推荐该职业”;
字符串输出=”;
//从字符串中分离单词
String[]splited=str.split(\\s+);
对于(int i=0;i