Click here to Skip to main content
16,020,378 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
the array is
Java
String[] words = {"Apple", "cheese", "Mississippi", "beer", "hand", "sandwich", "kadabra"};



my solutions were like this
1-
Java
public static String LongestWord() {
        String[] words = {"Apple", "cheese", "Mississippi", "beer", "hand", 
        "sandwich", "kadabra"};

        String maxLength = words[0];
        String newString = "";


        for (String str : words) {

            for (int i = 0; i < str.length(); i++) {
                {
                  for (int j=i+1; j<str.length(); j++){
 if="" (str.charat(i)="=" str.charat(j)){
 newstring+="str.charAt(i);
}
}

(newstring.length()=""> maxLength.length()) {
                maxLength = newString;
            }
        }

        return maxLength;
    } 


the problem with the above solution is, it doesn't go through all characters of string it starts with "i" and there may be a duplicate before "i" and it won't detect it so it doesn't work.

2-
Java
public static String LongestWord() {
        String[] words = {"Apple", "cheese", "Mississippi", "beer", "hand", "sandwich", "kadabra"};
        String maxLength = words[0];
        String newString = "";
        for (String str : words) {
            for (int i = 0; i < str.length(); i++) {
                {                  
for (int j=str.length()-1; j>=0; j--){
                      if (str.charAt(i) == str.charAt(j)){                          newString+=str.charAt(i);
                      }
                    }
                }
            }
            if (newString.length() > maxLength.length()) {
                maxLength = newString;
            }
        }
        return maxLength;
    } 


this one is closer to work but it still has a problem and that is when for example "i=0" and "j" reaches 0 then it will be equal because it's the same letter but not a duplicate, and i tried many others too, but nothing worked.

What I have tried:

I tried many solutions, but none of them compared all the characters to each other.
Posted
Updated 23-Jun-19 8:23am
v4
Comments
Patrice T 21-Jun-19 11:09am    
You need to show your work.
CPallini 21-Jun-19 16:06pm    
How do you count duplicates? For instance What is the expected result for 'Mississippi'?
hiwa doski 22-Jun-19 2:43am    
it's 4 "s"
CPallini 22-Jun-19 3:14am    
Why not the 'i's? There are also four 'i's. Or have you to count the letters which comes in adjacent pairs?
hiwa doski 23-Jun-19 4:55am    
i don't think it matters, as long as there is no word with more than 4 duplicated characters.

We are more than willing to help those that are stuck: but that doesn't mean that we are here to do it all for you! We can't do all the work, you are either getting paid for this, or it's part of your grades and it wouldn't be at all fair for us to do it all for you.

So we need you to do the work, and we will help you when you get stuck. That doesn't mean we will give you a step by step solution you can hand in!
Start by explaining where you are at the moment, and what the next step in the process is. Then tell us what you have tried to get that next step working, and what happened when you did.

But I'll give you a hint: sort the strings so they become "Aelpp", "ceeehs", iiiimppssss", "beer", ...
Then counting repeated characters becomes trivial!
 
Share this answer
 
Comments
hiwa doski 21-Jun-19 7:30am    
noh I'm not getting paid and it's not homework, it's just a problem I found online and want to know the solution for it, and i tried many solutions and i spent much time on it
my solutions were like this
1-
public static String LongestWord() {
        String[] words = {"Apple", "cheese", "Mississippi", "beer", "hand", 
        "sandwich", "kadabra"};

        String maxLength = words[0];
        String newString = "";


        for (String str : words) {

            for (int i = 0; i < str.length(); i++) {
                {
                  for (int j=i+1; j<str.length(); j++){
="" if="" (str.charat(i)="=" str.charat(j)){
="" newstring+="str.charAt(i);
" }
="" }

="" (newstring.length()=""> maxLength.length()) {
                maxLength = newString;
            }
        }

        return maxLength;
    } 

// the problem with the above solution is, it doesn't go through all characters of string it starts with "i" and there may be a duplicate before "i" and it won't detect it so it doesn't work.

2- Show   Expand   Copy Code
public static String LongestWord() {        String[] words = {"Apple", "cheese", "Mississippi", "beer", "hand", "sandwich", "kadabra"};        String maxLength = words[0];        String newString = "";        for (String str : words) {            for (int i = 0; i < str.length(); i++) {                {                  for (int j=str.length()-1; j>=0; j--){                      if (str.charAt(i) == str.charAt(j)){                          newString+=str.charAt(i);                      }                    }                }            }            if (newString.length() > maxLength.length()) {                maxLength = newString;            }        }        return maxLength;    }  

//this one is closer to work but it still has a problem and that is when for example "i=0" and "j" reaches 0 then it will be equal because it's the same letter but not a duplicate, and i tried many others too, but nothing worked.
Patrice T 22-Jun-19 4:40am    
Use Improve question to update your question.
So that everyone can pay attention to this information.
hiwa doski 23-Jun-19 4:55am    
okay, will do that.
Maciej Los 23-Jun-19 16:54pm    
5ed!
In a straightforward approach you order the letters inside the words (e.g. mississippi becomes iiiimppssss), then the task becomes trivial.


[update]

Try the following program:
Java
import java.util.*;

class Duplicates
{
  public static int count(String s)
  {
    char a[] = s.toCharArray();
    Arrays.sort(a);

    int cnt = 1;
    int max_cnt = 1;
    char cur = a[0];
    for (int n = 1; n < a.length; ++n)
    {
      if (cur != a[n])
      {
        if (max_cnt < cnt)
        {
          max_cnt = cnt;
        }
        cur = a[n];
        cnt = 1;
      }
      else
        ++cnt;
    }

    return max_cnt >= cnt ? max_cnt : cnt;
  }

  public static void main(String args[])
  {

    String[] words = {"Apple", "cheese", "Mississippi", "beer", "hand", "sandwich", "kadabra"};

    for (String s : words)
      System.out.printf( "%s: %d\n", s, Duplicates.count(s));

  }
}

[/update]
 
Share this answer
 
v2
Comments
hiwa doski 23-Jun-19 14:24pm    
Thank you so much for your help.
CPallini 23-Jun-19 15:10pm    
You are welcome.
Maciej Los 23-Jun-19 16:54pm    
5ed!
CPallini 24-Jun-19 7:39am    
Thank you very much, Maciej.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900