Verifying an Alien Dictionary

Sdílet
Vložit
  • čas přidán 3. 03. 2020
  • For business inquiries email partnerships@k2.codes SOCIAL
    ----------------------------------------------------------------------------------------------------------------
    Instagram: / kevinnaughtonjr
    Twitter: / kevinnaughtonjr
    Patreon: / kevinnaughtonjr
    GitHub: github.com/kdn251
    My Desk Setup
    Desk - bit.ly/3jfY195
    Chair - amzn.to/2O9TM3r
    Monitor - amzn.to/3rcSHGa
    Webcam - amzn.to/2NUmwgi
    Desktop - amzn.to/3tiySPL
    Laptops - amzn.to/3aRoN3Z
    iPad - amzn.to/2LlJzzJ
    Keyboard - amzn.to/3jfbxdd
    Mouse - amzn.to/36ElWtT
    Wrist Rest - amzn.to/3trrHF4 (pls don't buy this)
    Mouse Pad - amzn.to/2Myz2lt
    Microphone - amzn.to/3atNyTA
    Lamp - amzn.to/3jjfZYp
    Headphones - amzn.to/3tvr0KU (new model)
    Headphone Hook - amzn.to/3tr8uTC
    Blue Light Glasses - amzn.to/3cDVUdK
    Wireless Charger - amzn.to/39LY1uu
    Keyboard cable - amzn.to/2O5p2R5
    Mic arm - amzn.to/3cECZj8
    Audio interface - amzn.to/36HdWIi
    Cloudlifter - amzn.to/36VO6kf
    Laptop dock - amzn.to/2O2DsBw
    Motherboard - amzn.to/3rkiWuA
    Solid state - amzn.to/3rk5vuo
    CPU cooler - amzn.to/3tnwwPA
    CableMod - amzn.to/3tqbtM8
    CPU - amzn.to/3auG1ns
    Power supply - amzn.to/3trsAxo
    RAM - amzn.to/39JZcuf
    Designing Data-Intensive Applications - amzn.to/2YK4ek1
    Clean Code - amzn.to/3txqfB5
    Meditations - amzn.to/3cDa4fi
    MUSIC
    ----------------------------------------------------------------------------------------------------------------
    look at me. by ☽shinji☾
    leetcode.com/problems/verifyi...
  • Věda a technologie

Komentáře • 144

  • @KevinNaughtonJr
    @KevinNaughtonJr  Před 4 lety +8

    SMASH LIKE, SUBSCRIBE, AND LEAVE A COMMENT LETTING ME KNOW WHAT PROBLEM YOU WANT ME TO MAKE A VIDEO ON NEXT
    instagram: instagram.com/kevinnaughtonjr/
    twitter: twitter.com/kevinnaughtonjr

  • @SmallLab129
    @SmallLab129 Před 4 lety +185

    Its actually a bit simpler. You don't need to compare every word to every other word. Just compare your current word to the next word. If any i+1 is lexicographically before i, then they're not in the right order.

    • @KevinNaughtonJr
      @KevinNaughtonJr  Před 4 lety +16

      Nice!

    • @vk1618
      @vk1618 Před 4 lety

      @@KevinNaughtonJr In that case, how was your submission faster than 100% of Java submissions?

    • @vk1618
      @vk1618 Před 4 lety +1

      How was his submission faster than 100% of Java submissions? Are we missing something in the approach that you propose?

    • @vivekvitthalraopatil2775
      @vivekvitthalraopatil2775 Před 3 lety +12

      @@vk1618 Don't trust the speed of execution mentioned by Leetcode. It's mostly wrong.

    • @user-gk3iq3lm7t
      @user-gk3iq3lm7t Před 3 lety

      @@vk1618 public boolean isAlienSorted(String[] words, String order) {
      int [] ary = new int[26];
      for(int i = 0; i < 26; i++){
      ary[order.charAt(i) - 'a'] = i;
      }
      int j = 0, index = 0, length = words.length;
      while(j + 1 < length){
      if(ary[words[j].charAt(index) - 'a'] == ary[words[j + 1].charAt(index) - 'a']){
      int min = (words[j].length() < words[j + 1].length()) ?
      words[j].length() : words[j + 1].length();
      for(index = 0; index < min; index++){
      if(ary[words[j].charAt(index) - 'a'] > ary[words[j + 1].charAt(index) - 'a'])
      return false;
      }
      }else if(ary[words[j].charAt(index) - 'a'] > ary[words[j + 1].charAt(index) - 'a'])
      return false;
      if(index + 1

  • @urmichakravarty387
    @urmichakravarty387 Před 3 lety +8

    @Kevin ..Just saw this question today for my first round at Facebook with just a tiny difference of getting a String array of alphabets as the order parameter.I would have never done this question unless I came across your video title.I feel grateful.Thank you so much.

    • @KevinNaughtonJr
      @KevinNaughtonJr  Před 3 lety +2

      That's awesome super happy I was able to help :) If you need help with more questions like this check out the interviewing service I created thedailybyte.dev/?ref=kevin I'd recommend joining the annual tier if you can!

    • @daleprather3026
      @daleprather3026 Před 3 lety +2

      Did you get the job?

  • @kamertonaudiophileplayer847

    Algorithm is really simple, as you found first letter not matching a letter is same position of another word then use the order table and then the task done.

  • @alfredoluco189
    @alfredoluco189 Před 3 lety +1

    Hi Kevin, there's a way that can be solved with dynamic programming? I'm trying to optimize it with some technique

  • @konradonbal2020
    @konradonbal2020 Před 4 lety +2

    Thanks to you I am getting better and better in solving these problems. Couple months ago I was merely a copy machine of your solutions. Today I am able to sort most of these problem on my own, then i am comparing our solutions ;)

    • @KevinNaughtonJr
      @KevinNaughtonJr  Před 4 lety +1

      Konrad Onbal that’s amazing Konrad nice work keep it up!!! :)

  • @sahilsashi5818
    @sahilsashi5818 Před 3 lety +5

    Hey Kevin, I just wanted to know why you compare each word with every consequent word in the dictionary. If we keep comparing i and i + 1th word lexicographically I think we can reach the ordering because in each step we make sure that i and i + 1 are correct. So we won't have to compare i with i + 2, i + 3 and so on. As going by order comparing i , i + 1 then i + 2 and i + 3 will make sure the dictionary is aligned with the previous value. What do you think?

  • @cluster027
    @cluster027 Před 4 lety

    Very useful thank you! Could you make a video on 373 please? Having trouble wrapping my head around it!

  • @soubarnobanerjee8257
    @soubarnobanerjee8257 Před 4 lety

    Okay one question here...
    We are going with words[i] and words[i+1] and comparing lexicographically. But for words[i], we also have to compare with each letter of that word. For that another loop is used inside.
    Does that complete execution in linear time? I think it will result in O(n^2). Please correct me if I'm wrong

  • @thepinkcodon
    @thepinkcodon Před 2 lety

    I didn't quite get the purpose of the break statement. Why doe not traverse all throught the lengths of the ith / jth word? Wouldn't the loop break right after comparing the first characters from ith and jth words if we allow the break statement?

    • @thepinkcodon
      @thepinkcodon Před 2 lety

      Oh I figured it out. When we see that the second word's current character has more value than first word's current character, we immediately know that these two are in correct places in the given list. So our next step is to break the inner for loop, so that we can compare the THIRD word with the first word.

  • @gurdoman
    @gurdoman Před 4 lety +3

    I guess you could optimize it by comparing letter in the jth position of the ith and ith + 1 word while i is less than words.length -1, you avoid an unnecessary loop

  • @nchen
    @nchen Před 4 lety +2

    Is there a reason why you use an array rather than a HashMap

  • @b0606089
    @b0606089 Před 4 lety +23

    N*N*M is not required. Just compare i and i+1 each time and do in N*M.
    Edit :
    Oops. Seems like lot more people pointed the same.

  • @KGTube
    @KGTube Před 3 lety

    I had the same problem with my Interview with FB. Solved with the best optimal solution!
    Just solving this problem will not be enough you need to solve the next Middle-level problem to pass Onsite interview

  • @amlanch
    @amlanch Před 4 lety

    Excellent explanation

  • @anirudhlok2047
    @anirudhlok2047 Před 3 lety +5

    Amazing explanation bro!
    Don't get irked by these ppl who are suggesting they have a better solution and all those things. It takes massive efforts for you to put such content for free! Appreciate it!

  • @arp690ve
    @arp690ve Před 4 lety +2

    Sorry I'm new at this. Why we need to include the "- 'a' " part in the function?

    • @MradulShrivastava0786
      @MradulShrivastava0786 Před 4 lety

      get ASCII value for alphabets in order array

    • @AnkushPuriSDE
      @AnkushPuriSDE Před 4 lety +3

      "-a" to make sure that resultant integer lies b/w the array indices (0 to 25)

  • @niharikapatil902
    @niharikapatil902 Před 2 lety

    I'm a little confused so "hello" "leetcode" o is coming before c right? So that should be false??

  • @sandipbhaumik
    @sandipbhaumik Před 4 lety

    Can you please describe the time complexity over here?

  • @indiantruly5253
    @indiantruly5253 Před 2 lety

    Hi Kevin
    Can this be solved using Trie?

  • @arpitdixit9619
    @arpitdixit9619 Před 4 lety +1

    Thank you again, kevin.

  • @dev-skills
    @dev-skills Před 3 lety +3

    2:16 - you don't need to compare e's as from the first character itself we know they are lexicographically sorted. If the preceding characters are same only then you need to do the comparison of subsequent characters.

  • @Kaan-qr5pv
    @Kaan-qr5pv Před 4 lety

    What I don't understand in the first example is that, 'o' in hello comes after 'c' in leetcode but the first example outputs true? How does that work?

    • @TheMediocreDev
      @TheMediocreDev Před 4 lety

      Basically, the case passes/fails when you come across the first pair of distinct characters. The rest is irrelevant. Basically, the loops breaks after the first iteration since H comes before L. Not sure why Kevin kept iterating through the rest of the characters.

  • @javabk
    @javabk Před 4 lety

    Can we go through the array once, create a matching int array based on the ‘weight’ of each word, and check of its sorted ?

  • @dormelamed5974
    @dormelamed5974 Před 4 lety

    you rock dude !

  • @gatoradelight
    @gatoradelight Před 4 lety +2

    Great video! One question though, why did you decide to use an array to store the alphabet as opposed to using a hashmap? If you used a hashmap its still O(1) lookup, and you can simply index it directly with the char value as opposed to doing (letter - 'a');

    • @michaeldang8189
      @michaeldang8189 Před 4 lety +5

      HashMap and Array Indexing has the same O(1) access time, however, Array Indexing is clean and direct, no odd cases. HashMap is not as clean, it can have collision at times. So Array Indexing is always faster though not dimensional faster.
      HashMap can lookup the value with the character directly, though it is convenient, internally it is doing the math similar to (letter - 'a'). So speed wise, HashMap can only be slower if not on par with Array Indexing.

  • @ShivamPandey-wd9ro
    @ShivamPandey-wd9ro Před 4 lety +2

    Hi Kevin!
    I found this explanation really simple to understand. I myself reached to a similar solution with a hashmap instead of the array. However, I thought since the time complexity would be always greater than n^2, it would be a bad solution. I wanted to ask, how does one decide whether the brute force solution is good enough? Is there a way to do so before pressing the submit button in the coding round of the company?

    • @galanoth17
      @galanoth17 Před rokem

      If you can't think of any other solution, then you have to think that brute force is the best one. If you can think of another then just implement the other one. There is no way if knowing if you can't see the other solution

  • @BRBallin1
    @BRBallin1 Před 4 lety

    Solution summary: use a word_index to track your current word and char_index to track the current character index you are comparing with words that start with the same character(s). if order.index(words[word_index][char_index]) < order.index(words[word_index+1][char_index]) OR word_index == len(words[word_index]) -> word_index++ & char_index = 0 and keep checking while word_index is within range of the list of words

  • @hemanthn436
    @hemanthn436 Před 3 lety

    At 9:35 , How can you break whole loop by comparing the very first element buddy?
    can please highlight the meaning it??

    • @PankajKumar6493
      @PankajKumar6493 Před 2 lety

      I don't know his code ran successfully and that too with "faster than 100%" when there is an O(n) algorithm to solve ths. Fake

  • @harinaaraayans3453
    @harinaaraayans3453 Před 4 lety

    great video man. can u please do more graph based problems?

  • @shashigatadi
    @shashigatadi Před 4 lety +1

    In case of “hello” and “leetcode” example ‘c’ comes before ‘o’ in order string. I am confused, please clarify.

    • @TheMediocreDev
      @TheMediocreDev Před 4 lety

      Basically, the case passes/fails when you come across the first pair of distinct characters. The rest is irrelevant. Basically, the loops breaks after the first iteration since H comes before L. Not sure why Kevin kept iterating through the rest of the characters.

    • @srimanyadagiri7831
      @srimanyadagiri7831 Před 4 lety

      @thaxcutioner#1 , where does he iterate over the rest of the letters it breaks out of the inner most for loop and goes to the next word and as there is no next word, we return true.

  • @michaeldang8189
    @michaeldang8189 Před 4 lety +1

    Why would you need the i, j double loop? If word 1 is less than word 2, word 2 is less than word 3, that implies word 1 is less than word 3, right?

  • @sandipbhaumik
    @sandipbhaumik Před 4 lety

    Please advise on facebook software engineering interview preparation.

  • @Homelander007
    @Homelander007 Před 4 lety +1

    Thanks Kevin . Please do next video no : 1192
    Critical Connections in a Network

  • @ashishaswal7396
    @ashishaswal7396 Před 3 lety

    Can anyone tell me "our first check".. the break in line 15.. wouldn't it just check for first characters and then break?

    • @stuti251
      @stuti251 Před 2 lety

      Yes, it means the condition satisfied, should compare next words

  • @aymanebokhamy6492
    @aymanebokhamy6492 Před rokem

    I have a queestion. Why does he keep substracting the character 'a' in the loops?

  • @soymaxxing
    @soymaxxing Před 4 lety +3

    Whats your thoughts on learning python specifically for leetcoding?

    • @KevinNaughtonJr
      @KevinNaughtonJr  Před 4 lety

      I think that's a good call unless you feel comfortable in another language already

  • @juliahuanlingtong6757
    @juliahuanlingtong6757 Před 3 lety

    I don't think the if (...) break within the foor loop is necessary. Doesn't it?

  • @PeterParker-sy9bp
    @PeterParker-sy9bp Před 4 lety

    Hi Kevin, great work. One little correction tho, you don't need to compare every word to every other one. It is sufficient to compare only words[i] to words[i+1], since if the array is sorted until the i'th element, we only need to check the next one. This would yield O(n*m) instead of O(n^2*m).

  • @skr6199
    @skr6199 Před 4 lety +2

    Could you do leetcode problem 163 Missing Ranges? Thank you

  • @utsavshrestha7162
    @utsavshrestha7162 Před 2 lety

    We don't need to compare all the words in the list with every other word that comes after. We can just compare 1st and 2nd, then 2nd and 3rd and so on. Wouldn't that be better than this?

  • @arthamsreenivas8858
    @arthamsreenivas8858 Před 4 lety +15

    Kevin, i guess here 2 loops i,j is not required, because you compare in one loop i, i+1 words comparison ?

    • @KevinNaughtonJr
      @KevinNaughtonJr  Před 4 lety +4

      yep you're right, nice!

    • @ambidextrousTrades
      @ambidextrousTrades Před 3 lety

      I think 2 loops are required since you need to compare the letters according to their dictionary. So one loop to go through the words and the other loop to go through the letters:

  • @AmanSharma-vb5jl
    @AmanSharma-vb5jl Před 2 lety

    Great sir

  • @zeeshanmohammad6296
    @zeeshanmohammad6296 Před 4 lety +7

    dear kevin, the 'break' statement breaks the for-loop running k times as soon as it compares the first letter. so it only verified the lexicographical order of the first k character of each word and for rest of the k-1 characters it just breaks out of the for loop. isn't this an anomally? its the first check right to break away right after the very first letter compared and found to be correct?

    • @elenenify
      @elenenify Před 2 lety

      yes. azzzzzz always comes before baaaaa

  • @casual_chess
    @casual_chess Před 4 lety

    In the first example, why u compared other characters of strings after first mismatch?
    Wasn't comparison of the first character was enough?

    • @KevinNaughtonJr
      @KevinNaughtonJr  Před 4 lety

      yes it was I think I was trying to just explain the logic more

    • @TheMediocreDev
      @TheMediocreDev Před 4 lety

      @@KevinNaughtonJr Except that follows a different logic b/c for the first example "o" (hellO) comes after c (leetCode) thus that case would fail

  • @Uber_handle
    @Uber_handle Před 3 lety +2

    The - a is throwing me off big time.

    • @pashapa11
      @pashapa11 Před 3 lety

      Use a hashmap to store the letters and the index of them instead. I dont get the -'a' either. wont even run when i have that

  • @jinu870
    @jinu870 Před 2 lety

    What about using a trie

  • @metronadsouza506
    @metronadsouza506 Před 3 lety

    I just did not understand the task scheduler at all...its not the code that i did not get ...but the question itself ...i tried to read it several time ...but cannot get it

  • @Od253
    @Od253 Před 4 lety

    Never seen or heard of this problem. Thanks for sharing!

  • @algorithmimplementer415

    One question - is M the max letter count of all the words? I think it’s the min. As you take k as the min.

    • @KevinNaughtonJr
      @KevinNaughtonJr  Před 4 lety +1

      M (in the runtime) would be the maximum size that an individual word can be because in the worst case we'll have to run through all those M characters for each of the words

  • @Resideline
    @Resideline Před 3 lety +1

    letter o comes after the letter C, so my script is saying its False on the first example.

    • @NateGUMBC20
      @NateGUMBC20 Před 2 lety +1

      i was confused there too. the question only cares about the first great or smaller than case to decide. So in all the examples, it decides at the first instances a greater or smaller comparison

  • @dev-skills
    @dev-skills Před 3 lety

    13:51 - Memory usage could be better. No need to create an array of alphabet you can just compare using order.indexOf(words[i].charAt[j])

  • @MyPlaylist1ist
    @MyPlaylist1ist Před 4 lety

    Shouldn't the 'break' at line 15 be a 'continue' statement?

    • @KevinNaughtonJr
      @KevinNaughtonJr  Před 4 lety +3

      If the kth character in the ith word is less than the kth character in the jth word then we know those two words are in lexicographical order so we can stop there

    • @MehmetSafakAlbayrak
      @MehmetSafakAlbayrak Před 4 lety +2

      No, you already verified that iChar is < jChar so the lexicographical order is correct. You dont need to compare the rest.
      Think about ""abaxy" vs "abcde". As soon as you verified that a < c on 3rd characters, you need to break.

    • @KevinNaughtonJr
      @KevinNaughtonJr  Před 4 lety +2

      @@MehmetSafakAlbayrak Right which is what my code is doing :)

    • @dhamaka_2
      @dhamaka_2 Před 4 lety

      @@MehmetSafakAlbayrak but if loop breaks after verifying a

  • @SergiuMunteanu
    @SergiuMunteanu Před 4 lety

    I would translate the words from alien to English and then use the language string comparison to compare i and i + 1

  • @algorithmimplementer415

    Requesting for Find Max distance from closest person in a row.

  • @vishalvatsalya1439
    @vishalvatsalya1439 Před 4 lety

    Please solve minimum window subsequence. Thanks.

  • @blissfull7648
    @blissfull7648 Před 2 lety

    This is so hard

  • @janjaso
    @janjaso Před 3 lety

    WHY MINUS 'A' ???????

  • @ManishTiwari-or8zt
    @ManishTiwari-or8zt Před 4 lety

    please make video on Amazon Oops questions

  • @samvid74
    @samvid74 Před 2 lety

    How can somebody solve this in 20 minutes?

  • @adamgf1
    @adamgf1 Před 2 lety

    Good video but you do not need to compare every word with every other word. You just need to compare the current word with the next word (I think.) Also a hash map that stores the character index for each letter in the alien dictionary makes this a little easier. So map['h'] = 0 for example. Then you are just comparing indices not letters.

  • @dev-skills
    @dev-skills Před 3 lety

    You just need to compare adjacent words to check if they are lexicographically sorted., no need to compare a word with each of the other words.

  • @reelinvestor
    @reelinvestor Před 4 lety +1

    Could you make a video on Amazon's most FAQ on OOP, create api to search file in dictionary

  • @VinayKumar-rq5kd
    @VinayKumar-rq5kd Před 4 lety

    please do dynamic problems

  • @darod6098
    @darod6098 Před 4 lety +4

    Can you do more Facebook most asked questions pleaase?

  • @faaaszoooom6778
    @faaaszoooom6778 Před 3 lety

    Kevin, lexicographic comparison is transitive. You only need to compare to the next word.

  • @nitreall
    @nitreall Před 3 lety

    The modularity of the code isn't good.

  • @pierre1360
    @pierre1360 Před 2 lety

    Why the "- 'a' "

  • @duedares
    @duedares Před 3 lety

    I came here for the Hard version -_-
    Click-baited FTW

  • @iamabean
    @iamabean Před 4 lety

    I watch this video 3 weeks before an interview for facebook

    • @KevinNaughtonJr
      @KevinNaughtonJr  Před 4 lety

      Nice :)

    • @iamabean
      @iamabean Před 4 lety

      @@KevinNaughtonJr i am stressed . I am a data engineer. Asides from algorihm i have to prepared for sql as well

    • @KevinNaughtonJr
      @KevinNaughtonJr  Před 4 lety +1

      @@iamabean If you need help preparing for your interview check out my Patreon page: www.patreon.com/KevinNaughtonJr

    • @iamabean
      @iamabean Před 4 lety

      @@KevinNaughtonJr i am watching ur Facebook playlist and pray hahaha

  • @aryamankukal1056
    @aryamankukal1056 Před 2 lety

    lmao just realized this is an easy question

  • @The_Gurugram_Voyager
    @The_Gurugram_Voyager Před 2 lety

    Why is this rated hard in gfg😖😖

  • @MehmetSafakAlbayrak
    @MehmetSafakAlbayrak Před 4 lety +3

    You only need to compare i and i+1 words Don't need the inner for loop with j

    • @KevinNaughtonJr
      @KevinNaughtonJr  Před 4 lety

      I think you do cuz otherwise you can't guarantee that the current word is lexicographically sorted with respect to every other word

    • @MehmetSafakAlbayrak
      @MehmetSafakAlbayrak Před 4 lety +1

      @@KevinNaughtonJr czcams.com/video/qSbJZWENtX4/video.html
      We need to verify for every word[i] , it is lexicographically sorted with respect to word[i+1] which we can conclude that words[i] is lexicographically sorted with respect to all the remaining.

    • @kevinnaughton6038
      @kevinnaughton6038 Před 4 lety

      @@MehmetSafakAlbayrak ahhh gotcha :)

    • @bhopalsingh4102
      @bhopalsingh4102 Před 4 lety +1

      @@KevinNaughtonJr but if every word is sorted with respect to its next word, then definitely whole array is sorted

    • @KevinNaughtonJr
      @KevinNaughtonJr  Před 4 lety +1

      @@bhopalsingh4102 right

  • @ahkim93
    @ahkim93 Před 4 lety

    ["apap","app"]
    "abcdefghijklmnopqrstuvwxyz" i can't seem to understand how apap is less than app. Please explain.

    • @joepena330
      @joepena330 Před 4 lety

      Think of a dictionary.
      a comes before p, so the words are technically the same up until "ap", because right after one has an "a", the other has another "p", and since a comes before p, therefore appap is the first word.
      A dictionary (like a real world, alphabetical one, not a Python object) only cares about alphabetical order, not length.

  • @fionamatu4996
    @fionamatu4996 Před 3 lety

    I think we can solve this in O(N*M) time where N is number of words and M is average number of characters in a word , if we put the characters in order as keys and the respective position(weight) they occur on in the string in a hashmap e.g. h-> 0,l-> 1. We then iterate through the words string picking each word and adding up the weight of the word with the help of the hashmap. Then have a count variable that stores the total weight of the previous word and compares it to the weight of the current word we are looking at. If our current word is less in weight then we return false.If not, we continue the process till the end and return true.

  • @MoFields
    @MoFields Před 3 lety

    You don't need to compare every letter mate, you keep comparing while letters are the same still in current and next word.

  • @hemanthn436
    @hemanthn436 Před 3 lety

    mapmp;
    for(int i=0;i

  • @vineethsai1575
    @vineethsai1575 Před 3 lety +1

    This can be done in n*m times as others in the comments have suggested, Here is python solution for the same.
    class Solution:
    def isAlienSorted(self, words: List[str], order: str) -> bool:
    alphabet = [None] * 26;
    for i in range(0, len(order)):
    alphabet[ord(order[i]) - ord('a')] = i;
    for i in range(len(words) - 1):
    word1 = words[i]
    word2 = words[i + 1]
    minCharLength = min(len(word1), len(word2))
    for j in range(minCharLength):
    word1Char = word1[j]
    word2Char = word2[j]
    if(alphabet[ord(word1Char) - ord('a')] < alphabet[ord(word2Char) - ord('a')]):
    break
    elif(alphabet[ord(word2Char) - ord('a')] < alphabet[ord(word1Char) - ord('a')]):
    return False
    elif(j == minCharLength - 1 and len(words[i]) > len(words[i + 1])):
    return False
    return True

  • @bharathkalyans
    @bharathkalyans Před 2 lety

    class Solution {
    public boolean isAlienSorted(String[] words, String order) {
    int n = words.length;
    HashMap map = new HashMap();
    for (int i = 0; i < order.length(); i++) map.put(order.charAt(i), i);
    for (int i = 0; i < n - 1; i++)
    if (!sortedAlienly(words[i], words[i + 1], map)) return false;
    return true;
    }
    private boolean sortedAlienly(String s, String t,
    HashMap map) {
    int m = s.length(), n = t.length();
    int i = 0, j = 0;
    while (i < m && j < n) {
    char ss = s.charAt(i);
    char tt = t.charAt(j);
    if (ss != tt) {
    return map.get(ss)

  • @kismisharariya7843
    @kismisharariya7843 Před 2 lety

    Words: ["hello", "leetcode"]
    Order: "hlabcdefgijkmnopqrstuvwxyz"
    Does `o` in hello comes before `c` in leetcode ? how are they sorted? Can anyone help me to understand ?

    • @stuti251
      @stuti251 Před 2 lety

      you don't need to go till c. you compare h to l -> since h < l you broke the condition check and continue