I'm trying to solve an algo/data structure problem, here's the problem/question.
Problem: given a string of letters find the longest contiguous substring in alphabetical order and return the max substring length.
Question: how can I set this up so I find givenArray[currentChar] in the alphabet array, i.e. if a 'g' is in the given string find 'g' in the alphabet array so I can see if the next letter is 'h' and so forth.
In my code I have written an array of the alphabet and with a pointer going through the given array, (the given string is split into an array so I can compare it to the alphabet array).
// example string
let str2 = 'abcdggrisxzy';
const alphaString = function(s) {
const alphabet = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'];
let p1 = 0;
let max = 0;
let array = s.split('');
if (s.length <= 1) return s.length;
for (let p2 = p1; p2 < array.length; p2++) {
console.log(array[p2], 'given string')
let currentChar = array[p2];
// below is where I log alphabet at currentChar, I'm trying to find the given array's current character in the alphabet array.
// but it is coming up as undefined
//I've tried using an object as I believe that would be a hash table and optimal but I just get errors in return when I convert the alphabet array to an object.
console.log(alphabet[currentChar], 'alphabet');
break;
}
};
alphaString(str2);
*edited below
examples: If you were given the string 'abcqrstz' you would want to return 4 as the longest contiguous substring in alphabetical order is 4 being 'qrst'. Another example could be 'abcbyyxz' you would want to return 3 as the longest would be 'abc'.