{"id":7856,"date":"2020-12-27T00:17:51","date_gmt":"2020-12-27T08:17:51","guid":{"rendered":"https:\/\/zxi.mytechroad.com\/blog\/?p=7856"},"modified":"2020-12-27T00:21:02","modified_gmt":"2020-12-27T08:21:02","slug":"leetcode-1704-determine-if-string-halves-are-alike","status":"publish","type":"post","link":"https:\/\/zxi.mytechroad.com\/blog\/string\/leetcode-1704-determine-if-string-halves-are-alike\/","title":{"rendered":"\u82b1\u82b1\u9171 LeetCode 1704. Determine if String Halves Are Alike"},"content":{"rendered":"\n<p>You are given a string&nbsp;<code>s<\/code>&nbsp;of even length. Split this string into two halves of equal lengths, and let&nbsp;<code>a<\/code>&nbsp;be the first half and&nbsp;<code>b<\/code>&nbsp;be the second half.<\/p>\n\n\n\n<p>Two strings are&nbsp;<strong>alike<\/strong>&nbsp;if they have the same number of vowels (<code>'a'<\/code>,&nbsp;<code>'e'<\/code>,&nbsp;<code>'i'<\/code>,&nbsp;<code>'o'<\/code>,&nbsp;<code>'u'<\/code>,&nbsp;<code>'A'<\/code>,&nbsp;<code>'E'<\/code>,&nbsp;<code>'I'<\/code>,&nbsp;<code>'O'<\/code>,&nbsp;<code>'U'<\/code>). Notice that&nbsp;<code>s<\/code>&nbsp;contains uppercase and lowercase letters.<\/p>\n\n\n\n<p>Return&nbsp;<code>true<\/code><em>&nbsp;if&nbsp;<\/em><code>a<\/code><em>&nbsp;and&nbsp;<\/em><code>b<\/code><em>&nbsp;are&nbsp;<strong>alike<\/strong><\/em>. Otherwise, return&nbsp;<code>false<\/code>.<\/p>\n\n\n\n<p><strong>Example 1:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> s = \"book\"\n<strong>Output:<\/strong> true\n<strong>Explanation:<\/strong>&nbsp;a = \"bo\" and b = \"ok\". a has 1 vowel and b has 1 vowel. Therefore, they are alike.\n<\/pre>\n\n\n\n<p><strong>Example 2:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> s = \"textbook\"\n<strong>Output:<\/strong> false\n<strong>Explanation:<\/strong>&nbsp;a = \"text\" and b = \"book\". a has 1 vowel whereas b has 2. Therefore, they are not alike.\nNotice that the vowel o is counted twice.\n<\/pre>\n\n\n\n<p><strong>Example 3:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> s = \"MerryChristmas\"\n<strong>Output:<\/strong> false\n<\/pre>\n\n\n\n<p><strong>Example 4:<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted;crayon:false\"><strong>Input:<\/strong> s = \"AbCdEfGh\"\n<strong>Output:<\/strong> true\n<\/pre>\n\n\n\n<p><strong>Constraints:<\/strong><\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><code>2 &lt;= s.length &lt;= 1000<\/code><\/li><li><code>s.length<\/code>&nbsp;is even.<\/li><li><code>s<\/code>&nbsp;consists of&nbsp;<strong>uppercase and lowercase<\/strong>&nbsp;letters.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>Solution: Counting<\/strong><\/h2>\n\n\n\n<p>Time complexity: O(n)<br>Space complexity: O(1)<\/p>\n\n\n\n<div class=\"responsive-tabs\">\n<h2 class=\"tabtitle\">Python3<\/h2>\n<div class=\"tabcontent\">\n\n<pre lang=\"python\"># Author: Huahua\nclass Solution:\n  def halvesAreAlike(self, s: str) -> bool:\n    def count(s: str) -> int:\n      return sum(c in 'aeiouAEIOU' for c in s)\n    n = len(s)\n    return count(s[:n\/\/2]) == count(s[n\/\/2:])\n<\/pre>\n<\/div><\/div>\n","protected":false},"excerpt":{"rendered":"<p>You are given a string&nbsp;s&nbsp;of even length. Split this string into two halves of equal lengths, and let&nbsp;a&nbsp;be the first half and&nbsp;b&nbsp;be the second half.&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[47],"tags":[8,222,4,449],"class_list":["post-7856","post","type-post","status-publish","format-standard","hentry","category-string","tag-counting","tag-easy","tag-string","tag-vowel","entry","simple"],"_links":{"self":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7856","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/comments?post=7856"}],"version-history":[{"count":3,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7856\/revisions"}],"predecessor-version":[{"id":7859,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/posts\/7856\/revisions\/7859"}],"wp:attachment":[{"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/media?parent=7856"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/categories?post=7856"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zxi.mytechroad.com\/blog\/wp-json\/wp\/v2\/tags?post=7856"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}