0

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'.

lss555
  • 43
  • 6
  • 1
    Can you give a few sample inputs/output for this? – Daniel Hao Feb 26 '23 at 16:15
  • str2 is undefined (at the very bottom), you just have a comment what it could be – maraca Feb 26 '23 at 18:15
  • Have you solved the problem yet ? – Dennis Quan Feb 27 '23 at 03:07
  • you have answer on your problem [here](https://stackoverflow.com/questions/19601903/find-the-longest-substring-in-alphabetical-order) and remember that the english alphabet in python already sorted (alphabet[i] < alphabet[i + 1]) but if you want to make your own idea using a list of the alphabetical order I would suggest to make a dictionary of your alphabet instead of a normal array, if with one dictionary it doesn't work for you, use 2. for more info on bidirectional dictionary [here](https://stackoverflow.com/questions/3318625/how-to-implement-an-efficient-bidirectional-hash-table) – JackRaBeat Feb 27 '23 at 09:21
  • @JackRaBeat: Note that this is tagged `javascript` and not `python`. – Scott Sauyet Feb 27 '23 at 18:38
  • You might want to check out [`String.prototype.charCodeAt`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/charCodeAt). It should help you with the specific question, although I might suggest a different approach altogether, looping over/folding the characters of the string, keeping track as we go of the max start/stop values and the current start value and running index, updating index at every character, updating the others as you extend an alphabetic streak and as your streak becomes the maximum. – Scott Sauyet Feb 27 '23 at 21:05

0 Answers0