{"pageProps":{"post":{"title":"944. Delete Columns to Make Sorted","excerpt":"You are given an array of n strings strs, all of the same length. For example, strs = [\"abc\", \"bce\", \"cae\"]. You want to delete the columns that are not sorted lexicographically. Return the number of columns that you will delete.","featuredImage":{"url":"https://media.graphassets.com/hMPYrFStXjUlOZVXjMgl"},"author":{"name":"Md Jawad Noor Asif","bio":"A humble student of knowledge","photo":{"url":"https://media.graphassets.com/nky5NwkETTKtpNnikulF"}},"createdAt":"2023-01-03T04:02:49.371301+00:00","slug":"delete-columns-to-make-sorted","content":{"raw":{"children":[{"type":"heading-one","children":[{"text":"Problem:"}]},{"type":"paragraph","children":[{"text":"You are given an array of "},{"code":true,"text":"n"},{"text":" strings "},{"code":true,"text":"strs"},{"text":", all of the same length."}]},{"type":"paragraph","children":[{"text":"The strings can be arranged such that there is one on each line, making a grid. For example, "},{"code":true,"text":"strs = [\"abc\", \"bce\", \"cae\"]"},{"text":" can be arranged as:"}]},{"type":"code-block","children":[{"text":"abc\nbce\ncae\n"}]},{"type":"paragraph","children":[{"text":"You want to "},{"bold":true,"text":"delete"},{"text":" the columns that are "},{"bold":true,"text":"not sorted lexicographically"},{"text":". In the above example (0-indexed), columns 0 ("},{"code":true,"text":"'a'"},{"text":", "},{"code":true,"text":"'b'"},{"text":", "},{"code":true,"text":"'c'"},{"text":") and 2 ("},{"code":true,"text":"'c'"},{"text":", "},{"code":true,"text":"'e'"},{"text":", "},{"code":true,"text":"'e'"},{"text":") are sorted while column 1 ("},{"code":true,"text":"'b'"},{"text":", "},{"code":true,"text":"'c'"},{"text":", "},{"code":true,"text":"'a'"},{"text":") is not, so you would delete column 1."}]},{"type":"paragraph","children":[{"text":"Return "},{"text":"the number of columns that you will delete","italic":true},{"text":"."}]},{"type":"paragraph","children":[{"text":" "}]},{"type":"paragraph","children":[{"bold":true,"text":"Example 1:"}]},{"type":"code-block","children":[{"bold":true,"text":"Input:"},{"text":" strs = [\"cba\",\"daf\",\"ghi\"]\n"},{"bold":true,"text":"Output:"},{"text":" 1\n"},{"bold":true,"text":"Explanation:"},{"text":" The grid looks as follows:\n cba\n daf\n ghi\nColumns 0 and 2 are sorted, but column 1 is not, so you only need to delete 1 column.\n"}]},{"type":"paragraph","children":[{"bold":true,"text":"Example 2:"}]},{"type":"code-block","children":[{"bold":true,"text":"Input:"},{"text":" strs = [\"a\",\"b\"]\n"},{"bold":true,"text":"Output:"},{"text":" 0\n"},{"bold":true,"text":"Explanation:"},{"text":" The grid looks as follows:\n a\n b\nColumn 0 is the only column and is sorted, so you will not delete any columns.\n"}]},{"type":"paragraph","children":[{"bold":true,"text":"Example 3:"}]},{"type":"code-block","children":[{"bold":true,"text":"Input:"},{"text":" strs = [\"zyx\",\"wvu\",\"tsr\"]\n"},{"bold":true,"text":"Output:"},{"text":" 3\n"},{"bold":true,"text":"Explanation:"},{"text":" The grid looks as follows:\n zyx\n wvu\n tsr\nAll 3 columns are not sorted, so you will delete all 3.\n"}]},{"type":"paragraph","children":[{"text":" "}]},{"type":"paragraph","children":[{"bold":true,"text":"Constraints:"}]},{"type":"bulleted-list","children":[{"type":"list-item","children":[{"type":"list-item-child","children":[{"code":true,"text":"n == strs.length"}]}]},{"type":"list-item","children":[{"type":"list-item-child","children":[{"code":true,"text":"1 <= n <= 100"}]}]},{"type":"list-item","children":[{"type":"list-item-child","children":[{"code":true,"text":"1 <= strs[i].length <= 1000"}]}]},{"type":"list-item","children":[{"type":"list-item-child","children":[{"code":true,"text":"strs[i]"},{"text":" consists of lowercase English letters."}]}]}]},{"type":"heading-one","children":[{"text":"Solution:"}]},{"type":"heading-one","children":[{"text":"Intuition"}]},{"type":"paragraph","children":[{"text":"Iterate over each character of the strings, check if the current character ascii value is more than previous string's this index's character's ascii value"}]},{"type":"heading-one","children":[{"text":"Approach"}]},{"type":"paragraph","children":[{"text":"In outer loop, iterate over the length of the string. In inner loop iterate over the strings of the list. For each index postion, check if current string's current characters's (character in index position) ascii value is less than previous one. If found, increment "},{"code":true,"text":"count"},{"text":" variable and break the inner loop, else keep checking for next string.\nReset "},{"code":true,"text":"last_char"},{"text":" variable after checking all the strings for current index"}]},{"type":"heading-one","children":[{"text":"Complexity"}]},{"type":"bulleted-list","children":[{"type":"list-item","children":[{"type":"list-item-child","children":[{"type":"paragraph","children":[{"text":"Time complexity:\n"},{"text":"O(n∗len(n))","italic":true}]}]}]},{"type":"list-item","children":[{"type":"list-item-child","children":[{"type":"paragraph","children":[{"text":"Space complexity:\n"},{"text":"O(1)","italic":true}]}]}]}]},{"type":"heading-one","children":[{"text":"Code"}]},{"type":"class","children":[{"type":"code-block","children":[{"text":"class Solution:\r\n def minDeletionSize(self, strs: List[str]) -> int:\r\n count = 0\r\n for i in range(len(strs[0])):\r\n last_char = 0\r\n for str in strs:\r\n if ord(str[i]) < last_char:\r\n count += 1\r\n break\r\n else:\r\n last_char = ord(str[i])\r\n return count\r\n\r\n\r"}]}],"className":"python"},{"type":"paragraph","children":[{"text":""}]}]}},"categories":[{"name":"LeetCode","slug":"leetcode"},{"name":"Explanation","slug":"explanation"},{"name":"Python","slug":"python"}]}},"__N_SSG":true}