【问题标题】:How to remove duplicate words in string without using array?如何在不使用数组的情况下删除字符串中的重复单词?
【发布时间】:2017-12-26 03:09:31
【问题描述】:

有什么方法可以在不使用数组的情况下从字符串中删除重复的单词?

比如我有这句话“这个 java编程program”, 并且输出必须是“this java programming”。

我看到类似的删除重复问题,但它们都在使用数组。

【问题讨论】:

  • 我在您的句子中没有看到任何重复的单词。你的意思是重复的模式?
  • 即使有模式,为什么“编程”中的i 没有被删除(它出现在字符串的前面)?
  • 我在想“mm”,但是是的。是否有需要匹配的最少字符数?
  • 嗯,它应该是单词和子字符串。在“this is”中,“this”的“is”子串。与“编程”中的“程序”相同。我应该检查以空格分隔的单词。

标签: java string


【解决方案1】:

嗯,在 Java 中,字符串实际上是字符数组的对象包装器,主要增加了不变性。

因此,没有实际的方法可以不使用数组来完成您的任务。 即使您在代码实现中不使用任何直接数组创建的解决方案,您仍然会在幕后使用数组(如果您希望您的解决方案是最佳的)。

【讨论】:

  • 这在技术上是正确的,但不是特别有用。
  • @DM 一些解决方案探索只是错误的方式。如果你明白不这样做的原因,为什么要这样做?
  • 我同意这个思路。
  • 谢谢。我对java很陌生,这是我老师提出的一个挑战。
  • 当然问题是关于数组的直接使用,而不是间接使用。 String 可以是不使用数组的完全不透明的类。
【解决方案2】:

使用简单的方法从给定的字符串中删除重复的单词

package inffrd;

public class Q001 
{
    public static void removeduplicate(String input)
    {
        //convert the string to array by splitting it in words where space comes
        String[] words=input.split(" ");
        //put a for loop for outer comparison where words will start from "i" string
        for(int i=0;i<words.length;i++)
        {
            //check if already duplicate word is replaced with null
            if(words[i]!=null)
            {
                //put a for loop to compare outer words at i with inner word at j
                for(int j =i+1;j<words.length;j++)
                {
                    //if there is any duplicate word then make is Null
                    if(words[i].equals(words[j]))
                    {
                        words[j]=null;
                    }
                }
            }
        }
        //Print the output string where duplicate has been replaced with null
        for(int k=0;k<words.length;k++)
        {
            //check if word is a null then don't print it
            if(words[k]!=null)
            {
                System.out.print(words[k]+" ");
            }
        }
    }

    public static void main(String[] args) 
    {
        String s1="i am dinesh i am kumar";
        Q001.removeduplicate(s1);
    }
}

【讨论】:

    【解决方案3】:

    下面是更新后的代码@Han

    public class RemDup
    {
            public static void main ( String[] args )
            {
                String sentence = "this is java programming program progress";
                int max_word_length = sentence.length()/2;
                int min_word_length = 2;
                while(max_word_length>=min_word_length)
                {
                int si = 0;
                int ei = max_word_length;
                while ( ei<sentence.length() )
                {
                    int e=ei;
                    while ( e<sentence.length() )
                    {
                        int ind = sentence.indexOf ( sentence.substring ( si, ei ),e );
                        if ( ind!=-1 )
                        {
                            if(
                             sentence.substring(ind-1,ind).equals(" ")
                            &((ind+max_word_length)>=sentence.length()||
                            sentence.substring(ind+max_word_length,ind+max_word_length+1).equals(" "))
                            )
                            {
                            sentence = sentence.substring ( 0,ind ) +sentence.substring ( ind+max_word_length,sentence.length() );
                            }
                            e=ind+max_word_length;
    
                        }
                        else break;
                    }
    
    
                    si+=1;
                    ei+=1;
    
                }
                max_word_length--;
                }
                System.out.println(sentence);
            }
    
    }
    

    【讨论】:

    • 你应该更新你现有的答案而不是添加另一个
    【解决方案4】:

    下面的代码会帮助你:)

    public class RemDup
    {
        public static void main ( String[] args )
        {
            String sentence = "this is java programming program";
            int max_word_length = sentence.length()/2;
            int min_word_length = 2;
            while(max_word_length>=min_word_length)
            {
            int si = 0;
            int ei = max_word_length;
            while ( ei<sentence.length() )
            {
                int e=ei;
                while ( e<sentence.length() )
                {
                    int ind = sentence.indexOf ( sentence.substring ( si, ei ),e );
                    if ( ind!=-1 )
                    {
                        sentence = sentence.substring ( 0,ind ) +sentence.substring ( ind+max_word_length,sentence.length() );
                        e=ind+max_word_length;
                    }
                    else break;
                }
    
    
                si+=1;
                ei+=1;
    
            }
            max_word_length--;
            }
            System.out.println(sentence);
        }
    
    }
    

    【讨论】:

    • 嗨,“这是java编程程序”没问题。但是如果我们把“this is java programming program progress”这样更长的句子,它会输出“this java programmingess”
    • 因为 Progress 的前 5 个字母 - “Progr”是“Programming”前五个字母的副本。因此被删除。
    • 如果您也想处理这种情况。以下是更新后的代码
    猜你喜欢
    • 1970-01-01
    • 2023-03-09
    • 2011-10-20
    • 1970-01-01
    • 2021-11-14
    • 1970-01-01
    • 2011-12-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多