Skip to content

Commit 1c3eee7

Browse files
committed
Sync LeetCode submission Runtime - 45 ms (88.17%), Memory - 49.3 MB (6.17%)
1 parent 1d5adac commit 1c3eee7

File tree

2 files changed

+55
-0
lines changed

2 files changed

+55
-0
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<p>Given a string <code>s</code> consisting of lowercase English letters, return <em>the first letter to appear <strong>twice</strong></em>.</p>
2+
3+
<p><strong>Note</strong>:</p>
4+
5+
<ul>
6+
<li>A letter <code>a</code> appears twice before another letter <code>b</code> if the <strong>second</strong> occurrence of <code>a</code> is before the <strong>second</strong> occurrence of <code>b</code>.</li>
7+
<li><code>s</code> will contain at least one letter that appears twice.</li>
8+
</ul>
9+
10+
<p>&nbsp;</p>
11+
<p><strong class="example">Example 1:</strong></p>
12+
13+
<pre>
14+
<strong>Input:</strong> s = &quot;abccbaacz&quot;
15+
<strong>Output:</strong> &quot;c&quot;
16+
<strong>Explanation:</strong>
17+
The letter &#39;a&#39; appears on the indexes 0, 5 and 6.
18+
The letter &#39;b&#39; appears on the indexes 1 and 4.
19+
The letter &#39;c&#39; appears on the indexes 2, 3 and 7.
20+
The letter &#39;z&#39; appears on the index 8.
21+
The letter &#39;c&#39; is the first letter to appear twice, because out of all the letters the index of its second occurrence is the smallest.
22+
</pre>
23+
24+
<p><strong class="example">Example 2:</strong></p>
25+
26+
<pre>
27+
<strong>Input:</strong> s = &quot;abcdd&quot;
28+
<strong>Output:</strong> &quot;d&quot;
29+
<strong>Explanation:</strong>
30+
The only letter that appears twice is &#39;d&#39; so we return &#39;d&#39;.
31+
</pre>
32+
33+
<p>&nbsp;</p>
34+
<p><strong>Constraints:</strong></p>
35+
36+
<ul>
37+
<li><code>2 &lt;= s.length &lt;= 100</code></li>
38+
<li><code>s</code> consists of lowercase English letters.</li>
39+
<li><code>s</code> has at least one repeated letter.</li>
40+
</ul>
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
/**
2+
* @param {string} s
3+
* @return {character}
4+
*/
5+
var repeatedCharacter = function(s) {
6+
let hash = {}
7+
8+
for(let i=0;i<s.length;i++){
9+
hash[s[i]] = hash[s[i]] ? hash[s[i]] + 1 : 1
10+
11+
if(hash[s[i]] == 2){
12+
return s[i]
13+
}
14+
}
15+
};

0 commit comments

Comments
 (0)