Check if string has all the letters of the alphabet

前端 未结 15 1291
终归单人心
终归单人心 2020-12-19 17:51

What would be the best logic to check all the letters in a given string.

If all the 26 letters are available in the provided string, I want to check that and perform

15条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-19 18:20

    You could iterate over your string for each letter of the alphabet you want to check. When you find the letter you are looking for, continue with the next. If you don’t, abort. Here is some pseudo-code:

    found = true;
    for (letter in letters) {
        if (!string.contains(letter)) {
            found = false;
            break;
        }
    }
    

    This way you do not need to store the information for each letter but you have to search the string again and again for each letter. What you end up using will depend on your requirements: should it be fast? Should it use little memory? Your decision. :)

提交回复
热议问题