如何删除字符串中的重复单词而不使用数组?

时间:2021-02-27 01:34:46

Is there any way we can remove duplicate words from a String without using Arrays?

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

For example, I have this sentence "this is java programming program", and the output have to be "this java programming".

例如,我有这句话“这是java编程程序”,输出必须是“这个java编程”。

I see similar remove duplicate problems but all of them are using arrays.

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

4 个解决方案

#1


0  

Below is the updated code @Han

以下是更新的代码@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);
        }

}

#2


2  

well, in Java, Strings are actually objects wrappers for character arrays which primarily add immutability.

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

so, there is no actual way to not use arrays for your task. even if you wrap your head around a solution which doesn't use any direct array creation in the code implementation, you will still be using arrays behind the scenes (if you want your solution to be optimal).

所以,没有实际的方法可以不为您的任务使用数组。即使你围绕一个在代码实现中没有使用任何直接数组创建的解决方案,你仍然会在幕后使用数组(如果你希望你的解决方案是最优的)。

#3


1  

Remove duplicate words from a given string using simple way

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

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);
    }
}

#4


0  

Below code will help you :)

下面的代码将帮助你:)

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);
    }

}

#1


0  

Below is the updated code @Han

以下是更新的代码@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);
        }

}

#2


2  

well, in Java, Strings are actually objects wrappers for character arrays which primarily add immutability.

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

so, there is no actual way to not use arrays for your task. even if you wrap your head around a solution which doesn't use any direct array creation in the code implementation, you will still be using arrays behind the scenes (if you want your solution to be optimal).

所以,没有实际的方法可以不为您的任务使用数组。即使你围绕一个在代码实现中没有使用任何直接数组创建的解决方案,你仍然会在幕后使用数组(如果你希望你的解决方案是最优的)。

#3


1  

Remove duplicate words from a given string using simple way

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

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);
    }
}

#4


0  

Below code will help you :)

下面的代码将帮助你:)

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);
    }

}