<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>text Archives - Huahua&#039;s Tech Road</title>
	<atom:link href="https://zxi.mytechroad.com/blog/tag/text/feed/" rel="self" type="application/rss+xml" />
	<link>https://zxi.mytechroad.com/blog/tag/text/</link>
	<description></description>
	<lastBuildDate>Thu, 23 Dec 2021 20:10:14 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=6.0.8</generator>

<image>
	<url>https://zxi.mytechroad.com/blog/wp-content/uploads/2017/09/cropped-photo-32x32.jpg</url>
	<title>text Archives - Huahua&#039;s Tech Road</title>
	<link>https://zxi.mytechroad.com/blog/tag/text/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>花花酱 LeetCode 722. Remove Comments</title>
		<link>https://zxi.mytechroad.com/blog/string/leetcode-722-remove-comments/</link>
					<comments>https://zxi.mytechroad.com/blog/string/leetcode-722-remove-comments/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Thu, 23 Dec 2021 13:27:19 +0000</pubDate>
				<category><![CDATA[String]]></category>
		<category><![CDATA[medium]]></category>
		<category><![CDATA[string]]></category>
		<category><![CDATA[text]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=9199</guid>

					<description><![CDATA[<p>Given a C++ program, remove comments from it. The program source is an array of strings&#160;source&#160;where&#160;source[i]&#160;is the&#160;ith&#160;line of the source code. This represents the result&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/string/leetcode-722-remove-comments/">花花酱 LeetCode 722. Remove Comments</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>Given a C++ program, remove comments from it. The program source is an array of strings&nbsp;<code>source</code>&nbsp;where&nbsp;<code>source[i]</code>&nbsp;is the&nbsp;<code>i<sup>th</sup></code>&nbsp;line of the source code. This represents the result of splitting the original source code string by the newline character&nbsp;<code>'\n'</code>.</p>



<p>In C++, there are two types of comments, line comments, and block comments.</p>



<ul><li>The string&nbsp;<code>"//"</code>&nbsp;denotes a line comment, which represents that it and the rest of the characters to the right of it in the same line should be ignored.</li><li>The string&nbsp;<code>"/*"</code>&nbsp;denotes a block comment, which represents that all characters until the next (non-overlapping) occurrence of&nbsp;<code>"*/"</code>&nbsp;should be ignored. (Here, occurrences happen in reading order: line by line from left to right.) To be clear, the string&nbsp;<code>"/*/"</code>&nbsp;does not yet end the block comment, as the ending would be overlapping the beginning.</li></ul>



<p>The first effective comment takes precedence over others.</p>



<ul><li>For example, if the string&nbsp;<code>"//"</code>&nbsp;occurs in a block comment, it is ignored.</li><li>Similarly, if the string&nbsp;<code>"/*"</code>&nbsp;occurs in a line or block comment, it is also ignored.</li></ul>



<p>If a certain line of code is empty after removing comments, you must not output that line: each string in the answer list will be non-empty.</p>



<p>There will be no control characters, single quote, or double quote characters.</p>



<ul><li>For example,&nbsp;<code>source = "string s = "/* Not a comment. */";"</code>&nbsp;will not be a test case.</li></ul>



<p>Also, nothing else such as defines or macros will interfere with the comments.</p>



<p>It is guaranteed that every open block comment will eventually be closed, so&nbsp;<code>"/*"</code>&nbsp;outside of a line or block comment always starts a new comment.</p>



<p>Finally, implicit newline characters can be deleted by block comments. Please see the examples below for details.</p>



<p>After removing the comments from the source code, return&nbsp;<em>the source code in the same format</em>.</p>



<p><strong>Example 1:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> source = ["/*Test program */", "int main()", "{ ", "  // variable declaration ", "int a, b, c;", "/* This is a test", "   multiline  ", "   comment for ", "   testing */", "a = b + c;", "}"]
<strong>Output:</strong> ["int main()","{ ","  ","int a, b, c;","a = b + c;","}"]
<strong>Explanation:</strong> The line by line code is visualized as below:
/*Test program */
int main()
{ 
  // variable declaration 
int a, b, c;
/* This is a test
   multiline  
   comment for 
   testing */
a = b + c;
}
The string /* denotes a block comment, including line 1 and lines 6-9. The string // denotes line 4 as comments.
The line by line output code is visualized as below:
int main()
{ 
  
int a, b, c;
a = b + c;
}
</pre>



<p><strong>Example 2:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong> source = ["a/*comment", "line", "more_comment*/b"]
<strong>Output:</strong> ["ab"]
<strong>Explanation:</strong> The original source string is "a/*comment\nline\nmore_comment*/b", where we have bolded the newline characters.  After deletion, the implicit newline characters are deleted, leaving the string "ab", which when delimited by newline characters becomes ["ab"].
</pre>



<p><strong>Constraints:</strong></p>



<ul><li><code>1 &lt;= source.length &lt;= 100</code></li><li><code>0 &lt;= source[i].length &lt;= 80</code></li><li><code>source[i]</code>&nbsp;consists of printable&nbsp;<strong>ASCII</strong>&nbsp;characters.</li><li>Every open block comment is eventually closed.</li><li>There are no single-quote or&nbsp;double-quote in the input.</li></ul>



<h2><strong>Solution: Marking the block</strong></h2>



<p>The key of this problem is to mark the start and end of a block comment and handling new lines.</p>



<p>Time complexity: O(n)<br>Space complexity: O(n) -&gt; O(1)</p>



<div class="responsive-tabs">
<h2 class="tabtitle">C++</h2>
<div class="tabcontent">

<pre class="crayon-plain-tag">// Author: Huahua
class Solution {
public:
  vector&lt;string&gt; removeComments(vector&lt;string&gt;&amp; source) {
    vector&lt;string&gt; ans;
    bool block = false;
    string output;
    for (string_view line : source) {      
      for (int i = 0, n = line.size(); i &lt; n; ++i) {
        if (block) {
          if (line.substr(i, 2) == &quot;*/&quot;) {
            block = false; // leaving block.
            ++i;
          }
          // skip all characters inside the block.
        } else if (line.substr(i, 2) == &quot;/*&quot;) {
          block = true; // entering block.
          ++i;
        } else if (line.substr(i, 2) == &quot;//&quot;) {
          break;// skip the remaining part of the line.
        } else {
          output += line[i];
        }
      }
      if (!block and !output.empty())
        ans.push_back(std::move(output)); // output cleared.
    }
    return ans;
  }
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/string/leetcode-722-remove-comments/">花花酱 LeetCode 722. Remove Comments</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://zxi.mytechroad.com/blog/string/leetcode-722-remove-comments/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>花花酱 LeetCode 68. Text Justification</title>
		<link>https://zxi.mytechroad.com/blog/string/leetcode-68-text-justification/</link>
					<comments>https://zxi.mytechroad.com/blog/string/leetcode-68-text-justification/#respond</comments>
		
		<dc:creator><![CDATA[zxi]]></dc:creator>
		<pubDate>Wed, 02 Oct 2019 06:30:32 +0000</pubDate>
				<category><![CDATA[String]]></category>
		<category><![CDATA[hard]]></category>
		<category><![CDATA[string]]></category>
		<category><![CDATA[text]]></category>
		<guid isPermaLink="false">https://zxi.mytechroad.com/blog/?p=5688</guid>

					<description><![CDATA[<p>Given an array of words and a width&#160;maxWidth, format the text such that each line has exactly&#160;maxWidth&#160;characters and is fully (left and right) justified. You&#8230;</p>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/string/leetcode-68-text-justification/">花花酱 LeetCode 68. Text Justification</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<p>Given an array of words and a width&nbsp;<em>maxWidth</em>, format the text such that each line has exactly&nbsp;<em>maxWidth</em>&nbsp;characters and is fully (left and right) justified.</p>



<p>You should pack your words in a greedy approach; that is, pack as many words as you can in each line. Pad extra spaces&nbsp;<code>' '</code>&nbsp;when necessary so that each line has exactly&nbsp;<em>maxWidth</em>&nbsp;characters.</p>



<p>Extra spaces between words should be distributed as evenly as possible. If the number of spaces on a line do not divide evenly between words, the empty slots on the left will be assigned more spaces than the slots on the right.</p>



<p>For the last line of text, it should be left justified and no&nbsp;<strong>extra</strong>&nbsp;space is inserted between words.</p>



<p><strong>Note:</strong></p>



<ul><li>A word is defined as a character sequence consisting&nbsp;of non-space characters only.</li><li>Each word&#8217;s length is&nbsp;guaranteed to be greater than 0 and not exceed&nbsp;<em>maxWidth</em>.</li><li>The input array&nbsp;<code>words</code>&nbsp;contains at least one word.</li></ul>



<p><strong>Example 1:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong>
words = ["This", "is", "an", "example", "of", "text", "justification."]
maxWidth = 16
<strong>Output:</strong>
[
&nbsp; &nbsp;"This &nbsp; &nbsp;is &nbsp; &nbsp;an",
&nbsp; &nbsp;"example &nbsp;of text",
&nbsp; &nbsp;"justification. &nbsp;"
]
</pre>



<p><strong>Example 2:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong>
words = ["What","must","be","acknowledgment","shall","be"]
maxWidth = 16
<strong>Output:</strong>
[
&nbsp; "What &nbsp; must &nbsp; be",
&nbsp; "acknowledgment &nbsp;",
&nbsp; "shall be &nbsp; &nbsp; &nbsp; &nbsp;"
]
<strong>Explanation:</strong> Note that the last line is "shall be    " instead of "shall     be",
&nbsp;            because the last line must be left-justified instead of fully-justified.
             Note that the second line is also left-justified becase it contains only one word.
</pre>



<p><strong>Example 3:</strong></p>



<pre class="wp-block-preformatted;crayon:false"><strong>Input:</strong>
words = ["Science","is","what","we","understand","well","enough","to","explain",
&nbsp;        "to","a","computer.","Art","is","everything","else","we","do"]
maxWidth = 20
<strong>Output:</strong>
[
&nbsp; "Science &nbsp;is &nbsp;what we",
  "understand &nbsp; &nbsp; &nbsp;well",
&nbsp; "enough to explain to",
&nbsp; "a &nbsp;computer. &nbsp;Art is",
&nbsp; "everything &nbsp;else &nbsp;we",
&nbsp; "do &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;"
]</pre>



<p>Solution: Simulation</p>



<p>Time complexity: O(sum(len(s))<br>Space complexity: O(sum(len(s)) </p>



<div class="responsive-tabs">
<h2 class="tabtitle">C++</h2>
<div class="tabcontent">

<pre class="crayon-plain-tag">// Author: Huahua
class Solution {
public:
  vector&lt;string&gt; fullJustify(vector&lt;string&gt; &amp;words, int L) {
    size_t i = 0, n = words.size();
    vector&lt;string&gt; ans;
    
    while (i &lt; n) {
      bool last_line = false;
      int ll = 0;
      vector&lt;string_view&gt; tw;
      
      while (ll &lt;= L &amp;&amp; i &lt; n) {
        size_t wl = words[i].length();
        int tll = ll + (tw.size() ? 1 : 0) + wl;
        if (tll &gt; L) break;
        ll = tll;
        tw.push_back(string_view(words[i]));
        if (++i == n) last_line = true;
        if (ll == L) break;
      }

      string line;
      if (!last_line) {
        int tl = 0; 
        for(const auto&amp; w : tw) tl += w.length();
        int avg_space = tw.size()==1 ? 0 : ((L-tl) / (tw.size() - 1));
        int extra_space = tw.size()==1 ? 0 : (L - avg_space * (tw.size() - 1) - tl);        
        for (const auto&amp; w : tw) {
          if (line.length() &gt; 0) {
            line.append(avg_space, ' ');
            if (extra_space &gt; 0) { 
              line += ' '; 
              --extra_space; 
            }
          }
          line += w;
        }
      } else {
        for (const auto&amp; w : tw) {
          if (line.length() &gt; 0) 
            line += ' ';
          line += w;
        }
      }

      line.append(L - line.length(), ' ');
      ans.push_back(std::move(line));
    }
    return ans;
  }
};</pre>
</div></div>
<p>The post <a rel="nofollow" href="https://zxi.mytechroad.com/blog/string/leetcode-68-text-justification/">花花酱 LeetCode 68. Text Justification</a> appeared first on <a rel="nofollow" href="https://zxi.mytechroad.com/blog">Huahua&#039;s Tech Road</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://zxi.mytechroad.com/blog/string/leetcode-68-text-justification/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
