Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

5
  • You may find enumerate() helpful in the future, which gives you both the element and the index. You're currently iterating the list three times and you only need to do it twice. Commented Mar 19, 2022 at 19:39
  • @Ben, thanks for the feedback but Iterating the list 3 times, you mean the time complexity here is 3N ? I don't think that is what you meant, yes, enumerate() does mean that I don't have to reference by index but that isn't the same as iterating thrice, right ? Commented Mar 19, 2022 at 19:43
  • Sorry, yes. You're right. You're iterating the length of the list thrice but actually only iterating the list twice (via the reference by index). Commented Mar 19, 2022 at 19:55
  • @Ben, to me "iterating" means I'm performing an O(N) operation, accessing a particular index, say word[i][j] which means the Jth character of the Ith word would still not be iterating, accessing the element at any given index is O(1), so I think I'm iterating the list once, in the for loop. Commented Mar 20, 2022 at 3:57
  • If you're accessing a given index N times at O(1) complexity, you've got youreslf an O(N) operation Dhiwakar. Otherwise, your solution would have constant complexity, which we know to be untrue - if the list has 1 billion items it'll take longer than if it has 10. There's no difference between accessing the list N times and iteration. Commented Mar 20, 2022 at 6:03