Friday, December 19, 2014

Longest Substring with At Most Two Distinct Characters

This problem can be solved in a similar approach as the Longest Substring Without Repeating Characters. Just for reference, I post the solution of this one here.


public class LongestSubstring {
    public int lengthOfLongestSubstring(String s) {
        if (s == null || s.length() == 0)
            return 0;
        HashSet hs = new HashSet();
        int leftBound = 0;
        int maxLength = 0;
        for (int i = 0; i < s.length(); i++)
        {
            if (!hs.contains(s.charAt(i)))
            {
                hs.add(s.charAt(i));
                maxLength = Math.max(maxLength, i - leftBound + 1);
                continue;
            }
            while (leftBound < i && s.charAt(leftBound) != s.charAt(i))
            {
                hs.remove(s.charAt(leftBound));
                leftBound++;
            }
            leftBound++;
        }
        return maxLength;
        
    }
}

Similarly, I use a moving window approach. A hash set is used to store the two characters of the substring. If the hash set doesn't contain the character, we add it, and we keep tracking the length of the substring that contains the characters in the hash set. The leftBound tracks the start point of the substring.
The tricky part comes when the third character comes up (char3). We need to move our window now! There are two possible cases:

1. s.charAt(leftBound) equals the character before the third character (let's say char2) and
2.  s.charAt(leftBound) doesn't equal to the character before the third character (char1).

The problem is, we only want to remove char1 and preserve char2, because char2 and char3 may form a longer substring.
For the second case, I use String.contains() method to check if the substring from charAt(leftBound) to char3 contains char1, if it does, we move the leftBound toward right.
For the first case, I just move the leftBound until I find the first char1. Then we can apply the solution for the second case again!

Based on my limited test case, it gives me the correct answer. However, since I haven't found enough convincible solutions online, I am open to any corrections.


import java.util.HashSet;


public class LongestSubstring {
 public int longestSubstring(String s)
 {
  if (s == null || s.length() == 0)
   return 0;
  HashSet hs = new HashSet();
  int leftBound = 0;
  int maxLength = 0;
  String sub = "";
  for (int i = 0; i < s.length(); i++)
  {
   if (!hs.contains(s.charAt(i)))
   {
    if (hs.size() < 2)
     hs.add(s.charAt(i));
    else
    {
     while (s.charAt(leftBound) == s.charAt(i - 1))
      leftBound++;
     CharSequence tmp = String.valueOf(s.charAt(leftBound));
     hs.remove(s.charAt(leftBound));
     while (s.substring(leftBound, i).contains(tmp))
      leftBound++;
     hs.add(s.charAt(i));
    }
   }
   maxLength = Math.max(maxLength, i - leftBound + 1);
   if (maxLength > sub.length())
    sub = s.substring(leftBound, i + 1);
  }
  System.out.println(sub);
  return maxLength;
 }

}

Update 2015 - 03 - 03:
I realize if all characters in the string are letters, then we can use a bit mask instead of a set, this may save some space.

public int longestSubstring3(String s){
  if (s == null || s.length() == 0)
         return 0;
  s = s.toLowerCase();
  int mask = 0;
  int size = 0;
  int max = 0;
  int leftBound = 0;
  for (int i = 0; i < s.length(); i++){
   if(size < 2){
    mask |= (1 << (s.charAt(i) - 'a'));
    size++;
   } else{
    if((mask & (1 << (s.charAt(i) - 'a'))) == 0){
     while(leftBound < i && s.charAt(leftBound) == s.charAt(i - 1))
      leftBound++;
     String tmp = String.valueOf(s.charAt(leftBound));
     mask &= (~(1 << (s.charAt(leftBound) - 'a')));
     while(leftBound < i && s.substring(leftBound, i).contains(tmp))
      leftBound++;
     mask |= (1 << (s.charAt(i) - 'a'));
    }
   }
   max = Math.max(max, i - leftBound + 1);
  }
  return max;
 }

1 comment:

  1. The development of artificial intelligence (AI) has propelled more programming architects, information scientists, and different experts to investigate the plausibility of a vocation in machine learning. Notwithstanding, a few newcomers will in general spotlight a lot on hypothesis and insufficient on commonsense application. IEEE final year projects on machine learning In case you will succeed, you have to begin building machine learning projects in the near future.

    Projects assist you with improving your applied ML skills rapidly while allowing you to investigate an intriguing point. Furthermore, you can include projects into your portfolio, making it simpler to get a vocation, discover cool profession openings, and Final Year Project Centers in Chennai even arrange a more significant compensation.


    Data analytics is the study of dissecting crude data so as to make decisions about that data. Data analytics advances and procedures are generally utilized in business ventures to empower associations to settle on progressively Python Training in Chennai educated business choices. In the present worldwide commercial center, it isn't sufficient to assemble data and do the math; you should realize how to apply that data to genuine situations such that will affect conduct. In the program you will initially gain proficiency with the specialized skills, including R and Python dialects most usually utilized in data analytics programming and usage; Python Training in Chennai at that point center around the commonsense application, in view of genuine business issues in a scope of industry segments, for example, wellbeing, promoting and account.

    ReplyDelete