We use cookies to ensure you have the best browsing experience on our website. Please read our cookie policy for more information about how we use cookies.
Basically what he did (and actually I also did) was to build a square array of size 26 (imagine columns a,b,c,d.. rows also a,b,c,d). Also a similar array initialized to zeros.
For each letter in the string, check the corresponding row AND column for that letter. If the cell is empty or it is the other value (i.e. if checking columns take the row id and viceversa), update the cell with the char of the string being analized and add a +1 to the parallel count matrix.
If it's the same value as the char being checked it means that between both instances of that char the other one isn't there (and I also mark the count matrix with -1).
Basically you're checking for alternations. For example if checking with 'a' you find an 'a' already in the cell for say row 'd' it means that between the previous 'a' and this one there has been no 'd' in the string so therefore 'a' and 'd' are NOT a valid pair of chars.
When you've finished traversing the string, simply find and print the biggest value in the count matrix.
So as to not give it all away, there's an edge case to cater for (Test case #1).
Cookie support is required to access HackerRank
Seems like cookies are disabled on this browser, please enable them to open this website
Two Characters
You are viewing a single comment's thread. Return to all comments →
Basically what he did (and actually I also did) was to build a square array of size 26 (imagine columns a,b,c,d.. rows also a,b,c,d). Also a similar array initialized to zeros. For each letter in the string, check the corresponding row AND column for that letter. If the cell is empty or it is the other value (i.e. if checking columns take the row id and viceversa), update the cell with the char of the string being analized and add a +1 to the parallel count matrix. If it's the same value as the char being checked it means that between both instances of that char the other one isn't there (and I also mark the count matrix with -1).
Basically you're checking for alternations. For example if checking with 'a' you find an 'a' already in the cell for say row 'd' it means that between the previous 'a' and this one there has been no 'd' in the string so therefore 'a' and 'd' are NOT a valid pair of chars.
When you've finished traversing the string, simply find and print the biggest value in the count matrix. So as to not give it all away, there's an edge case to cater for (Test case #1).