Refactored all solutions
This commit is contained in:
@@ -6,18 +6,26 @@ import java.util.Scanner;
|
||||
|
||||
/*
|
||||
|
||||
There are approximately 26^2 possible combinations of alternating pairs of letters. Notice that this number remains constant and is not dependent on the length of our input String. This fact will help us achieve a linear O(n) runtime.
|
||||
Time Complexity: O(n) with just 1 traversal of our String
|
||||
Space Complexity: O(1)
|
||||
|
||||
We want to solve this problem with just 1 traversal of our String, so we solve all 26^2 subproblems simultaneously. We use two int[26][26] arrays to keep track of the 26^2 solutions.
|
||||
There are approximately 26^2 possible combinations of alternating pairs of letters.
|
||||
Notice that this number remains constant and is not dependent on the length of our
|
||||
input String. This fact will help us achieve a linear O(n) runtime.
|
||||
|
||||
We want to solve this problem with just 1 traversal of our String, so we solve all
|
||||
26^2 subproblems simultaneously. We use two int[26][26] arrays to keep track of the
|
||||
26^2 solutions.
|
||||
|
||||
As we iterate through our String, we update our two int[26][26] arrays as follows:
|
||||
- int[26][26] letter ---> This array is used to keep track of which of the alternating characters we saw last. To achieve this, for the current character "ch", we update the corresponding row (26 entries) and column (26 entries) with the (ASCII) value of "ch".
|
||||
- int[26][26] count ----> if we find that no solution exists for a pair of characters (which happens when the characters don't alternate), we store -1 in this array. Otherwise, we store he current maximum length of "s" for the pair of characters.
|
||||
- int[26][26] letter ---> This array is used to keep track of which of the alternating
|
||||
characters we saw last. To achieve this, for the current character "ch", we update the
|
||||
corresponding row (26 entries) and column (26 entries) with the (ASCII) value of "ch".
|
||||
- int[26][26] count ----> if we find that no solution exists for a pair of characters
|
||||
(which happens when the characters don't alternate), we store -1 in this array.
|
||||
Otherwise, we store he current maximum length of "s" for the pair of characters.
|
||||
|
||||
Our final answer is the maximum value in "int[26][26] count".
|
||||
|
||||
Time Complexity: O(n) with just 1 traversal of our String
|
||||
Space Complexity: O(1)
|
||||
Our final answer is the maximum value in our "int[26][26] count" array.
|
||||
|
||||
*/
|
||||
|
||||
@@ -25,7 +33,7 @@ public class Solution {
|
||||
|
||||
public static final int NUM_LETTERS = 26;
|
||||
|
||||
public static void main(String[] args) {
|
||||
public static void main(String [] args) {
|
||||
/* Save input */
|
||||
Scanner scan = new Scanner(System.in);
|
||||
int length = scan.nextInt();
|
||||
|
||||
Reference in New Issue
Block a user