You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
<h3 id="solution-1-case-discussion-two-pointers">Solution 1: Case Discussion + Two Pointers</h3>
86169
-
<p>We denote the lengths of strings <span class="arithmatex">\(first\)</span> and <span class="arithmatex">\(second\)</span> as <span class="arithmatex">\(m\)</span> and <span class="arithmatex">\(n\)</span>, respectively, where <span class="arithmatex">\(m \geq n\)</span>.</p>
86170
-
<p>Next, we discuss different cases:</p>
86168
+
<h3 id="solution-1-case-analysis-two-pointers">Solution 1: Case Analysis + Two Pointers</h3>
86169
+
<p>Let the lengths of the strings <span class="arithmatex">\(\textit{first}\)</span> and <span class="arithmatex">\(\textit{second}\)</span> be <span class="arithmatex">\(m\)</span> and <span class="arithmatex">\(n\)</span>, respectively. Assume <span class="arithmatex">\(m \geq n\)</span>.</p>
86170
+
<p>Next, we discuss the following cases:</p>
86171
86171
<ul>
86172
-
<li>When <span class="arithmatex">\(m - n > 1\)</span>, <span class="arithmatex">\(first\)</span> and <span class="arithmatex">\(second\)</span> cannot be obtained through a single edit, so we return <code>false</code>.</li>
86173
-
<li>When <span class="arithmatex">\(m = n\)</span>, <span class="arithmatex">\(first\)</span> and <span class="arithmatex">\(second\)</span> can only be obtained through a single edit if and only if exactly one character is different.</li>
86174
-
<li>When <span class="arithmatex">\(m - n = 1\)</span>, <span class="arithmatex">\(first\)</span> and <span class="arithmatex">\(second\)</span> can only be obtained through a single edit if and only if <span class="arithmatex">\(second\)</span> is obtained by deleting one character from <span class="arithmatex">\(first\)</span>. We can use two pointers to implement this.</li>
86172
+
<li>When <span class="arithmatex">\(m - n \gt 1\)</span>, <span class="arithmatex">\(\textit{first}\)</span> and <span class="arithmatex">\(\textit{second}\)</span> cannot be made equal with one edit, so return <code>false</code>;</li>
86173
+
<li>When <span class="arithmatex">\(m = n\)</span>, <span class="arithmatex">\(\textit{first}\)</span> and <span class="arithmatex">\(\textit{second}\)</span> can be made equal with one edit only if there is exactly one different character;</li>
86174
+
<li>When <span class="arithmatex">\(m - n = 1\)</span>, <span class="arithmatex">\(\textit{first}\)</span> and <span class="arithmatex">\(\textit{second}\)</span> can be made equal with one edit only if <span class="arithmatex">\(\textit{second}\)</span> is obtained by deleting one character from <span class="arithmatex">\(\textit{first}\)</span>. We can use two pointers to achieve this.</li>
86175
86175
</ul>
86176
86176
<p>The time complexity is <span class="arithmatex">\(O(n)\)</span>, where <span class="arithmatex">\(n\)</span> is the length of the string. The space complexity is <span class="arithmatex">\(O(1)\)</span>.</p>
Copy file name to clipboardExpand all lines: en/lcci/1.6/index.html
+1-25Lines changed: 1 addition & 25 deletions
Original file line number
Diff line number
Diff line change
@@ -86170,7 +86170,7 @@ <h3 id="solution-1-two-pointers">Solution 1: Two Pointers</h3>
86170
86170
<p>We can use two pointers to find the start and end positions of each consecutive character, calculate the length of the consecutive characters, and then append the character and length to the string <span class="arithmatex">\(t\)</span>.</p>
86171
86171
<p>Finally, we compare the lengths of <span class="arithmatex">\(t\)</span> and <span class="arithmatex">\(S\)</span>. If the length of <span class="arithmatex">\(t\)</span> is less than <span class="arithmatex">\(S\)</span>, we return <span class="arithmatex">\(t\)</span>, otherwise we return <span class="arithmatex">\(S\)</span>.</p>
86172
86172
<p>The time complexity is <span class="arithmatex">\(O(n)\)</span>, and the space complexity is <span class="arithmatex">\(O(n)\)</span>. Here, <span class="arithmatex">\(n\)</span> is the length of the string.</p>
<p>According to the problem requirements, we actually need to rotate <span class="arithmatex">\(matrix[i][j]\)</span> to <span class="arithmatex">\(matrix[j][n - i - 1]\)</span>.</p>
86212
-
<p>We can first flip the matrix upside down, that is, swap <span class="arithmatex">\(matrix[i][j]\)</span> and <span class="arithmatex">\(matrix[n - i - 1][j]\)</span>, and then flip the matrix along the main diagonal, that is, swap <span class="arithmatex">\(matrix[i][j]\)</span> and <span class="arithmatex">\(matrix[j][i]\)</span>. This way, we can rotate <span class="arithmatex">\(matrix[i][j]\)</span> to <span class="arithmatex">\(matrix[j][n - i - 1]\)</span>.</p>
<p>According to the problem requirements, we need to rotate <span class="arithmatex">\(\text{matrix}[i][j]\)</span> to <span class="arithmatex">\(\text{matrix}[j][n - i - 1]\)</span>.</p>
86212
+
<p>We can first flip the matrix upside down, i.e., swap <span class="arithmatex">\(\text{matrix}[i][j]\)</span> with <span class="arithmatex">\(\text{matrix}[n - i - 1][j]\)</span>, and then flip the matrix along the main diagonal, i.e., swap <span class="arithmatex">\(\text{matrix}[i][j]\)</span> with <span class="arithmatex">\(\text{matrix}[j][i]\)</span>. This will rotate <span class="arithmatex">\(\text{matrix}[i][j]\)</span> to <span class="arithmatex">\(\text{matrix}[j][n - i - 1]\)</span>.</p>
86213
86213
<p>The time complexity is <span class="arithmatex">\(O(n^2)\)</span>, where <span class="arithmatex">\(n\)</span> is the side length of the matrix. The space complexity is <span class="arithmatex">\(O(1)\)</span>.</p>
0 commit comments