Substrings of Size Three with Distinct Characters
Count how many runs of three neighbouring characters in a string contain no repeated character.
Call a string good when no character appears in it twice. Given a string ss
Example 1
Output: 1
Explanation: the substrings of length 3 are "xyz", "yzz", "zza" and "zaz", and only "xyz" is good.
Example 2
Output: 4
Explanation: the good ones are "abc", "bca", "cab" and "abc" again, at a later position.
The Idea
Imagine reading a word through a small card with a hole in it, just wide enough to show three letters. You start at the left edge, read the three letters in the hole, then slide the card one letter to the right and read again. Every three-letter substring passes through the hole exactly once.
Do that with "xyzzaz"xyzyzzzzazaz
Look at the picture below. Each row is one position of the hole, drawn as a blue frame, and the orange boxes are the letters that repeat.
The row labelled "xyz: all different" is the only good one, so the answer is 1. In the rows "yzz: z twice" and "zza: z twice", the repeated letters stand side by side. The last row, "zaz: first and last are both z", is different: the two z's are not neighbours, with the a between them.
So how do we tell, quickly, that three letters are all different? The last row shows that checking neighbours is not enough, because the first and last letter can match too. Three letters make exactly three pairs: first and second, second and third, first and third. If all three pairs differ, the substring is good.
The hole in the card has a usual name in code. A fixed-width frame that moves along an array or string one step at a time is called a sliding window, and here the window is always three characters wide.
Solution Approach
Turning that idea into code means settling two things: how to visit every window of three characters, and how to check one window.
Visiting every window
Which positions does the window take? A window can be named by the index of its last character. The first window ends at index 2, and the last one ends at the last index of the string, so a loop over iii - 2i - 1i
Moving the window one step at a time means the windows overlap, and that is what the problem asks for. In Example 2, "abc"
A simpler but slower way is to cut each substring out with s.substring(i - 2, i + 1)s
Checking three characters
How should one window be checked? Read the three characters into abca != bb != ca != czaz
Longer windows (optional)
What if the window were k
Java Solution
The method countGoodSubstrings
- First, set
to 0. It holds the number of good windows found so far.count - Slide the window with one loop, where
is the index of the window's last character. It starts at 2 and runs to the end ofi .s - Inside the loop, read the window's three characters into
(ata ),i - 2 (atb ) andi - 1 (atc ).i - If
,a != b andb != c all hold, add 1 toa != c .count - When the loop ends, return
.count
The main"aba""abcabc""aaaa""ab"
public class SubstringsOfSizeThreeWithDistinctCharacters {
public static int countGoodSubstrings(String s) {
int count = 0;
for (int i = 2; i < s.length(); i++) {
char a = s.charAt(i - 2); // the window is s[i-2 .. i]
char b = s.charAt(i - 1);
char c = s.charAt(i);
if (a != b && b != c && a != c) {
count++;
}
}
return count;
}
public static void main(String[] args) {
System.out.println(countGoodSubstrings("xyzzaz"));
System.out.println(countGoodSubstrings("aababcabc"));
System.out.println(countGoodSubstrings("aba"));
System.out.println(countGoodSubstrings("abcabc"));
System.out.println(countGoodSubstrings("aaaa"));
System.out.println(countGoodSubstrings("ab"));
}
}
Running it prints:
1
4
0
4
0
0
The 1 and the 4 at the top match the two examples. Look at the third line: "aba"a != c"aba""xyzzaz"zaz
The fourth line is 4 because "abcabc"abcbcacababc"aaaa"aaa"ab"
Complexity
The work is one pass of the window over the string, with a fixed amount of work at each position.
- Time: O(n), because the window takes n - 2 positions in a string of n characters, and at each one the loop reads three characters and makes at most three comparisons.
- Space: O(1), because the method keeps only
,count and the three charactersi ,a andb , and never copies the string or builds substrings.c
When the window has no fixed width and must grow and shrink to avoid repeats, keeping a count of each character in the window takes over: see