#Contains method question

1 messages · Page 1 of 1 (latest)

paper depot
#
class Solution {
    public int lengthOfLongestSubstring(String s) {
        String sub = "";
        String longest = "";
        for(int i = 0; i < s.length(); i++) {
            if(sub.contains(s.charAt(i))) { //THIS LINE OF CODE PERTAINS TO MY QUESTION
                if(sub.length() > longest.length()) {
                    longest = sub;
                }
                sub = s.charAt(i) + "";
            } else {
                sub  += s.charAt(i);
            }
        }
        return longest.length();
    }
}

In the code above, why am I getting an error in the if statement that I have commented? I want to see if a certain character exists in a string. i.e, does a certain letter exist in the string.

A fix I found was to rewrite the if statement as -

if(sub.indexOf(s.charAt(i)) != -1) {

But I am genuinely not sure why we can't see if a specific character is present in a string or not with the contains method.

safe trellisBOT
# paper depot ```java class Solution { public int lengthOfLongestSubstring(String s) { ...

Detected code, here are some useful tools:

Formatted code
class Solution {
  public int lengthOfLongestSubstring(String s) {
    String sub = "";
    String longest = "";
    for (int i = 0; i < s.length(); i++) {
      if (sub.contains(s.charAt(i))) {
        //THIS LINE OF CODE PERTAINS TO MY QUESTION
        if (sub.length() > longest.length()) {
          longest = sub;
        }
        sub = s.charAt(i) + "";
      }
      else {
        sub += s.charAt(i);
      }
    }
    return longest.length();
  }
}
#

<@&987246399047479336> please have a look, thanks.

safe trellisBOT
#

While you are waiting for getting help, here are some tips to improve your experience:

Code is much easier to read if posted with syntax highlighting and proper formatting.

If nobody is calling back, that usually means that your question was not well asked and hence nobody feels confident enough answering. Try to use your time to elaborate, provide details, context, more code, examples and maybe some screenshots. With enough info, someone knows the answer for sure.

Don't forget to close your thread using the command </help-thread close:1027500463647621170> when your question has been answered, thanks.

#

Here is an AI assisted attempt to answer your question 🤖. Maybe it helps! In any case, a human is on the way 👍. To continue talking to the AI, you can use </chatgpt:1108714622413963314>.

#

The code provided is incomplete, as the for loop is not closed. However, based on the provided code, it seems like the goal is to find the length of the longest substring without repeating characters in a given string.

To achieve this, you can use a sliding window approach. Here's an example implementation:

class Solution {
    public int lengthOfLongestSubstring(String s) {
        int maxLength = 0;
        int start = 0;
        int end = 0;
        Set<Character> uniqueChars = new HashSet<>();
        
        while (end < s.length()) {
            char currentChar = s.charAt(end);
            
            if (!uniqueChars.contains(currentChar)) {
                uniqueChars.add(currentChar);
                maxLength = Math.max(maxLength, end - start + 1);
                end++;
            } else {
                uniqueChars.remove(s.charAt(start));
                start++;
            }
        }
        
        return maxLength;
    }
}

In this implementation, we maintain a sliding window defined by start and end indices. We also use a Set called uniqueChars to keep track of the unique characters within the current substring.

We iterate through the string using the end index. If we encounter a character that is not in uniqueChars, we add it to the set and update maxLength if necessary. We then increment end.

If we encounter a character that is already in uniqueChars, we remove the character at index start from the set and increment start. This allows us to slide our window and continue searching for longer substrings without repeating characters.

Finally, we return the value of maxLength, which represents the length of the longest substring without repeating characters in the given string.

iron edge
#

What error do you get?

paper depot
iron edge
#

contains takes a String (or CharSequence)

#

you can use sub.indexOf(somechar) != -1 to check if a single character exists in a string

#

indexOf will return -1 when the character is not found

austere vapor
#

.contains() doesn't accept type int

paper depot
#

Thanks! solved