id int64 1 3.58k | problem_description stringlengths 516 21.8k | instruction int64 0 3 | solution_c dict |
|---|---|---|---|
13 | <p>Roman numerals are represented by seven different symbols: <code>I</code>, <code>V</code>, <code>X</code>, <code>L</code>, <code>C</code>, <code>D</code> and <code>M</code>.</p>
<pre>
<strong>Symbol</strong> <strong>Value</strong>
I 1
V 5
X 10
L 50
C 100
D 500
M 1000</pre>
<p>For example, <code>2</code> is written as <code>II</code> in Roman numeral, just two ones added together. <code>12</code> is written as <code>XII</code>, which is simply <code>X + II</code>. The number <code>27</code> is written as <code>XXVII</code>, which is <code>XX + V + II</code>.</p>
<p>Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not <code>IIII</code>. Instead, the number four is written as <code>IV</code>. Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as <code>IX</code>. There are six instances where subtraction is used:</p>
<ul>
<li><code>I</code> can be placed before <code>V</code> (5) and <code>X</code> (10) to make 4 and 9. </li>
<li><code>X</code> can be placed before <code>L</code> (50) and <code>C</code> (100) to make 40 and 90. </li>
<li><code>C</code> can be placed before <code>D</code> (500) and <code>M</code> (1000) to make 400 and 900.</li>
</ul>
<p>Given a roman numeral, convert it to an integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "III"
<strong>Output:</strong> 3
<strong>Explanation:</strong> III = 3.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "LVIII"
<strong>Output:</strong> 58
<strong>Explanation:</strong> L = 50, V= 5, III = 3.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "MCMXCIV"
<strong>Output:</strong> 1994
<strong>Explanation:</strong> M = 1000, CM = 900, XC = 90 and IV = 4.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 15</code></li>
<li><code>s</code> contains only the characters <code>('I', 'V', 'X', 'L', 'C', 'D', 'M')</code>.</li>
<li>It is <strong>guaranteed</strong> that <code>s</code> is a valid roman numeral in the range <code>[1, 3999]</code>.</li>
</ul>
| 0 | {
"code": "typedef std::pair<std::string, int> pairs;\nstd::vector<pairs> map_roman = {{\"I\",1},{\"IV\",4},{\"V\",5},{\"IX\",9},\n {\"X\",10},{\"XL\",40},{\"L\",50},{\"XC\",90},\n {\"C\",100}, {\"CD\",400},{\"D\",500},{\"CM\",900},\n {\"M\",1000}};\nclass Solution {\npublic:\n \n int romanToInt(std::string s) {\n std::vector<int> vec;\n auto it = map_roman.rbegin();\n int i = 0;\n while (true)\n {\n if (i == s.length())\n break;\n if (it->first.length()==1){\n if (s[i] == it->first.c_str()[0])\n {\n vec.push_back(it->second);\n // std::cout << \"1 \" << vec.back() << std::endl;\n i++;\n }\n else\n {\n it++;\n if (it == map_roman.rend())\n break;\n }\n \n }\n else\n {\n if ( i < s.length() - 1 && s[i] == it->first.c_str()[0] && s[i+1] == it->first.c_str()[1])\n {\n vec.push_back(it->second);\n it++;\n i+=2;\n }\n else{\n it++;\n }\n }\n }\n int vec_sum = 0;\n for (auto it = vec.begin(); it!=vec.end(); ++it)\n {\n vec_sum+=*it;\n }\n return vec_sum;\n }\n};",
"memory": "11500"
} |
13 | <p>Roman numerals are represented by seven different symbols: <code>I</code>, <code>V</code>, <code>X</code>, <code>L</code>, <code>C</code>, <code>D</code> and <code>M</code>.</p>
<pre>
<strong>Symbol</strong> <strong>Value</strong>
I 1
V 5
X 10
L 50
C 100
D 500
M 1000</pre>
<p>For example, <code>2</code> is written as <code>II</code> in Roman numeral, just two ones added together. <code>12</code> is written as <code>XII</code>, which is simply <code>X + II</code>. The number <code>27</code> is written as <code>XXVII</code>, which is <code>XX + V + II</code>.</p>
<p>Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not <code>IIII</code>. Instead, the number four is written as <code>IV</code>. Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as <code>IX</code>. There are six instances where subtraction is used:</p>
<ul>
<li><code>I</code> can be placed before <code>V</code> (5) and <code>X</code> (10) to make 4 and 9. </li>
<li><code>X</code> can be placed before <code>L</code> (50) and <code>C</code> (100) to make 40 and 90. </li>
<li><code>C</code> can be placed before <code>D</code> (500) and <code>M</code> (1000) to make 400 and 900.</li>
</ul>
<p>Given a roman numeral, convert it to an integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "III"
<strong>Output:</strong> 3
<strong>Explanation:</strong> III = 3.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "LVIII"
<strong>Output:</strong> 58
<strong>Explanation:</strong> L = 50, V= 5, III = 3.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "MCMXCIV"
<strong>Output:</strong> 1994
<strong>Explanation:</strong> M = 1000, CM = 900, XC = 90 and IV = 4.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 15</code></li>
<li><code>s</code> contains only the characters <code>('I', 'V', 'X', 'L', 'C', 'D', 'M')</code>.</li>
<li>It is <strong>guaranteed</strong> that <code>s</code> is a valid roman numeral in the range <code>[1, 3999]</code>.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n void createRomanMap(vector<int>& map){\n map['I'-'A'] = 1, map['V'-'A'] = 5;\n map['X'-'A'] = 10, map['L'-'A'] = 50;\n map['C'-'A'] = 100, map['D'-'A'] = 500;\n map['M'-'A'] = 1000;\n }\n\n int romanToInt(string s) {\n vector<int> hmap(26,0);\n createRomanMap(hmap);\n\n int n=s.length();\n int i=0, j=1, ans=0;\n while(j<n){\n int ithValue = hmap[s[i]-'A'];\n int jthValue = hmap[s[j]-'A'];\n \n if(jthValue > ithValue){\n ans += jthValue - ithValue;\n i += 2;\n j += 2;\n }\n else{\n ans += ithValue;\n i += 1;\n j += 1;\n }\n }\n if(j == n) ans += hmap[s[n-1]-'A'];\n\n return ans;\n }\n};",
"memory": "11600"
} |
13 | <p>Roman numerals are represented by seven different symbols: <code>I</code>, <code>V</code>, <code>X</code>, <code>L</code>, <code>C</code>, <code>D</code> and <code>M</code>.</p>
<pre>
<strong>Symbol</strong> <strong>Value</strong>
I 1
V 5
X 10
L 50
C 100
D 500
M 1000</pre>
<p>For example, <code>2</code> is written as <code>II</code> in Roman numeral, just two ones added together. <code>12</code> is written as <code>XII</code>, which is simply <code>X + II</code>. The number <code>27</code> is written as <code>XXVII</code>, which is <code>XX + V + II</code>.</p>
<p>Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not <code>IIII</code>. Instead, the number four is written as <code>IV</code>. Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as <code>IX</code>. There are six instances where subtraction is used:</p>
<ul>
<li><code>I</code> can be placed before <code>V</code> (5) and <code>X</code> (10) to make 4 and 9. </li>
<li><code>X</code> can be placed before <code>L</code> (50) and <code>C</code> (100) to make 40 and 90. </li>
<li><code>C</code> can be placed before <code>D</code> (500) and <code>M</code> (1000) to make 400 and 900.</li>
</ul>
<p>Given a roman numeral, convert it to an integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "III"
<strong>Output:</strong> 3
<strong>Explanation:</strong> III = 3.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "LVIII"
<strong>Output:</strong> 58
<strong>Explanation:</strong> L = 50, V= 5, III = 3.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "MCMXCIV"
<strong>Output:</strong> 1994
<strong>Explanation:</strong> M = 1000, CM = 900, XC = 90 and IV = 4.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 15</code></li>
<li><code>s</code> contains only the characters <code>('I', 'V', 'X', 'L', 'C', 'D', 'M')</code>.</li>
<li>It is <strong>guaranteed</strong> that <code>s</code> is a valid roman numeral in the range <code>[1, 3999]</code>.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int romanToInt(string s) {\n // I->1, V->5, X->10, C->100, D->500, L->1000\n // DXCII-> 500+90+2->\n vector<int> mp(26, 0);\n mp['I'-'A'] = 1;\n mp['V'-'A'] = 5;\n mp['X'-'A'] = 10;\n mp['L'-'A'] = 50;\n mp['C'-'A'] = 100;\n mp['D'-'A'] = 500;\n mp['M'-'A'] = 1000;\n\n int result = 0;\n\n for(int i = 0; i < s.length(); i++) {\n // compare each char with the next; if the char is less than the next one then substrart the corresponding num to result,\n // otherwise add\n if(i < s.length() -1 && mp[s[i]-'A'] < mp[s[i+1]-'A']) {\n result -= mp[s[i]-'A'];\n } else {\n result += mp[s[i]-'A'];\n }\n }\n return result;\n }\n};",
"memory": "11700"
} |
13 | <p>Roman numerals are represented by seven different symbols: <code>I</code>, <code>V</code>, <code>X</code>, <code>L</code>, <code>C</code>, <code>D</code> and <code>M</code>.</p>
<pre>
<strong>Symbol</strong> <strong>Value</strong>
I 1
V 5
X 10
L 50
C 100
D 500
M 1000</pre>
<p>For example, <code>2</code> is written as <code>II</code> in Roman numeral, just two ones added together. <code>12</code> is written as <code>XII</code>, which is simply <code>X + II</code>. The number <code>27</code> is written as <code>XXVII</code>, which is <code>XX + V + II</code>.</p>
<p>Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not <code>IIII</code>. Instead, the number four is written as <code>IV</code>. Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as <code>IX</code>. There are six instances where subtraction is used:</p>
<ul>
<li><code>I</code> can be placed before <code>V</code> (5) and <code>X</code> (10) to make 4 and 9. </li>
<li><code>X</code> can be placed before <code>L</code> (50) and <code>C</code> (100) to make 40 and 90. </li>
<li><code>C</code> can be placed before <code>D</code> (500) and <code>M</code> (1000) to make 400 and 900.</li>
</ul>
<p>Given a roman numeral, convert it to an integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "III"
<strong>Output:</strong> 3
<strong>Explanation:</strong> III = 3.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "LVIII"
<strong>Output:</strong> 58
<strong>Explanation:</strong> L = 50, V= 5, III = 3.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "MCMXCIV"
<strong>Output:</strong> 1994
<strong>Explanation:</strong> M = 1000, CM = 900, XC = 90 and IV = 4.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 15</code></li>
<li><code>s</code> contains only the characters <code>('I', 'V', 'X', 'L', 'C', 'D', 'M')</code>.</li>
<li>It is <strong>guaranteed</strong> that <code>s</code> is a valid roman numeral in the range <code>[1, 3999]</code>.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int romanToInt(string s) {\n constexpr auto create_mapping = []() constexpr {\n array<int, 26> mapping = {0};\n array<char, 7> letters = {\n 'I', 'V', 'X', 'L', 'C', 'D', 'M'\n };\n array<int, 7> values = {\n 1, 5, 10, 50, 100, 500, 1000\n };\n for (int i = 0; i < 7; ++i) {\n mapping[letters[i]-'A'] = values[i];\n }\n return mapping;\n };\n constexpr auto mapping = create_mapping();\n int dec = 0;\n for (int i = 0; i < s.size(); ++i) {\n switch (s[i]) {\n case 'V':\n case 'L':\n case 'D':\n case 'M':\n dec += mapping[s[i]-'A'];\n continue;\n case 'I':\n if ((i+1 != s.size()) && (s[i+1] == 'V' || s[i+1] == 'X')) {\n dec += mapping[s[i+1]-'A'] - mapping[s[i]-'A'];\n ++i;\n } else {\n dec += mapping[s[i]-'A'];\n }\n continue;\n case 'X':\n if ((i+1 != s.size()) && (s[i+1] == 'L' || s[i+1] == 'C')) {\n dec += mapping[s[i+1]-'A'] - mapping[s[i]-'A'];\n ++i;\n } else {\n dec += mapping[s[i]-'A'];\n }\n continue;\n case 'C':\n if ((i+1 != s.size()) && (s[i+1] == 'D' || s[i+1] == 'M')) {\n dec += mapping[s[i+1]-'A'] - mapping[s[i]-'A'];\n ++i;\n } else {\n dec += mapping[s[i]-'A'];\n }\n continue;\n }\n }\n return dec;\n }\n};",
"memory": "11800"
} |
13 | <p>Roman numerals are represented by seven different symbols: <code>I</code>, <code>V</code>, <code>X</code>, <code>L</code>, <code>C</code>, <code>D</code> and <code>M</code>.</p>
<pre>
<strong>Symbol</strong> <strong>Value</strong>
I 1
V 5
X 10
L 50
C 100
D 500
M 1000</pre>
<p>For example, <code>2</code> is written as <code>II</code> in Roman numeral, just two ones added together. <code>12</code> is written as <code>XII</code>, which is simply <code>X + II</code>. The number <code>27</code> is written as <code>XXVII</code>, which is <code>XX + V + II</code>.</p>
<p>Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not <code>IIII</code>. Instead, the number four is written as <code>IV</code>. Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as <code>IX</code>. There are six instances where subtraction is used:</p>
<ul>
<li><code>I</code> can be placed before <code>V</code> (5) and <code>X</code> (10) to make 4 and 9. </li>
<li><code>X</code> can be placed before <code>L</code> (50) and <code>C</code> (100) to make 40 and 90. </li>
<li><code>C</code> can be placed before <code>D</code> (500) and <code>M</code> (1000) to make 400 and 900.</li>
</ul>
<p>Given a roman numeral, convert it to an integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "III"
<strong>Output:</strong> 3
<strong>Explanation:</strong> III = 3.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "LVIII"
<strong>Output:</strong> 58
<strong>Explanation:</strong> L = 50, V= 5, III = 3.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "MCMXCIV"
<strong>Output:</strong> 1994
<strong>Explanation:</strong> M = 1000, CM = 900, XC = 90 and IV = 4.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 15</code></li>
<li><code>s</code> contains only the characters <code>('I', 'V', 'X', 'L', 'C', 'D', 'M')</code>.</li>
<li>It is <strong>guaranteed</strong> that <code>s</code> is a valid roman numeral in the range <code>[1, 3999]</code>.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int romanToInt(string s) {\n int len = s.length();\n string sub_str;\n int sum = 0;\n for (int i = 0; i < len; i++)\n {\n if (i != len - 1)\n {\n if (s[i] == 'I' || s[i] == 'X' || s[i] == 'C')\n {\n sub_str = s.substr(i, 2);\n if (sub_str == \"IV\")\n {\n sum += 4;\n i++;\n }\n else if (sub_str == \"IX\")\n {\n sum += 9;\n i++;\n }\n else if (sub_str == \"XL\")\n {\n sum += 40;\n i++;\n }\n else if (sub_str == \"XC\")\n {\n sum += 90;\n i++;\n }\n else if (sub_str == \"CD\")\n {\n sum += 400;\n i++;\n }\n else if (sub_str == \"CM\")\n {\n sum += 900;\n i++;\n }\n else\n {\n if (s[i] == 'I')\n {\n sum += 1;\n }\n else if (s[i] == 'X')\n {\n sum += 10;\n }\n else if (s[i] == 'C')\n {\n sum += 100;\n }\n \n }\n }\n else\n {\n if (s[i] == 'V')\n {\n sum += 5;\n }\n else if (s[i] == 'L')\n {\n sum += 50;\n\n }\n else if (s[i] == 'D')\n {\n sum += 500;\n }\n else if (s[i] == 'M')\n {\n sum += 1000;\n }\n }\n \n }\n else\n {\n if (s[i] == 'V')\n {\n sum += 5;\n }\n else if (s[i] == 'L')\n {\n sum += 50;\n\n }\n else if (s[i] == 'D')\n {\n sum += 500;\n }\n else if (s[i] == 'M')\n {\n sum += 1000;\n }\n else if (s[i] == 'I')\n {\n sum += 1;\n }\n else if (s[i] == 'X')\n {\n sum += 10;\n }\n else if (s[i] == 'C')\n {\n sum += 100;\n }\n }\n }\n\n return sum;\n }\n};",
"memory": "11900"
} |
13 | <p>Roman numerals are represented by seven different symbols: <code>I</code>, <code>V</code>, <code>X</code>, <code>L</code>, <code>C</code>, <code>D</code> and <code>M</code>.</p>
<pre>
<strong>Symbol</strong> <strong>Value</strong>
I 1
V 5
X 10
L 50
C 100
D 500
M 1000</pre>
<p>For example, <code>2</code> is written as <code>II</code> in Roman numeral, just two ones added together. <code>12</code> is written as <code>XII</code>, which is simply <code>X + II</code>. The number <code>27</code> is written as <code>XXVII</code>, which is <code>XX + V + II</code>.</p>
<p>Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not <code>IIII</code>. Instead, the number four is written as <code>IV</code>. Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as <code>IX</code>. There are six instances where subtraction is used:</p>
<ul>
<li><code>I</code> can be placed before <code>V</code> (5) and <code>X</code> (10) to make 4 and 9. </li>
<li><code>X</code> can be placed before <code>L</code> (50) and <code>C</code> (100) to make 40 and 90. </li>
<li><code>C</code> can be placed before <code>D</code> (500) and <code>M</code> (1000) to make 400 and 900.</li>
</ul>
<p>Given a roman numeral, convert it to an integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "III"
<strong>Output:</strong> 3
<strong>Explanation:</strong> III = 3.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "LVIII"
<strong>Output:</strong> 58
<strong>Explanation:</strong> L = 50, V= 5, III = 3.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "MCMXCIV"
<strong>Output:</strong> 1994
<strong>Explanation:</strong> M = 1000, CM = 900, XC = 90 and IV = 4.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 15</code></li>
<li><code>s</code> contains only the characters <code>('I', 'V', 'X', 'L', 'C', 'D', 'M')</code>.</li>
<li>It is <strong>guaranteed</strong> that <code>s</code> is a valid roman numeral in the range <code>[1, 3999]</code>.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int romanToInt(string s) {\n int arr[26]={0};\n arr[2]=100;\n arr[3]=500;\n arr[8]=1;\n arr[11]=50;\n arr[12]=1000;\n arr[21]=5;\n arr[23]=10;\n\n int res=0;\n for(int i=0;i<s.length();i++)\n {\n if(s[i]=='I'){\n if(s[i+1]=='V')\n { \n res+=4;\n i++;\n continue;\n } \n if(s[i+1]=='X')\n {\n res+=9;\n i++;\n continue;\n }\n }\n\n if(s[i]=='X')\n {\n if(s[i+1]=='L')\n { \n res+=40;\n i++;\n continue;\n } \n if(s[i+1]=='C')\n {\n res+=90;\n i++;\n continue;\n } \n \n }\n\n if(s[i]=='C')\n {\n if(s[i+1]=='D')\n {\n res+=400;\n i++;\n continue;\n } \n if(s[i+1]=='M')\n { \n res+=900;\n i++;\n continue;\n } \n }\n\n int num=s[i]-'A';\n //cout<<s[i]<<\" \"<<num<<endl;\n res+=arr[num];\n //cout<<res<<endl;\n }\n return res;\n }\n};",
"memory": "11900"
} |
13 | <p>Roman numerals are represented by seven different symbols: <code>I</code>, <code>V</code>, <code>X</code>, <code>L</code>, <code>C</code>, <code>D</code> and <code>M</code>.</p>
<pre>
<strong>Symbol</strong> <strong>Value</strong>
I 1
V 5
X 10
L 50
C 100
D 500
M 1000</pre>
<p>For example, <code>2</code> is written as <code>II</code> in Roman numeral, just two ones added together. <code>12</code> is written as <code>XII</code>, which is simply <code>X + II</code>. The number <code>27</code> is written as <code>XXVII</code>, which is <code>XX + V + II</code>.</p>
<p>Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not <code>IIII</code>. Instead, the number four is written as <code>IV</code>. Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as <code>IX</code>. There are six instances where subtraction is used:</p>
<ul>
<li><code>I</code> can be placed before <code>V</code> (5) and <code>X</code> (10) to make 4 and 9. </li>
<li><code>X</code> can be placed before <code>L</code> (50) and <code>C</code> (100) to make 40 and 90. </li>
<li><code>C</code> can be placed before <code>D</code> (500) and <code>M</code> (1000) to make 400 and 900.</li>
</ul>
<p>Given a roman numeral, convert it to an integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "III"
<strong>Output:</strong> 3
<strong>Explanation:</strong> III = 3.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "LVIII"
<strong>Output:</strong> 58
<strong>Explanation:</strong> L = 50, V= 5, III = 3.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "MCMXCIV"
<strong>Output:</strong> 1994
<strong>Explanation:</strong> M = 1000, CM = 900, XC = 90 and IV = 4.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 15</code></li>
<li><code>s</code> contains only the characters <code>('I', 'V', 'X', 'L', 'C', 'D', 'M')</code>.</li>
<li>It is <strong>guaranteed</strong> that <code>s</code> is a valid roman numeral in the range <code>[1, 3999]</code>.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int romanToInt(string s) {\n int solution = 0;\n int value = 0;\n\n for (int i=0; i < s.length() - 1; i++){\n\n value = getLetterValue(s[i]);\n\n if (isSubtracting(s[i], s[i+1])){\n solution -= value;\n }\n else{\n solution += value;\n }\n }\n solution += getLetterValue(s[s.length()-1]);\n return solution;\n }\n\n int getLetterValue(char letter){\n\n char letters_order[7] = {'I', 'V', 'X', 'L', 'C', 'D', 'M'};\n int letters_value[7] = {1, 5, 10, 50, 100, 500, 1000};\n\n for (int i = 0 ; i < 7 ; i++){\n if (letter == letters_order[i])\n return letters_value[i];\n }\n\n return 0;\n }\n\n bool isSubtracting (char letter, char next_letter){\n\n int idx_letter = 0; int idx_next_letter = 0;\n char letters_order[7] = {'I', 'V', 'X', 'L', 'C', 'D', 'M'};\n\n for (int i = 0 ; i < 7 ; i++){\n if (letter == letters_order[i])\n idx_letter = i;\n if (next_letter == letters_order[i])\n idx_next_letter = i;\n }\n\n return idx_next_letter > idx_letter;\n }\n};",
"memory": "12000"
} |
13 | <p>Roman numerals are represented by seven different symbols: <code>I</code>, <code>V</code>, <code>X</code>, <code>L</code>, <code>C</code>, <code>D</code> and <code>M</code>.</p>
<pre>
<strong>Symbol</strong> <strong>Value</strong>
I 1
V 5
X 10
L 50
C 100
D 500
M 1000</pre>
<p>For example, <code>2</code> is written as <code>II</code> in Roman numeral, just two ones added together. <code>12</code> is written as <code>XII</code>, which is simply <code>X + II</code>. The number <code>27</code> is written as <code>XXVII</code>, which is <code>XX + V + II</code>.</p>
<p>Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not <code>IIII</code>. Instead, the number four is written as <code>IV</code>. Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as <code>IX</code>. There are six instances where subtraction is used:</p>
<ul>
<li><code>I</code> can be placed before <code>V</code> (5) and <code>X</code> (10) to make 4 and 9. </li>
<li><code>X</code> can be placed before <code>L</code> (50) and <code>C</code> (100) to make 40 and 90. </li>
<li><code>C</code> can be placed before <code>D</code> (500) and <code>M</code> (1000) to make 400 and 900.</li>
</ul>
<p>Given a roman numeral, convert it to an integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "III"
<strong>Output:</strong> 3
<strong>Explanation:</strong> III = 3.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "LVIII"
<strong>Output:</strong> 58
<strong>Explanation:</strong> L = 50, V= 5, III = 3.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "MCMXCIV"
<strong>Output:</strong> 1994
<strong>Explanation:</strong> M = 1000, CM = 900, XC = 90 and IV = 4.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 15</code></li>
<li><code>s</code> contains only the characters <code>('I', 'V', 'X', 'L', 'C', 'D', 'M')</code>.</li>
<li>It is <strong>guaranteed</strong> that <code>s</code> is a valid roman numeral in the range <code>[1, 3999]</code>.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int romanToInt(string s) {\n int val = 0;\n int length = s.size();\n\n int values[128];\n values['I'] = 1;\n values['V'] = 5;\n values['X'] = 10;\n values['L'] = 50;\n values['C'] = 100;\n values['D'] = 500;\n values['M'] = 1000;\n\n val += values[s[0]];\n\n for(int i=1; i<length; i++) {\n char c = s[i];\n val += values[c];\n\n if(c == 'V' || c == 'X') {\n if(s[i-1] == 'I')\n val -= 2;\n } else if(c == 'L' || c == 'C') {\n if(s[i-1] == 'X')\n val -= 20;\n } else if(c == 'D' || c == 'M') {\n if(s[i-1] == 'C')\n val -= 200;\n }\n }\n\n return val;\n }\n};",
"memory": "12000"
} |
13 | <p>Roman numerals are represented by seven different symbols: <code>I</code>, <code>V</code>, <code>X</code>, <code>L</code>, <code>C</code>, <code>D</code> and <code>M</code>.</p>
<pre>
<strong>Symbol</strong> <strong>Value</strong>
I 1
V 5
X 10
L 50
C 100
D 500
M 1000</pre>
<p>For example, <code>2</code> is written as <code>II</code> in Roman numeral, just two ones added together. <code>12</code> is written as <code>XII</code>, which is simply <code>X + II</code>. The number <code>27</code> is written as <code>XXVII</code>, which is <code>XX + V + II</code>.</p>
<p>Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not <code>IIII</code>. Instead, the number four is written as <code>IV</code>. Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as <code>IX</code>. There are six instances where subtraction is used:</p>
<ul>
<li><code>I</code> can be placed before <code>V</code> (5) and <code>X</code> (10) to make 4 and 9. </li>
<li><code>X</code> can be placed before <code>L</code> (50) and <code>C</code> (100) to make 40 and 90. </li>
<li><code>C</code> can be placed before <code>D</code> (500) and <code>M</code> (1000) to make 400 and 900.</li>
</ul>
<p>Given a roman numeral, convert it to an integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "III"
<strong>Output:</strong> 3
<strong>Explanation:</strong> III = 3.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "LVIII"
<strong>Output:</strong> 58
<strong>Explanation:</strong> L = 50, V= 5, III = 3.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "MCMXCIV"
<strong>Output:</strong> 1994
<strong>Explanation:</strong> M = 1000, CM = 900, XC = 90 and IV = 4.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 15</code></li>
<li><code>s</code> contains only the characters <code>('I', 'V', 'X', 'L', 'C', 'D', 'M')</code>.</li>
<li>It is <strong>guaranteed</strong> that <code>s</code> is a valid roman numeral in the range <code>[1, 3999]</code>.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int romanToInt(string s) {\n int m[1000];\n m['I'] = 1;\n m['V'] = 5;\n m['X'] = 10;\n m['L'] = 50;\n m['C'] = 100;\n m['D'] = 500;\n m['M'] = 1000;\n int ans = 0;\n for(int i=0;i<s.size();i++){\n if(m[s[i]] < m[s[i+1]]){\n ans -= m[s[i]];\n }\n else{\n ans += m[s[i]];\n }\n }\n return ans;\n }\n};",
"memory": "12100"
} |
13 | <p>Roman numerals are represented by seven different symbols: <code>I</code>, <code>V</code>, <code>X</code>, <code>L</code>, <code>C</code>, <code>D</code> and <code>M</code>.</p>
<pre>
<strong>Symbol</strong> <strong>Value</strong>
I 1
V 5
X 10
L 50
C 100
D 500
M 1000</pre>
<p>For example, <code>2</code> is written as <code>II</code> in Roman numeral, just two ones added together. <code>12</code> is written as <code>XII</code>, which is simply <code>X + II</code>. The number <code>27</code> is written as <code>XXVII</code>, which is <code>XX + V + II</code>.</p>
<p>Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not <code>IIII</code>. Instead, the number four is written as <code>IV</code>. Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as <code>IX</code>. There are six instances where subtraction is used:</p>
<ul>
<li><code>I</code> can be placed before <code>V</code> (5) and <code>X</code> (10) to make 4 and 9. </li>
<li><code>X</code> can be placed before <code>L</code> (50) and <code>C</code> (100) to make 40 and 90. </li>
<li><code>C</code> can be placed before <code>D</code> (500) and <code>M</code> (1000) to make 400 and 900.</li>
</ul>
<p>Given a roman numeral, convert it to an integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "III"
<strong>Output:</strong> 3
<strong>Explanation:</strong> III = 3.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "LVIII"
<strong>Output:</strong> 58
<strong>Explanation:</strong> L = 50, V= 5, III = 3.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "MCMXCIV"
<strong>Output:</strong> 1994
<strong>Explanation:</strong> M = 1000, CM = 900, XC = 90 and IV = 4.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 15</code></li>
<li><code>s</code> contains only the characters <code>('I', 'V', 'X', 'L', 'C', 'D', 'M')</code>.</li>
<li>It is <strong>guaranteed</strong> that <code>s</code> is a valid roman numeral in the range <code>[1, 3999]</code>.</li>
</ul>
| 0 | {
"code": "class Solution{\npublic:\n int romanToInt(string s){\n int values[1001];\n values[0+'I']=1;\n values[0+'V']=5;\n values[0+'X']=10;\n values[0+'L']=50;\n values[0+'C']=100;\n values[0+'D']=500;\n values[0+'M']=1000;\n int sum=values[int(s[0])],a;\n float b;\n for(int i=1;i<size(s);i++){\n a=values[int(s[i])];\n b=log10(values[int(s[i-1])]);\n if(b-int(b)==0&&(a/values[int(s[i-1])]==10||a/values[int(s[i-1])]==5)){\n sum+=a-2*values[int(s[i-1])];\n }\n else\n sum+=a;\n }\n return sum;\n }\n};",
"memory": "12200"
} |
13 | <p>Roman numerals are represented by seven different symbols: <code>I</code>, <code>V</code>, <code>X</code>, <code>L</code>, <code>C</code>, <code>D</code> and <code>M</code>.</p>
<pre>
<strong>Symbol</strong> <strong>Value</strong>
I 1
V 5
X 10
L 50
C 100
D 500
M 1000</pre>
<p>For example, <code>2</code> is written as <code>II</code> in Roman numeral, just two ones added together. <code>12</code> is written as <code>XII</code>, which is simply <code>X + II</code>. The number <code>27</code> is written as <code>XXVII</code>, which is <code>XX + V + II</code>.</p>
<p>Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not <code>IIII</code>. Instead, the number four is written as <code>IV</code>. Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as <code>IX</code>. There are six instances where subtraction is used:</p>
<ul>
<li><code>I</code> can be placed before <code>V</code> (5) and <code>X</code> (10) to make 4 and 9. </li>
<li><code>X</code> can be placed before <code>L</code> (50) and <code>C</code> (100) to make 40 and 90. </li>
<li><code>C</code> can be placed before <code>D</code> (500) and <code>M</code> (1000) to make 400 and 900.</li>
</ul>
<p>Given a roman numeral, convert it to an integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "III"
<strong>Output:</strong> 3
<strong>Explanation:</strong> III = 3.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "LVIII"
<strong>Output:</strong> 58
<strong>Explanation:</strong> L = 50, V= 5, III = 3.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "MCMXCIV"
<strong>Output:</strong> 1994
<strong>Explanation:</strong> M = 1000, CM = 900, XC = 90 and IV = 4.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 15</code></li>
<li><code>s</code> contains only the characters <code>('I', 'V', 'X', 'L', 'C', 'D', 'M')</code>.</li>
<li>It is <strong>guaranteed</strong> that <code>s</code> is a valid roman numeral in the range <code>[1, 3999]</code>.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n vector<char> chrList = {'I', 'V', 'X', 'L', 'C', 'D', 'M'};\n vector<int> numList = {1, 5, 10, 50, 100, 500, 1000};\n \n int getValue(string s, int loc){\n if(loc >= s.size()) return -1;\n \n for(int i = 0; i < chrList.size(); i++)\n if(s[loc] == chrList[i])\n return numList[i];\n \n return -1;\n }\n \n int romanToInt(string s) {\n int ans = 0;\n \n for(int i = 0; i < s.size(); i++){\n int curValue = getValue(s, i);\n int nextValue = getValue(s, i+1);\n \n if(curValue < nextValue)\n ans += nextValue - curValue, i++;\n else\n ans += curValue;\n }\n \n return ans;\n }\n};",
"memory": "12300"
} |
13 | <p>Roman numerals are represented by seven different symbols: <code>I</code>, <code>V</code>, <code>X</code>, <code>L</code>, <code>C</code>, <code>D</code> and <code>M</code>.</p>
<pre>
<strong>Symbol</strong> <strong>Value</strong>
I 1
V 5
X 10
L 50
C 100
D 500
M 1000</pre>
<p>For example, <code>2</code> is written as <code>II</code> in Roman numeral, just two ones added together. <code>12</code> is written as <code>XII</code>, which is simply <code>X + II</code>. The number <code>27</code> is written as <code>XXVII</code>, which is <code>XX + V + II</code>.</p>
<p>Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not <code>IIII</code>. Instead, the number four is written as <code>IV</code>. Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as <code>IX</code>. There are six instances where subtraction is used:</p>
<ul>
<li><code>I</code> can be placed before <code>V</code> (5) and <code>X</code> (10) to make 4 and 9. </li>
<li><code>X</code> can be placed before <code>L</code> (50) and <code>C</code> (100) to make 40 and 90. </li>
<li><code>C</code> can be placed before <code>D</code> (500) and <code>M</code> (1000) to make 400 and 900.</li>
</ul>
<p>Given a roman numeral, convert it to an integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "III"
<strong>Output:</strong> 3
<strong>Explanation:</strong> III = 3.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "LVIII"
<strong>Output:</strong> 58
<strong>Explanation:</strong> L = 50, V= 5, III = 3.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "MCMXCIV"
<strong>Output:</strong> 1994
<strong>Explanation:</strong> M = 1000, CM = 900, XC = 90 and IV = 4.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 15</code></li>
<li><code>s</code> contains only the characters <code>('I', 'V', 'X', 'L', 'C', 'D', 'M')</code>.</li>
<li>It is <strong>guaranteed</strong> that <code>s</code> is a valid roman numeral in the range <code>[1, 3999]</code>.</li>
</ul>
| 0 | {
"code": "class Solution {\nprivate:\n std::vector<char> m_symbolRoman{'I', 'V', 'X', 'L', 'C', 'D', 'M'};\n std::vector<int> m_valueInteger{1, 5, 10, 50, 100, 500, 1000};\npublic:\n int romanToInt(string s) {\n //assert(m_symbolRoman.size() == m_valueInteger.size());\n\n int sum{0};\n for(char roma : s){\n switch(roma){\n case 'I': sum += 1; break;\n case 'V': sum += 5; break;\n case 'X': sum += 10; break;\n case 'L': sum += 50; break;\n case 'C': sum += 100; break;\n case 'D': sum += 500; break;\n case 'M': sum += 1000; break;\n default : break;\n }\n }\n\n return sum - romanNumberContainSubtruct(s);\n }\n\n int romanNumberContainSubtruct(string& s){\n int subtract {0};\n if (s.find(\"CD\") != string::npos || s.find(\"CM\") != string::npos)\n subtract += 2 * 100;\n if (s.find(\"XL\") != string::npos || s.find(\"XC\") != string::npos)\n subtract += 2 * 10;\n if (s.find(\"IV\") != string::npos || s.find(\"IX\") != string::npos)\n subtract += 2 * 1;\n\n return subtract;\n }\n};",
"memory": "12400"
} |
13 | <p>Roman numerals are represented by seven different symbols: <code>I</code>, <code>V</code>, <code>X</code>, <code>L</code>, <code>C</code>, <code>D</code> and <code>M</code>.</p>
<pre>
<strong>Symbol</strong> <strong>Value</strong>
I 1
V 5
X 10
L 50
C 100
D 500
M 1000</pre>
<p>For example, <code>2</code> is written as <code>II</code> in Roman numeral, just two ones added together. <code>12</code> is written as <code>XII</code>, which is simply <code>X + II</code>. The number <code>27</code> is written as <code>XXVII</code>, which is <code>XX + V + II</code>.</p>
<p>Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not <code>IIII</code>. Instead, the number four is written as <code>IV</code>. Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as <code>IX</code>. There are six instances where subtraction is used:</p>
<ul>
<li><code>I</code> can be placed before <code>V</code> (5) and <code>X</code> (10) to make 4 and 9. </li>
<li><code>X</code> can be placed before <code>L</code> (50) and <code>C</code> (100) to make 40 and 90. </li>
<li><code>C</code> can be placed before <code>D</code> (500) and <code>M</code> (1000) to make 400 and 900.</li>
</ul>
<p>Given a roman numeral, convert it to an integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "III"
<strong>Output:</strong> 3
<strong>Explanation:</strong> III = 3.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "LVIII"
<strong>Output:</strong> 58
<strong>Explanation:</strong> L = 50, V= 5, III = 3.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "MCMXCIV"
<strong>Output:</strong> 1994
<strong>Explanation:</strong> M = 1000, CM = 900, XC = 90 and IV = 4.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 15</code></li>
<li><code>s</code> contains only the characters <code>('I', 'V', 'X', 'L', 'C', 'D', 'M')</code>.</li>
<li>It is <strong>guaranteed</strong> that <code>s</code> is a valid roman numeral in the range <code>[1, 3999]</code>.</li>
</ul>
| 0 | {
"code": "class Solution\n{\npublic:\n vector<pair<char, int>> wordMap = {{'I', 1}, {'V', 5}, {'X', 10}, {'L', 50}, {'C', 100}, {'D', 500}, {'M', 1000}};\n\n int getVal(char ch)\n {\n for (pair<char, int> K : wordMap)\n {\n if (K.first == ch)\n {\n return K.second;\n }\n }\n\n return 0;\n }\n\n int romanToInt(string s)\n {\n int value = 0, i = 0;\n while (i < s.length() - 1)\n {\n if (getVal(s[i]) < getVal(s[i + 1]))\n {\n value += (getVal(s[i + 1]) - getVal(s[i]));\n i += 2;\n }\n else\n {\n value += getVal(s[i]);\n i++;\n }\n }\n\n if (i == s.length() - 1)\n {\n value += getVal(s[i]);\n }\n\n return value;\n }\n};",
"memory": "12500"
} |
13 | <p>Roman numerals are represented by seven different symbols: <code>I</code>, <code>V</code>, <code>X</code>, <code>L</code>, <code>C</code>, <code>D</code> and <code>M</code>.</p>
<pre>
<strong>Symbol</strong> <strong>Value</strong>
I 1
V 5
X 10
L 50
C 100
D 500
M 1000</pre>
<p>For example, <code>2</code> is written as <code>II</code> in Roman numeral, just two ones added together. <code>12</code> is written as <code>XII</code>, which is simply <code>X + II</code>. The number <code>27</code> is written as <code>XXVII</code>, which is <code>XX + V + II</code>.</p>
<p>Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not <code>IIII</code>. Instead, the number four is written as <code>IV</code>. Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as <code>IX</code>. There are six instances where subtraction is used:</p>
<ul>
<li><code>I</code> can be placed before <code>V</code> (5) and <code>X</code> (10) to make 4 and 9. </li>
<li><code>X</code> can be placed before <code>L</code> (50) and <code>C</code> (100) to make 40 and 90. </li>
<li><code>C</code> can be placed before <code>D</code> (500) and <code>M</code> (1000) to make 400 and 900.</li>
</ul>
<p>Given a roman numeral, convert it to an integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "III"
<strong>Output:</strong> 3
<strong>Explanation:</strong> III = 3.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "LVIII"
<strong>Output:</strong> 58
<strong>Explanation:</strong> L = 50, V= 5, III = 3.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "MCMXCIV"
<strong>Output:</strong> 1994
<strong>Explanation:</strong> M = 1000, CM = 900, XC = 90 and IV = 4.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 15</code></li>
<li><code>s</code> contains only the characters <code>('I', 'V', 'X', 'L', 'C', 'D', 'M')</code>.</li>
<li>It is <strong>guaranteed</strong> that <code>s</code> is a valid roman numeral in the range <code>[1, 3999]</code>.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int romanToInt(string s) {\n unordered_map<char, int> nums{\n {'I', 1},\n {'V', 5},\n {'X', 10},\n {'L', 50},\n {'C', 100},\n {'D', 500},\n {'M', 1000},\n };\n int ans = nums[s.back()];\n for (int i = 0; i < s.size() - 1; ++i) {\n int sign = nums[s[i]] < nums[s[i + 1]] ? -1 : 1;\n ans += sign * nums[s[i]];\n }\n return ans;\n }\n};",
"memory": "12600"
} |
13 | <p>Roman numerals are represented by seven different symbols: <code>I</code>, <code>V</code>, <code>X</code>, <code>L</code>, <code>C</code>, <code>D</code> and <code>M</code>.</p>
<pre>
<strong>Symbol</strong> <strong>Value</strong>
I 1
V 5
X 10
L 50
C 100
D 500
M 1000</pre>
<p>For example, <code>2</code> is written as <code>II</code> in Roman numeral, just two ones added together. <code>12</code> is written as <code>XII</code>, which is simply <code>X + II</code>. The number <code>27</code> is written as <code>XXVII</code>, which is <code>XX + V + II</code>.</p>
<p>Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not <code>IIII</code>. Instead, the number four is written as <code>IV</code>. Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as <code>IX</code>. There are six instances where subtraction is used:</p>
<ul>
<li><code>I</code> can be placed before <code>V</code> (5) and <code>X</code> (10) to make 4 and 9. </li>
<li><code>X</code> can be placed before <code>L</code> (50) and <code>C</code> (100) to make 40 and 90. </li>
<li><code>C</code> can be placed before <code>D</code> (500) and <code>M</code> (1000) to make 400 and 900.</li>
</ul>
<p>Given a roman numeral, convert it to an integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "III"
<strong>Output:</strong> 3
<strong>Explanation:</strong> III = 3.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "LVIII"
<strong>Output:</strong> 58
<strong>Explanation:</strong> L = 50, V= 5, III = 3.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "MCMXCIV"
<strong>Output:</strong> 1994
<strong>Explanation:</strong> M = 1000, CM = 900, XC = 90 and IV = 4.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 15</code></li>
<li><code>s</code> contains only the characters <code>('I', 'V', 'X', 'L', 'C', 'D', 'M')</code>.</li>
<li>It is <strong>guaranteed</strong> that <code>s</code> is a valid roman numeral in the range <code>[1, 3999]</code>.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n \n int romanToInt(string s) {\n unordered_map<char,int> m={{'I',1},{'V',5},{'X',10},{'L',50},{'C',100},{'D',500},{'M',1000}};\n int ans=0;\n for(int i=s.size()-1;i>=0; i--){\n ans+=m[s[i]];\n if(i-1>=0&&m[s[i-1]]<m[s[i]]){\n ans-=m[s[i-1]];\n i--;\n }\n }\n return ans;\n }\n};",
"memory": "12600"
} |
13 | <p>Roman numerals are represented by seven different symbols: <code>I</code>, <code>V</code>, <code>X</code>, <code>L</code>, <code>C</code>, <code>D</code> and <code>M</code>.</p>
<pre>
<strong>Symbol</strong> <strong>Value</strong>
I 1
V 5
X 10
L 50
C 100
D 500
M 1000</pre>
<p>For example, <code>2</code> is written as <code>II</code> in Roman numeral, just two ones added together. <code>12</code> is written as <code>XII</code>, which is simply <code>X + II</code>. The number <code>27</code> is written as <code>XXVII</code>, which is <code>XX + V + II</code>.</p>
<p>Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not <code>IIII</code>. Instead, the number four is written as <code>IV</code>. Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as <code>IX</code>. There are six instances where subtraction is used:</p>
<ul>
<li><code>I</code> can be placed before <code>V</code> (5) and <code>X</code> (10) to make 4 and 9. </li>
<li><code>X</code> can be placed before <code>L</code> (50) and <code>C</code> (100) to make 40 and 90. </li>
<li><code>C</code> can be placed before <code>D</code> (500) and <code>M</code> (1000) to make 400 and 900.</li>
</ul>
<p>Given a roman numeral, convert it to an integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "III"
<strong>Output:</strong> 3
<strong>Explanation:</strong> III = 3.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "LVIII"
<strong>Output:</strong> 58
<strong>Explanation:</strong> L = 50, V= 5, III = 3.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "MCMXCIV"
<strong>Output:</strong> 1994
<strong>Explanation:</strong> M = 1000, CM = 900, XC = 90 and IV = 4.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 15</code></li>
<li><code>s</code> contains only the characters <code>('I', 'V', 'X', 'L', 'C', 'D', 'M')</code>.</li>
<li>It is <strong>guaranteed</strong> that <code>s</code> is a valid roman numeral in the range <code>[1, 3999]</code>.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int romanToInt(string s) {\n unordered_map<char, int> romanMap = {\n {'I', 1}, {'V', 5}, {'X', 10},\n {'L', 50}, {'C', 100}, {'D', 500}, {'M', 1000}\n };\n \n int total = 0;\n int n = s.length();\n \n for (int i = 0; i < n; ++i) {\n if (i < n - 1 && romanMap[s[i]] < romanMap[s[i + 1]]) {\n total -= romanMap[s[i]];\n } else {\n total += romanMap[s[i]];\n }\n }\n \n return total;\n \n }\n};",
"memory": "12700"
} |
13 | <p>Roman numerals are represented by seven different symbols: <code>I</code>, <code>V</code>, <code>X</code>, <code>L</code>, <code>C</code>, <code>D</code> and <code>M</code>.</p>
<pre>
<strong>Symbol</strong> <strong>Value</strong>
I 1
V 5
X 10
L 50
C 100
D 500
M 1000</pre>
<p>For example, <code>2</code> is written as <code>II</code> in Roman numeral, just two ones added together. <code>12</code> is written as <code>XII</code>, which is simply <code>X + II</code>. The number <code>27</code> is written as <code>XXVII</code>, which is <code>XX + V + II</code>.</p>
<p>Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not <code>IIII</code>. Instead, the number four is written as <code>IV</code>. Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as <code>IX</code>. There are six instances where subtraction is used:</p>
<ul>
<li><code>I</code> can be placed before <code>V</code> (5) and <code>X</code> (10) to make 4 and 9. </li>
<li><code>X</code> can be placed before <code>L</code> (50) and <code>C</code> (100) to make 40 and 90. </li>
<li><code>C</code> can be placed before <code>D</code> (500) and <code>M</code> (1000) to make 400 and 900.</li>
</ul>
<p>Given a roman numeral, convert it to an integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "III"
<strong>Output:</strong> 3
<strong>Explanation:</strong> III = 3.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "LVIII"
<strong>Output:</strong> 58
<strong>Explanation:</strong> L = 50, V= 5, III = 3.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "MCMXCIV"
<strong>Output:</strong> 1994
<strong>Explanation:</strong> M = 1000, CM = 900, XC = 90 and IV = 4.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 15</code></li>
<li><code>s</code> contains only the characters <code>('I', 'V', 'X', 'L', 'C', 'D', 'M')</code>.</li>
<li>It is <strong>guaranteed</strong> that <code>s</code> is a valid roman numeral in the range <code>[1, 3999]</code>.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int romanToInt(string s) {\n unordered_map<char, int> nums{\n {'I', 1},\n {'V', 5},\n {'X', 10},\n {'L', 50},\n {'C', 100},\n {'D', 500},\n {'M', 1000},\n };\n int ans = nums[s.back()];\n for (int i = 0; i < s.size() - 1; ++i) {\n int sign = nums[s[i]] < nums[s[i + 1]] ? -1 : 1;\n ans += sign * nums[s[i]];\n }\n return ans;\n }\n};",
"memory": "12700"
} |
13 | <p>Roman numerals are represented by seven different symbols: <code>I</code>, <code>V</code>, <code>X</code>, <code>L</code>, <code>C</code>, <code>D</code> and <code>M</code>.</p>
<pre>
<strong>Symbol</strong> <strong>Value</strong>
I 1
V 5
X 10
L 50
C 100
D 500
M 1000</pre>
<p>For example, <code>2</code> is written as <code>II</code> in Roman numeral, just two ones added together. <code>12</code> is written as <code>XII</code>, which is simply <code>X + II</code>. The number <code>27</code> is written as <code>XXVII</code>, which is <code>XX + V + II</code>.</p>
<p>Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not <code>IIII</code>. Instead, the number four is written as <code>IV</code>. Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as <code>IX</code>. There are six instances where subtraction is used:</p>
<ul>
<li><code>I</code> can be placed before <code>V</code> (5) and <code>X</code> (10) to make 4 and 9. </li>
<li><code>X</code> can be placed before <code>L</code> (50) and <code>C</code> (100) to make 40 and 90. </li>
<li><code>C</code> can be placed before <code>D</code> (500) and <code>M</code> (1000) to make 400 and 900.</li>
</ul>
<p>Given a roman numeral, convert it to an integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "III"
<strong>Output:</strong> 3
<strong>Explanation:</strong> III = 3.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "LVIII"
<strong>Output:</strong> 58
<strong>Explanation:</strong> L = 50, V= 5, III = 3.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "MCMXCIV"
<strong>Output:</strong> 1994
<strong>Explanation:</strong> M = 1000, CM = 900, XC = 90 and IV = 4.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 15</code></li>
<li><code>s</code> contains only the characters <code>('I', 'V', 'X', 'L', 'C', 'D', 'M')</code>.</li>
<li>It is <strong>guaranteed</strong> that <code>s</code> is a valid roman numeral in the range <code>[1, 3999]</code>.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int romanToInt(std::string s)\n {\n\n\n std::unordered_map<char, int> roman =\n {\n {'I', 1},\n {'V', 5},\n {'X', 10},\n {'L', 50},\n {'C', 100},\n {'D', 500},\n {'M', 1000}\n };\n int total = 0;\n for (int i = 0; i < s.size(); i++)\n {\n if (roman[s[i]] == 1,10,100 && i != s.size())\n {\n if (roman[s[i]] < roman[s[i + 1]])\n {\n total += roman[s[i + 1]] - roman[s[i]];\n i++;\n }\n else\n {\n total += roman[s[i]];\n }\n }\n else {\n total += roman[s[i]];\n }\n \n }\n return total;\n }\n \n};",
"memory": "12800"
} |
13 | <p>Roman numerals are represented by seven different symbols: <code>I</code>, <code>V</code>, <code>X</code>, <code>L</code>, <code>C</code>, <code>D</code> and <code>M</code>.</p>
<pre>
<strong>Symbol</strong> <strong>Value</strong>
I 1
V 5
X 10
L 50
C 100
D 500
M 1000</pre>
<p>For example, <code>2</code> is written as <code>II</code> in Roman numeral, just two ones added together. <code>12</code> is written as <code>XII</code>, which is simply <code>X + II</code>. The number <code>27</code> is written as <code>XXVII</code>, which is <code>XX + V + II</code>.</p>
<p>Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not <code>IIII</code>. Instead, the number four is written as <code>IV</code>. Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as <code>IX</code>. There are six instances where subtraction is used:</p>
<ul>
<li><code>I</code> can be placed before <code>V</code> (5) and <code>X</code> (10) to make 4 and 9. </li>
<li><code>X</code> can be placed before <code>L</code> (50) and <code>C</code> (100) to make 40 and 90. </li>
<li><code>C</code> can be placed before <code>D</code> (500) and <code>M</code> (1000) to make 400 and 900.</li>
</ul>
<p>Given a roman numeral, convert it to an integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "III"
<strong>Output:</strong> 3
<strong>Explanation:</strong> III = 3.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "LVIII"
<strong>Output:</strong> 58
<strong>Explanation:</strong> L = 50, V= 5, III = 3.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "MCMXCIV"
<strong>Output:</strong> 1994
<strong>Explanation:</strong> M = 1000, CM = 900, XC = 90 and IV = 4.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 15</code></li>
<li><code>s</code> contains only the characters <code>('I', 'V', 'X', 'L', 'C', 'D', 'M')</code>.</li>
<li>It is <strong>guaranteed</strong> that <code>s</code> is a valid roman numeral in the range <code>[1, 3999]</code>.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n int romanToInt(string s) {\n unordered_map<char, int> nums{\n {'I', 1},\n {'V', 5},\n {'X', 10},\n {'L', 50},\n {'C', 100},\n {'D', 500},\n {'M', 1000},\n };\n int ans = nums[s.back()];\n for (int i = 0; i < s.size() - 1; ++i) {\n int sign = nums[s[i]] < nums[s[i + 1]] ? -1 : 1;\n ans += sign * nums[s[i]];\n }\n return ans;\n }\n};",
"memory": "12800"
} |
13 | <p>Roman numerals are represented by seven different symbols: <code>I</code>, <code>V</code>, <code>X</code>, <code>L</code>, <code>C</code>, <code>D</code> and <code>M</code>.</p>
<pre>
<strong>Symbol</strong> <strong>Value</strong>
I 1
V 5
X 10
L 50
C 100
D 500
M 1000</pre>
<p>For example, <code>2</code> is written as <code>II</code> in Roman numeral, just two ones added together. <code>12</code> is written as <code>XII</code>, which is simply <code>X + II</code>. The number <code>27</code> is written as <code>XXVII</code>, which is <code>XX + V + II</code>.</p>
<p>Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not <code>IIII</code>. Instead, the number four is written as <code>IV</code>. Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as <code>IX</code>. There are six instances where subtraction is used:</p>
<ul>
<li><code>I</code> can be placed before <code>V</code> (5) and <code>X</code> (10) to make 4 and 9. </li>
<li><code>X</code> can be placed before <code>L</code> (50) and <code>C</code> (100) to make 40 and 90. </li>
<li><code>C</code> can be placed before <code>D</code> (500) and <code>M</code> (1000) to make 400 and 900.</li>
</ul>
<p>Given a roman numeral, convert it to an integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "III"
<strong>Output:</strong> 3
<strong>Explanation:</strong> III = 3.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "LVIII"
<strong>Output:</strong> 58
<strong>Explanation:</strong> L = 50, V= 5, III = 3.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "MCMXCIV"
<strong>Output:</strong> 1994
<strong>Explanation:</strong> M = 1000, CM = 900, XC = 90 and IV = 4.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 15</code></li>
<li><code>s</code> contains only the characters <code>('I', 'V', 'X', 'L', 'C', 'D', 'M')</code>.</li>
<li>It is <strong>guaranteed</strong> that <code>s</code> is a valid roman numeral in the range <code>[1, 3999]</code>.</li>
</ul>
| 1 | {
"code": "class Solution\n{\n std::unordered_map<char, int> romans = {\n { 'M', 1000 },\n { 'D', 500 },\n { 'C', 100 },\n { 'L', 50 },\n { 'X', 10 },\n { 'V', 5 },\n { 'I', 1 },\n };\n\npublic:\n int romanToInt(std::string s)\n {\n return solutionMapped(s);\n }\n\n int solutionMapped(const std::string& s)\n {\n int num = 0;\n\n for (int i = 0; i < s.size(); ++i) {\n if (i < s.size() - 1 && romans[s[i]] < romans[s[i + 1]]) {\n num -= romans[s[i]];\n }\n else {\n num += romans[s[i]];\n }\n }\n\n return num;\n }\n\n int solutionFast(const std::string& s)\n {\n int num = 0;\n\n for (int i = s.size() - 1; i >= 0; --i) {\n if (s[i] == 'M') {\n if (i > 0 && s[i - 1] == 'C') {\n num += 900;\n --i;\n }\n else {\n num += 1000;\n }\n }\n else if (s[i] == 'D') {\n if (i > 0 && s[i - 1] == 'C') {\n num += 400;\n --i;\n }\n else {\n num += 500;\n }\n }\n else if (s[i] == 'C') {\n if (i > 0 && s[i - 1] == 'X') {\n num += 90;\n --i;\n }\n else {\n num += 100;\n }\n }\n else if (s[i] == 'L') {\n if (i > 0 && s[i - 1] == 'X') {\n num += 40;\n --i;\n }\n else {\n num += 50;\n }\n }\n else if (s[i] == 'X') {\n if (i > 0 && s[i - 1] == 'I') {\n num += 9;\n --i;\n }\n else {\n num += 10;\n }\n }\n else if (s[i] == 'V') {\n if (i > 0 && s[i - 1] == 'I') {\n num += 4;\n --i;\n }\n else {\n num += 5;\n }\n }\n else if (s[i] == 'I') {\n num += 1;\n }\n }\n\n return num;\n }\n};",
"memory": "12900"
} |
13 | <p>Roman numerals are represented by seven different symbols: <code>I</code>, <code>V</code>, <code>X</code>, <code>L</code>, <code>C</code>, <code>D</code> and <code>M</code>.</p>
<pre>
<strong>Symbol</strong> <strong>Value</strong>
I 1
V 5
X 10
L 50
C 100
D 500
M 1000</pre>
<p>For example, <code>2</code> is written as <code>II</code> in Roman numeral, just two ones added together. <code>12</code> is written as <code>XII</code>, which is simply <code>X + II</code>. The number <code>27</code> is written as <code>XXVII</code>, which is <code>XX + V + II</code>.</p>
<p>Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not <code>IIII</code>. Instead, the number four is written as <code>IV</code>. Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as <code>IX</code>. There are six instances where subtraction is used:</p>
<ul>
<li><code>I</code> can be placed before <code>V</code> (5) and <code>X</code> (10) to make 4 and 9. </li>
<li><code>X</code> can be placed before <code>L</code> (50) and <code>C</code> (100) to make 40 and 90. </li>
<li><code>C</code> can be placed before <code>D</code> (500) and <code>M</code> (1000) to make 400 and 900.</li>
</ul>
<p>Given a roman numeral, convert it to an integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "III"
<strong>Output:</strong> 3
<strong>Explanation:</strong> III = 3.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "LVIII"
<strong>Output:</strong> 58
<strong>Explanation:</strong> L = 50, V= 5, III = 3.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "MCMXCIV"
<strong>Output:</strong> 1994
<strong>Explanation:</strong> M = 1000, CM = 900, XC = 90 and IV = 4.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 15</code></li>
<li><code>s</code> contains only the characters <code>('I', 'V', 'X', 'L', 'C', 'D', 'M')</code>.</li>
<li>It is <strong>guaranteed</strong> that <code>s</code> is a valid roman numeral in the range <code>[1, 3999]</code>.</li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n int romanToInt(string s) {\n unordered_map<char, int> map = {\n { 'I', 1 },\n { 'V', 5 },\n { 'X', 10 },\n { 'L', 50 },\n { 'C', 100 },\n { 'D', 500 },\n { 'M', 1000 }\n };\n\n int result = 0;\n\n for (int i = 0; i < s.size(); i++) {\n if (i + 1 < s.size() && map[s[i]] < map[s[i + 1]]) {\n result += map[s[i + 1]] - map[s[i]];\n i++;\n }\n\n else {\n result += map[s[i]];\n }\n }\n\n return result;\n }\n};",
"memory": "12900"
} |
13 | <p>Roman numerals are represented by seven different symbols: <code>I</code>, <code>V</code>, <code>X</code>, <code>L</code>, <code>C</code>, <code>D</code> and <code>M</code>.</p>
<pre>
<strong>Symbol</strong> <strong>Value</strong>
I 1
V 5
X 10
L 50
C 100
D 500
M 1000</pre>
<p>For example, <code>2</code> is written as <code>II</code> in Roman numeral, just two ones added together. <code>12</code> is written as <code>XII</code>, which is simply <code>X + II</code>. The number <code>27</code> is written as <code>XXVII</code>, which is <code>XX + V + II</code>.</p>
<p>Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not <code>IIII</code>. Instead, the number four is written as <code>IV</code>. Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as <code>IX</code>. There are six instances where subtraction is used:</p>
<ul>
<li><code>I</code> can be placed before <code>V</code> (5) and <code>X</code> (10) to make 4 and 9. </li>
<li><code>X</code> can be placed before <code>L</code> (50) and <code>C</code> (100) to make 40 and 90. </li>
<li><code>C</code> can be placed before <code>D</code> (500) and <code>M</code> (1000) to make 400 and 900.</li>
</ul>
<p>Given a roman numeral, convert it to an integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "III"
<strong>Output:</strong> 3
<strong>Explanation:</strong> III = 3.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "LVIII"
<strong>Output:</strong> 58
<strong>Explanation:</strong> L = 50, V= 5, III = 3.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "MCMXCIV"
<strong>Output:</strong> 1994
<strong>Explanation:</strong> M = 1000, CM = 900, XC = 90 and IV = 4.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 15</code></li>
<li><code>s</code> contains only the characters <code>('I', 'V', 'X', 'L', 'C', 'D', 'M')</code>.</li>
<li>It is <strong>guaranteed</strong> that <code>s</code> is a valid roman numeral in the range <code>[1, 3999]</code>.</li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n int romanToInt(string s) {\n unordered_map<char, int> map = {{'I', 1}, {'V', 5}, {'X', 10}, {'L', 50}, {'C', 100}, {'D', 500}, {'M', 1000}};\n\n int res = map[s[s.size()-1]];\n\n for(int i= s.size()-2; i>=0; i--){\n if(map[s[i]]>=map[s[i+1]]){\n res+=map[s[i]];\n }\n else{\n res-=map[s[i]];\n }\n }\n\n return res;\n \n }\n};",
"memory": "13000"
} |
13 | <p>Roman numerals are represented by seven different symbols: <code>I</code>, <code>V</code>, <code>X</code>, <code>L</code>, <code>C</code>, <code>D</code> and <code>M</code>.</p>
<pre>
<strong>Symbol</strong> <strong>Value</strong>
I 1
V 5
X 10
L 50
C 100
D 500
M 1000</pre>
<p>For example, <code>2</code> is written as <code>II</code> in Roman numeral, just two ones added together. <code>12</code> is written as <code>XII</code>, which is simply <code>X + II</code>. The number <code>27</code> is written as <code>XXVII</code>, which is <code>XX + V + II</code>.</p>
<p>Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not <code>IIII</code>. Instead, the number four is written as <code>IV</code>. Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as <code>IX</code>. There are six instances where subtraction is used:</p>
<ul>
<li><code>I</code> can be placed before <code>V</code> (5) and <code>X</code> (10) to make 4 and 9. </li>
<li><code>X</code> can be placed before <code>L</code> (50) and <code>C</code> (100) to make 40 and 90. </li>
<li><code>C</code> can be placed before <code>D</code> (500) and <code>M</code> (1000) to make 400 and 900.</li>
</ul>
<p>Given a roman numeral, convert it to an integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "III"
<strong>Output:</strong> 3
<strong>Explanation:</strong> III = 3.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "LVIII"
<strong>Output:</strong> 58
<strong>Explanation:</strong> L = 50, V= 5, III = 3.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "MCMXCIV"
<strong>Output:</strong> 1994
<strong>Explanation:</strong> M = 1000, CM = 900, XC = 90 and IV = 4.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 15</code></li>
<li><code>s</code> contains only the characters <code>('I', 'V', 'X', 'L', 'C', 'D', 'M')</code>.</li>
<li>It is <strong>guaranteed</strong> that <code>s</code> is a valid roman numeral in the range <code>[1, 3999]</code>.</li>
</ul>
| 1 | {
"code": "class Solution {\n public:\n int romanToInt(string s) {\n int ans = 0;\n vector<int> roman(128);\n\n roman['I'] = 1;\n roman['V'] = 5;\n roman['X'] = 10;\n roman['L'] = 50;\n roman['C'] = 100;\n roman['D'] = 500;\n roman['M'] = 1000;\n\n for (int i = 0; i + 1 < s.length(); ++i)\n if (roman[s[i]] < roman[s[i + 1]])\n ans -= roman[s[i]];\n else\n ans += roman[s[i]];\n\n return ans + roman[s.back()];\n }\n};",
"memory": "13500"
} |
13 | <p>Roman numerals are represented by seven different symbols: <code>I</code>, <code>V</code>, <code>X</code>, <code>L</code>, <code>C</code>, <code>D</code> and <code>M</code>.</p>
<pre>
<strong>Symbol</strong> <strong>Value</strong>
I 1
V 5
X 10
L 50
C 100
D 500
M 1000</pre>
<p>For example, <code>2</code> is written as <code>II</code> in Roman numeral, just two ones added together. <code>12</code> is written as <code>XII</code>, which is simply <code>X + II</code>. The number <code>27</code> is written as <code>XXVII</code>, which is <code>XX + V + II</code>.</p>
<p>Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not <code>IIII</code>. Instead, the number four is written as <code>IV</code>. Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as <code>IX</code>. There are six instances where subtraction is used:</p>
<ul>
<li><code>I</code> can be placed before <code>V</code> (5) and <code>X</code> (10) to make 4 and 9. </li>
<li><code>X</code> can be placed before <code>L</code> (50) and <code>C</code> (100) to make 40 and 90. </li>
<li><code>C</code> can be placed before <code>D</code> (500) and <code>M</code> (1000) to make 400 and 900.</li>
</ul>
<p>Given a roman numeral, convert it to an integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "III"
<strong>Output:</strong> 3
<strong>Explanation:</strong> III = 3.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "LVIII"
<strong>Output:</strong> 58
<strong>Explanation:</strong> L = 50, V= 5, III = 3.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "MCMXCIV"
<strong>Output:</strong> 1994
<strong>Explanation:</strong> M = 1000, CM = 900, XC = 90 and IV = 4.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 15</code></li>
<li><code>s</code> contains only the characters <code>('I', 'V', 'X', 'L', 'C', 'D', 'M')</code>.</li>
<li>It is <strong>guaranteed</strong> that <code>s</code> is a valid roman numeral in the range <code>[1, 3999]</code>.</li>
</ul>
| 1 | {
"code": "class Solution {\n public:\n int romanToInt(string s) {\n int ans = 0;\n vector<int> roman(128);\n\n roman['I'] = 1;\n roman['V'] = 5;\n roman['X'] = 10;\n roman['L'] = 50;\n roman['C'] = 100;\n roman['D'] = 500;\n roman['M'] = 1000;\n\n for (int i = 0; i + 1 < s.length(); ++i)\n if (roman[s[i]] < roman[s[i + 1]])\n ans -= roman[s[i]];\n else\n ans += roman[s[i]];\n\n return ans + roman[s.back()];\n }\n};",
"memory": "13500"
} |
13 | <p>Roman numerals are represented by seven different symbols: <code>I</code>, <code>V</code>, <code>X</code>, <code>L</code>, <code>C</code>, <code>D</code> and <code>M</code>.</p>
<pre>
<strong>Symbol</strong> <strong>Value</strong>
I 1
V 5
X 10
L 50
C 100
D 500
M 1000</pre>
<p>For example, <code>2</code> is written as <code>II</code> in Roman numeral, just two ones added together. <code>12</code> is written as <code>XII</code>, which is simply <code>X + II</code>. The number <code>27</code> is written as <code>XXVII</code>, which is <code>XX + V + II</code>.</p>
<p>Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not <code>IIII</code>. Instead, the number four is written as <code>IV</code>. Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as <code>IX</code>. There are six instances where subtraction is used:</p>
<ul>
<li><code>I</code> can be placed before <code>V</code> (5) and <code>X</code> (10) to make 4 and 9. </li>
<li><code>X</code> can be placed before <code>L</code> (50) and <code>C</code> (100) to make 40 and 90. </li>
<li><code>C</code> can be placed before <code>D</code> (500) and <code>M</code> (1000) to make 400 and 900.</li>
</ul>
<p>Given a roman numeral, convert it to an integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "III"
<strong>Output:</strong> 3
<strong>Explanation:</strong> III = 3.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "LVIII"
<strong>Output:</strong> 58
<strong>Explanation:</strong> L = 50, V= 5, III = 3.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "MCMXCIV"
<strong>Output:</strong> 1994
<strong>Explanation:</strong> M = 1000, CM = 900, XC = 90 and IV = 4.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 15</code></li>
<li><code>s</code> contains only the characters <code>('I', 'V', 'X', 'L', 'C', 'D', 'M')</code>.</li>
<li>It is <strong>guaranteed</strong> that <code>s</code> is a valid roman numeral in the range <code>[1, 3999]</code>.</li>
</ul>
| 1 | {
"code": "class Solution {\n public:\n int romanToInt(string s) {\n int ans = 0;\n vector<int> roman(128);\n\n roman['I'] = 1;\n roman['V'] = 5;\n roman['X'] = 10;\n roman['L'] = 50;\n roman['C'] = 100;\n roman['D'] = 500;\n roman['M'] = 1000;\n\n for (int i = 0; i + 1 < s.length(); ++i)\n if (roman[s[i]] < roman[s[i + 1]])\n ans -= roman[s[i]];\n else\n ans += roman[s[i]];\n\n return ans + roman[s.back()];\n }\n};",
"memory": "13600"
} |
13 | <p>Roman numerals are represented by seven different symbols: <code>I</code>, <code>V</code>, <code>X</code>, <code>L</code>, <code>C</code>, <code>D</code> and <code>M</code>.</p>
<pre>
<strong>Symbol</strong> <strong>Value</strong>
I 1
V 5
X 10
L 50
C 100
D 500
M 1000</pre>
<p>For example, <code>2</code> is written as <code>II</code> in Roman numeral, just two ones added together. <code>12</code> is written as <code>XII</code>, which is simply <code>X + II</code>. The number <code>27</code> is written as <code>XXVII</code>, which is <code>XX + V + II</code>.</p>
<p>Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not <code>IIII</code>. Instead, the number four is written as <code>IV</code>. Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as <code>IX</code>. There are six instances where subtraction is used:</p>
<ul>
<li><code>I</code> can be placed before <code>V</code> (5) and <code>X</code> (10) to make 4 and 9. </li>
<li><code>X</code> can be placed before <code>L</code> (50) and <code>C</code> (100) to make 40 and 90. </li>
<li><code>C</code> can be placed before <code>D</code> (500) and <code>M</code> (1000) to make 400 and 900.</li>
</ul>
<p>Given a roman numeral, convert it to an integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "III"
<strong>Output:</strong> 3
<strong>Explanation:</strong> III = 3.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "LVIII"
<strong>Output:</strong> 58
<strong>Explanation:</strong> L = 50, V= 5, III = 3.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "MCMXCIV"
<strong>Output:</strong> 1994
<strong>Explanation:</strong> M = 1000, CM = 900, XC = 90 and IV = 4.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 15</code></li>
<li><code>s</code> contains only the characters <code>('I', 'V', 'X', 'L', 'C', 'D', 'M')</code>.</li>
<li>It is <strong>guaranteed</strong> that <code>s</code> is a valid roman numeral in the range <code>[1, 3999]</code>.</li>
</ul>
| 1 | {
"code": "class Solution {\n public:\n int romanToInt(string s) {\n int ans = 0;\n vector<int> roman(128);\n\n roman['I'] = 1;\n roman['V'] = 5;\n roman['X'] = 10;\n roman['L'] = 50;\n roman['C'] = 100;\n roman['D'] = 500;\n roman['M'] = 1000;\n\n for (int i = 0; i + 1 < s.length(); ++i)\n if (roman[s[i]] < roman[s[i + 1]])\n ans -= roman[s[i]];\n else\n ans += roman[s[i]];\n\n return ans + roman[s.back()];\n }\n};",
"memory": "13600"
} |
13 | <p>Roman numerals are represented by seven different symbols: <code>I</code>, <code>V</code>, <code>X</code>, <code>L</code>, <code>C</code>, <code>D</code> and <code>M</code>.</p>
<pre>
<strong>Symbol</strong> <strong>Value</strong>
I 1
V 5
X 10
L 50
C 100
D 500
M 1000</pre>
<p>For example, <code>2</code> is written as <code>II</code> in Roman numeral, just two ones added together. <code>12</code> is written as <code>XII</code>, which is simply <code>X + II</code>. The number <code>27</code> is written as <code>XXVII</code>, which is <code>XX + V + II</code>.</p>
<p>Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not <code>IIII</code>. Instead, the number four is written as <code>IV</code>. Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as <code>IX</code>. There are six instances where subtraction is used:</p>
<ul>
<li><code>I</code> can be placed before <code>V</code> (5) and <code>X</code> (10) to make 4 and 9. </li>
<li><code>X</code> can be placed before <code>L</code> (50) and <code>C</code> (100) to make 40 and 90. </li>
<li><code>C</code> can be placed before <code>D</code> (500) and <code>M</code> (1000) to make 400 and 900.</li>
</ul>
<p>Given a roman numeral, convert it to an integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "III"
<strong>Output:</strong> 3
<strong>Explanation:</strong> III = 3.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "LVIII"
<strong>Output:</strong> 58
<strong>Explanation:</strong> L = 50, V= 5, III = 3.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "MCMXCIV"
<strong>Output:</strong> 1994
<strong>Explanation:</strong> M = 1000, CM = 900, XC = 90 and IV = 4.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 15</code></li>
<li><code>s</code> contains only the characters <code>('I', 'V', 'X', 'L', 'C', 'D', 'M')</code>.</li>
<li>It is <strong>guaranteed</strong> that <code>s</code> is a valid roman numeral in the range <code>[1, 3999]</code>.</li>
</ul>
| 2 | {
"code": "class Solution {\n public:\n int romanToInt(string s) {\n int ans = 0;\n vector<int> roman(128);\n\n roman['I'] = 1;\n roman['V'] = 5;\n roman['X'] = 10;\n roman['L'] = 50;\n roman['C'] = 100;\n roman['D'] = 500;\n roman['M'] = 1000;\n\n for (int i = 0; i + 1 < s.length(); ++i)\n if (roman[s[i]] < roman[s[i + 1]])\n ans -= roman[s[i]];\n else\n ans += roman[s[i]];\n\n return ans + roman[s.back()];\n }\n};\n",
"memory": "13700"
} |
13 | <p>Roman numerals are represented by seven different symbols: <code>I</code>, <code>V</code>, <code>X</code>, <code>L</code>, <code>C</code>, <code>D</code> and <code>M</code>.</p>
<pre>
<strong>Symbol</strong> <strong>Value</strong>
I 1
V 5
X 10
L 50
C 100
D 500
M 1000</pre>
<p>For example, <code>2</code> is written as <code>II</code> in Roman numeral, just two ones added together. <code>12</code> is written as <code>XII</code>, which is simply <code>X + II</code>. The number <code>27</code> is written as <code>XXVII</code>, which is <code>XX + V + II</code>.</p>
<p>Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not <code>IIII</code>. Instead, the number four is written as <code>IV</code>. Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as <code>IX</code>. There are six instances where subtraction is used:</p>
<ul>
<li><code>I</code> can be placed before <code>V</code> (5) and <code>X</code> (10) to make 4 and 9. </li>
<li><code>X</code> can be placed before <code>L</code> (50) and <code>C</code> (100) to make 40 and 90. </li>
<li><code>C</code> can be placed before <code>D</code> (500) and <code>M</code> (1000) to make 400 and 900.</li>
</ul>
<p>Given a roman numeral, convert it to an integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "III"
<strong>Output:</strong> 3
<strong>Explanation:</strong> III = 3.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "LVIII"
<strong>Output:</strong> 58
<strong>Explanation:</strong> L = 50, V= 5, III = 3.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "MCMXCIV"
<strong>Output:</strong> 1994
<strong>Explanation:</strong> M = 1000, CM = 900, XC = 90 and IV = 4.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 15</code></li>
<li><code>s</code> contains only the characters <code>('I', 'V', 'X', 'L', 'C', 'D', 'M')</code>.</li>
<li>It is <strong>guaranteed</strong> that <code>s</code> is a valid roman numeral in the range <code>[1, 3999]</code>.</li>
</ul>
| 2 | {
"code": "class Solution {\n public:\n int romanToInt(string s) {\n int ans = 0;\n vector<int> roman(128);\n\n roman['I'] = 1;\n roman['V'] = 5;\n roman['X'] = 10;\n roman['L'] = 50;\n roman['C'] = 100;\n roman['D'] = 500;\n roman['M'] = 1000;\n\n for (int i = 0; i + 1 < s.length(); ++i)\n if (roman[s[i]] < roman[s[i + 1]])\n ans -= roman[s[i]];\n else\n ans += roman[s[i]];\n\n return ans + roman[s.back()];\n }\n};",
"memory": "13700"
} |
13 | <p>Roman numerals are represented by seven different symbols: <code>I</code>, <code>V</code>, <code>X</code>, <code>L</code>, <code>C</code>, <code>D</code> and <code>M</code>.</p>
<pre>
<strong>Symbol</strong> <strong>Value</strong>
I 1
V 5
X 10
L 50
C 100
D 500
M 1000</pre>
<p>For example, <code>2</code> is written as <code>II</code> in Roman numeral, just two ones added together. <code>12</code> is written as <code>XII</code>, which is simply <code>X + II</code>. The number <code>27</code> is written as <code>XXVII</code>, which is <code>XX + V + II</code>.</p>
<p>Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not <code>IIII</code>. Instead, the number four is written as <code>IV</code>. Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as <code>IX</code>. There are six instances where subtraction is used:</p>
<ul>
<li><code>I</code> can be placed before <code>V</code> (5) and <code>X</code> (10) to make 4 and 9. </li>
<li><code>X</code> can be placed before <code>L</code> (50) and <code>C</code> (100) to make 40 and 90. </li>
<li><code>C</code> can be placed before <code>D</code> (500) and <code>M</code> (1000) to make 400 and 900.</li>
</ul>
<p>Given a roman numeral, convert it to an integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "III"
<strong>Output:</strong> 3
<strong>Explanation:</strong> III = 3.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "LVIII"
<strong>Output:</strong> 58
<strong>Explanation:</strong> L = 50, V= 5, III = 3.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "MCMXCIV"
<strong>Output:</strong> 1994
<strong>Explanation:</strong> M = 1000, CM = 900, XC = 90 and IV = 4.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 15</code></li>
<li><code>s</code> contains only the characters <code>('I', 'V', 'X', 'L', 'C', 'D', 'M')</code>.</li>
<li>It is <strong>guaranteed</strong> that <code>s</code> is a valid roman numeral in the range <code>[1, 3999]</code>.</li>
</ul>
| 2 | {
"code": "class Solution {\n public:\n int romanToInt(string s) {\n int ans = 0;\n vector<int> roman(128);\n\n roman['I'] = 1;\n roman['V'] = 5;\n roman['X'] = 10;\n roman['L'] = 50;\n roman['C'] = 100;\n roman['D'] = 500;\n roman['M'] = 1000;\n\n for (int i = 0; i + 1 < s.length(); ++i)\n if (roman[s[i]] < roman[s[i + 1]])\n ans -= roman[s[i]];\n else\n ans += roman[s[i]];\n\n return ans + roman[s.back()];\n }\n};",
"memory": "13800"
} |
13 | <p>Roman numerals are represented by seven different symbols: <code>I</code>, <code>V</code>, <code>X</code>, <code>L</code>, <code>C</code>, <code>D</code> and <code>M</code>.</p>
<pre>
<strong>Symbol</strong> <strong>Value</strong>
I 1
V 5
X 10
L 50
C 100
D 500
M 1000</pre>
<p>For example, <code>2</code> is written as <code>II</code> in Roman numeral, just two ones added together. <code>12</code> is written as <code>XII</code>, which is simply <code>X + II</code>. The number <code>27</code> is written as <code>XXVII</code>, which is <code>XX + V + II</code>.</p>
<p>Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not <code>IIII</code>. Instead, the number four is written as <code>IV</code>. Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as <code>IX</code>. There are six instances where subtraction is used:</p>
<ul>
<li><code>I</code> can be placed before <code>V</code> (5) and <code>X</code> (10) to make 4 and 9. </li>
<li><code>X</code> can be placed before <code>L</code> (50) and <code>C</code> (100) to make 40 and 90. </li>
<li><code>C</code> can be placed before <code>D</code> (500) and <code>M</code> (1000) to make 400 and 900.</li>
</ul>
<p>Given a roman numeral, convert it to an integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "III"
<strong>Output:</strong> 3
<strong>Explanation:</strong> III = 3.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "LVIII"
<strong>Output:</strong> 58
<strong>Explanation:</strong> L = 50, V= 5, III = 3.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "MCMXCIV"
<strong>Output:</strong> 1994
<strong>Explanation:</strong> M = 1000, CM = 900, XC = 90 and IV = 4.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 15</code></li>
<li><code>s</code> contains only the characters <code>('I', 'V', 'X', 'L', 'C', 'D', 'M')</code>.</li>
<li>It is <strong>guaranteed</strong> that <code>s</code> is a valid roman numeral in the range <code>[1, 3999]</code>.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int romanToInt(string s) {\n unordered_map<char, int> mp;\n mp['I'] = 1;\n mp['V'] = 5;\n mp['X'] = 10;\n mp['L'] = 50;\n mp['C'] = 100;\n mp['D'] = 500;\n mp['M'] = 1000;\n \n int sum = 0;\n for (int i = 0; i < s.length(); i++) {\n // If the next numeral is larger, subtract the current value; otherwise, add it.\n if (i + 1 < s.length() && mp[s[i]] < mp[s[i + 1]]) {\n sum -= mp[s[i]];\n } else {\n sum += mp[s[i]];\n }\n }\n return sum;\n }\n};\n",
"memory": "13800"
} |
13 | <p>Roman numerals are represented by seven different symbols: <code>I</code>, <code>V</code>, <code>X</code>, <code>L</code>, <code>C</code>, <code>D</code> and <code>M</code>.</p>
<pre>
<strong>Symbol</strong> <strong>Value</strong>
I 1
V 5
X 10
L 50
C 100
D 500
M 1000</pre>
<p>For example, <code>2</code> is written as <code>II</code> in Roman numeral, just two ones added together. <code>12</code> is written as <code>XII</code>, which is simply <code>X + II</code>. The number <code>27</code> is written as <code>XXVII</code>, which is <code>XX + V + II</code>.</p>
<p>Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not <code>IIII</code>. Instead, the number four is written as <code>IV</code>. Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as <code>IX</code>. There are six instances where subtraction is used:</p>
<ul>
<li><code>I</code> can be placed before <code>V</code> (5) and <code>X</code> (10) to make 4 and 9. </li>
<li><code>X</code> can be placed before <code>L</code> (50) and <code>C</code> (100) to make 40 and 90. </li>
<li><code>C</code> can be placed before <code>D</code> (500) and <code>M</code> (1000) to make 400 and 900.</li>
</ul>
<p>Given a roman numeral, convert it to an integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "III"
<strong>Output:</strong> 3
<strong>Explanation:</strong> III = 3.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "LVIII"
<strong>Output:</strong> 58
<strong>Explanation:</strong> L = 50, V= 5, III = 3.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "MCMXCIV"
<strong>Output:</strong> 1994
<strong>Explanation:</strong> M = 1000, CM = 900, XC = 90 and IV = 4.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 15</code></li>
<li><code>s</code> contains only the characters <code>('I', 'V', 'X', 'L', 'C', 'D', 'M')</code>.</li>
<li>It is <strong>guaranteed</strong> that <code>s</code> is a valid roman numeral in the range <code>[1, 3999]</code>.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int romanToInt(string s) {\n unordered_map<char, int> umap;\n umap['I'] = 1;\n umap['V'] = 5;\n umap['X'] = 10;\n umap['L'] = 50;\n umap['C'] = 100;\n umap['D'] = 500;\n umap['M'] = 1000;\n vector<int> v;\n\n for(int i = 0; i < s.length(); ++i) {\n v.push_back(umap[s[i]]);\n }\n\n int total = 0;\n for(int i = 0; i < v.size(); ++i) {\n if(i < v.size() - 1 && v[i] < v[i+1]) {\n total -= v[i];\n } else {\n total += v[i];\n }\n }\n return total;\n }\n};",
"memory": "13900"
} |
13 | <p>Roman numerals are represented by seven different symbols: <code>I</code>, <code>V</code>, <code>X</code>, <code>L</code>, <code>C</code>, <code>D</code> and <code>M</code>.</p>
<pre>
<strong>Symbol</strong> <strong>Value</strong>
I 1
V 5
X 10
L 50
C 100
D 500
M 1000</pre>
<p>For example, <code>2</code> is written as <code>II</code> in Roman numeral, just two ones added together. <code>12</code> is written as <code>XII</code>, which is simply <code>X + II</code>. The number <code>27</code> is written as <code>XXVII</code>, which is <code>XX + V + II</code>.</p>
<p>Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not <code>IIII</code>. Instead, the number four is written as <code>IV</code>. Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as <code>IX</code>. There are six instances where subtraction is used:</p>
<ul>
<li><code>I</code> can be placed before <code>V</code> (5) and <code>X</code> (10) to make 4 and 9. </li>
<li><code>X</code> can be placed before <code>L</code> (50) and <code>C</code> (100) to make 40 and 90. </li>
<li><code>C</code> can be placed before <code>D</code> (500) and <code>M</code> (1000) to make 400 and 900.</li>
</ul>
<p>Given a roman numeral, convert it to an integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "III"
<strong>Output:</strong> 3
<strong>Explanation:</strong> III = 3.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "LVIII"
<strong>Output:</strong> 58
<strong>Explanation:</strong> L = 50, V= 5, III = 3.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "MCMXCIV"
<strong>Output:</strong> 1994
<strong>Explanation:</strong> M = 1000, CM = 900, XC = 90 and IV = 4.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 15</code></li>
<li><code>s</code> contains only the characters <code>('I', 'V', 'X', 'L', 'C', 'D', 'M')</code>.</li>
<li>It is <strong>guaranteed</strong> that <code>s</code> is a valid roman numeral in the range <code>[1, 3999]</code>.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int romanToInt(string s) {\n std::map<char, int> map = {\n {'I', 1},\n {'V', 5},\n {'X', 10},\n {'L', 50},\n {'C', 100},\n {'D', 500},\n {'M', 1000}\n };\n\n int result = 0;\n for(unsigned int i = 0; i < s.length(); i++)\n {\n switch(s[i])\n {\n case 'I':\n if (s[i+1] == 'V')\n {\n result += 4;\n i++;\n } else if (s[i+1] == 'X')\n {\n result += 9;\n i++;\n } else\n {\n result += map[s[i]];\n };\n break;\n case 'X':\n if (s[i+1] == 'L')\n {\n result += 40;\n i++;\n } else if (s[i+1] == 'C')\n {\n result += 90;\n i++;\n } else\n {\n result += map[s[i]];\n };\n break;\n case 'C':\n if (s[i+1] == 'D')\n {\n result += 400;\n i++;\n } else if (s[i+1] == 'M')\n {\n result += 900;\n i++;\n } else\n {\n result += map[s[i]];\n };\n break;\n default:\n result += map[s[i]];\n break;\n };\n };\n return result;\n }\n};",
"memory": "13900"
} |
13 | <p>Roman numerals are represented by seven different symbols: <code>I</code>, <code>V</code>, <code>X</code>, <code>L</code>, <code>C</code>, <code>D</code> and <code>M</code>.</p>
<pre>
<strong>Symbol</strong> <strong>Value</strong>
I 1
V 5
X 10
L 50
C 100
D 500
M 1000</pre>
<p>For example, <code>2</code> is written as <code>II</code> in Roman numeral, just two ones added together. <code>12</code> is written as <code>XII</code>, which is simply <code>X + II</code>. The number <code>27</code> is written as <code>XXVII</code>, which is <code>XX + V + II</code>.</p>
<p>Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not <code>IIII</code>. Instead, the number four is written as <code>IV</code>. Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as <code>IX</code>. There are six instances where subtraction is used:</p>
<ul>
<li><code>I</code> can be placed before <code>V</code> (5) and <code>X</code> (10) to make 4 and 9. </li>
<li><code>X</code> can be placed before <code>L</code> (50) and <code>C</code> (100) to make 40 and 90. </li>
<li><code>C</code> can be placed before <code>D</code> (500) and <code>M</code> (1000) to make 400 and 900.</li>
</ul>
<p>Given a roman numeral, convert it to an integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "III"
<strong>Output:</strong> 3
<strong>Explanation:</strong> III = 3.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "LVIII"
<strong>Output:</strong> 58
<strong>Explanation:</strong> L = 50, V= 5, III = 3.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "MCMXCIV"
<strong>Output:</strong> 1994
<strong>Explanation:</strong> M = 1000, CM = 900, XC = 90 and IV = 4.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 15</code></li>
<li><code>s</code> contains only the characters <code>('I', 'V', 'X', 'L', 'C', 'D', 'M')</code>.</li>
<li>It is <strong>guaranteed</strong> that <code>s</code> is a valid roman numeral in the range <code>[1, 3999]</code>.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n\n \n\n int romanToInt(string s) {\n\n map<char, int> numeralMap;\n\n numeralMap['I'] = 1;\n numeralMap['V'] = 5;\n numeralMap['X'] = 10;\n numeralMap['L'] = 50;\n numeralMap['C'] = 100;\n numeralMap['D'] = 500;\n numeralMap['M'] = 1000;\n \n int num = 0;\n\n reverse(s.begin(), s.end());\n\n for(int i = 0; i < s.length(); i++){\n if ((s[i] == 'V' || s[i] == 'X') && s[i+1] == 'I'){\n num += numeralMap[s[i]] - 1;\n i++;\n }\n else if ((s[i] == 'L' || s[i] == 'C') && s[i+1] == 'X'){\n num += numeralMap[s[i]] - 10;\n i++;\n }\n else if ((s[i] == 'D' || s[i] == 'M') && s[i+1] == 'C'){\n num += numeralMap[s[i]] - 100;\n i++;\n }\n else{\n num += numeralMap[s[i]];\n }\n cout << num << endl;\n }\n return num;\n }\n};",
"memory": "14000"
} |
13 | <p>Roman numerals are represented by seven different symbols: <code>I</code>, <code>V</code>, <code>X</code>, <code>L</code>, <code>C</code>, <code>D</code> and <code>M</code>.</p>
<pre>
<strong>Symbol</strong> <strong>Value</strong>
I 1
V 5
X 10
L 50
C 100
D 500
M 1000</pre>
<p>For example, <code>2</code> is written as <code>II</code> in Roman numeral, just two ones added together. <code>12</code> is written as <code>XII</code>, which is simply <code>X + II</code>. The number <code>27</code> is written as <code>XXVII</code>, which is <code>XX + V + II</code>.</p>
<p>Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not <code>IIII</code>. Instead, the number four is written as <code>IV</code>. Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as <code>IX</code>. There are six instances where subtraction is used:</p>
<ul>
<li><code>I</code> can be placed before <code>V</code> (5) and <code>X</code> (10) to make 4 and 9. </li>
<li><code>X</code> can be placed before <code>L</code> (50) and <code>C</code> (100) to make 40 and 90. </li>
<li><code>C</code> can be placed before <code>D</code> (500) and <code>M</code> (1000) to make 400 and 900.</li>
</ul>
<p>Given a roman numeral, convert it to an integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "III"
<strong>Output:</strong> 3
<strong>Explanation:</strong> III = 3.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "LVIII"
<strong>Output:</strong> 58
<strong>Explanation:</strong> L = 50, V= 5, III = 3.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "MCMXCIV"
<strong>Output:</strong> 1994
<strong>Explanation:</strong> M = 1000, CM = 900, XC = 90 and IV = 4.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 15</code></li>
<li><code>s</code> contains only the characters <code>('I', 'V', 'X', 'L', 'C', 'D', 'M')</code>.</li>
<li>It is <strong>guaranteed</strong> that <code>s</code> is a valid roman numeral in the range <code>[1, 3999]</code>.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int romanToInt(string s) {\n unordered_map<char, int> mp;\n\n mp['I'] = 1;\n mp['V'] = 5;\n mp['X'] = 10;\n mp['L'] = 50;\n mp['C'] = 100;\n mp['D'] = 500;\n mp['M'] = 1000;\n\n int n = s.length();\n int res = mp[s[n - 1]]; // Initialize with the value of the last character\n\n // Iterate from the second last character to the beginning\n for (int i = n - 2; i >= 0; i--) {\n // Check if the current Roman numeral is less than the next one\n if (mp[s[i]] < mp[s[i + 1]]) {\n res -= mp[s[i]]; // Subtract if the current numeral is smaller\n } else {\n res += mp[s[i]]; // Add if the current numeral is greater or equal\n }\n }\n\n return res;\n\n return res; \n }\n};",
"memory": "14000"
} |
13 | <p>Roman numerals are represented by seven different symbols: <code>I</code>, <code>V</code>, <code>X</code>, <code>L</code>, <code>C</code>, <code>D</code> and <code>M</code>.</p>
<pre>
<strong>Symbol</strong> <strong>Value</strong>
I 1
V 5
X 10
L 50
C 100
D 500
M 1000</pre>
<p>For example, <code>2</code> is written as <code>II</code> in Roman numeral, just two ones added together. <code>12</code> is written as <code>XII</code>, which is simply <code>X + II</code>. The number <code>27</code> is written as <code>XXVII</code>, which is <code>XX + V + II</code>.</p>
<p>Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not <code>IIII</code>. Instead, the number four is written as <code>IV</code>. Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as <code>IX</code>. There are six instances where subtraction is used:</p>
<ul>
<li><code>I</code> can be placed before <code>V</code> (5) and <code>X</code> (10) to make 4 and 9. </li>
<li><code>X</code> can be placed before <code>L</code> (50) and <code>C</code> (100) to make 40 and 90. </li>
<li><code>C</code> can be placed before <code>D</code> (500) and <code>M</code> (1000) to make 400 and 900.</li>
</ul>
<p>Given a roman numeral, convert it to an integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "III"
<strong>Output:</strong> 3
<strong>Explanation:</strong> III = 3.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "LVIII"
<strong>Output:</strong> 58
<strong>Explanation:</strong> L = 50, V= 5, III = 3.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "MCMXCIV"
<strong>Output:</strong> 1994
<strong>Explanation:</strong> M = 1000, CM = 900, XC = 90 and IV = 4.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 15</code></li>
<li><code>s</code> contains only the characters <code>('I', 'V', 'X', 'L', 'C', 'D', 'M')</code>.</li>
<li>It is <strong>guaranteed</strong> that <code>s</code> is a valid roman numeral in the range <code>[1, 3999]</code>.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int romanToInt(string s) {\n unordered_map<char, int> m;\n \n m['I'] = 1;\n m['V'] = 5;\n m['X'] = 10;\n m['L'] = 50;\n m['C'] = 100;\n m['D'] = 500;\n m['M'] = 1000;\n \n int ans = 0;\n \n for(int i = 0; i < s.length(); i++){\n if(m[s[i]] < m[s[i+1]]){\n ans -= m[s[i]];\n }\n else{\n ans += m[s[i]];\n }\n }\n return ans;\n }\n};",
"memory": "14100"
} |
13 | <p>Roman numerals are represented by seven different symbols: <code>I</code>, <code>V</code>, <code>X</code>, <code>L</code>, <code>C</code>, <code>D</code> and <code>M</code>.</p>
<pre>
<strong>Symbol</strong> <strong>Value</strong>
I 1
V 5
X 10
L 50
C 100
D 500
M 1000</pre>
<p>For example, <code>2</code> is written as <code>II</code> in Roman numeral, just two ones added together. <code>12</code> is written as <code>XII</code>, which is simply <code>X + II</code>. The number <code>27</code> is written as <code>XXVII</code>, which is <code>XX + V + II</code>.</p>
<p>Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not <code>IIII</code>. Instead, the number four is written as <code>IV</code>. Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as <code>IX</code>. There are six instances where subtraction is used:</p>
<ul>
<li><code>I</code> can be placed before <code>V</code> (5) and <code>X</code> (10) to make 4 and 9. </li>
<li><code>X</code> can be placed before <code>L</code> (50) and <code>C</code> (100) to make 40 and 90. </li>
<li><code>C</code> can be placed before <code>D</code> (500) and <code>M</code> (1000) to make 400 and 900.</li>
</ul>
<p>Given a roman numeral, convert it to an integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "III"
<strong>Output:</strong> 3
<strong>Explanation:</strong> III = 3.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "LVIII"
<strong>Output:</strong> 58
<strong>Explanation:</strong> L = 50, V= 5, III = 3.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "MCMXCIV"
<strong>Output:</strong> 1994
<strong>Explanation:</strong> M = 1000, CM = 900, XC = 90 and IV = 4.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 15</code></li>
<li><code>s</code> contains only the characters <code>('I', 'V', 'X', 'L', 'C', 'D', 'M')</code>.</li>
<li>It is <strong>guaranteed</strong> that <code>s</code> is a valid roman numeral in the range <code>[1, 3999]</code>.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int romanToInt(string s) {\n unordered_map<char, int> m;\n \n m['I'] = 1;\n m['V'] = 5;\n m['X'] = 10;\n m['L'] = 50;\n m['C'] = 100;\n m['D'] = 500;\n m['M'] = 1000;\n \n int ans = 0;\n \n for(int i = 0; i < s.length(); i++){\n if(m[s[i]] < m[s[i+1]]){\n ans -= m[s[i]];\n }\n else{\n ans += m[s[i]];\n }\n }\n return ans;\n }\n};",
"memory": "14100"
} |
13 | <p>Roman numerals are represented by seven different symbols: <code>I</code>, <code>V</code>, <code>X</code>, <code>L</code>, <code>C</code>, <code>D</code> and <code>M</code>.</p>
<pre>
<strong>Symbol</strong> <strong>Value</strong>
I 1
V 5
X 10
L 50
C 100
D 500
M 1000</pre>
<p>For example, <code>2</code> is written as <code>II</code> in Roman numeral, just two ones added together. <code>12</code> is written as <code>XII</code>, which is simply <code>X + II</code>. The number <code>27</code> is written as <code>XXVII</code>, which is <code>XX + V + II</code>.</p>
<p>Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not <code>IIII</code>. Instead, the number four is written as <code>IV</code>. Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as <code>IX</code>. There are six instances where subtraction is used:</p>
<ul>
<li><code>I</code> can be placed before <code>V</code> (5) and <code>X</code> (10) to make 4 and 9. </li>
<li><code>X</code> can be placed before <code>L</code> (50) and <code>C</code> (100) to make 40 and 90. </li>
<li><code>C</code> can be placed before <code>D</code> (500) and <code>M</code> (1000) to make 400 and 900.</li>
</ul>
<p>Given a roman numeral, convert it to an integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "III"
<strong>Output:</strong> 3
<strong>Explanation:</strong> III = 3.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "LVIII"
<strong>Output:</strong> 58
<strong>Explanation:</strong> L = 50, V= 5, III = 3.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "MCMXCIV"
<strong>Output:</strong> 1994
<strong>Explanation:</strong> M = 1000, CM = 900, XC = 90 and IV = 4.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 15</code></li>
<li><code>s</code> contains only the characters <code>('I', 'V', 'X', 'L', 'C', 'D', 'M')</code>.</li>
<li>It is <strong>guaranteed</strong> that <code>s</code> is a valid roman numeral in the range <code>[1, 3999]</code>.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int romanToInt(string s) {\n unordered_map<char, int> m;\n \n m['I'] = 1;\n m['V'] = 5;\n m['X'] = 10;\n m['L'] = 50;\n m['C'] = 100;\n m['D'] = 500;\n m['M'] = 1000;\n \n int ans = 0;\n \n for(int i = 0; i < s.length(); i++){\n if(m[s[i]] < m[s[i+1]]){\n ans -= m[s[i]];\n }\n else{\n ans += m[s[i]];\n }\n }\n return ans;\n }\n};",
"memory": "14200"
} |
13 | <p>Roman numerals are represented by seven different symbols: <code>I</code>, <code>V</code>, <code>X</code>, <code>L</code>, <code>C</code>, <code>D</code> and <code>M</code>.</p>
<pre>
<strong>Symbol</strong> <strong>Value</strong>
I 1
V 5
X 10
L 50
C 100
D 500
M 1000</pre>
<p>For example, <code>2</code> is written as <code>II</code> in Roman numeral, just two ones added together. <code>12</code> is written as <code>XII</code>, which is simply <code>X + II</code>. The number <code>27</code> is written as <code>XXVII</code>, which is <code>XX + V + II</code>.</p>
<p>Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not <code>IIII</code>. Instead, the number four is written as <code>IV</code>. Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as <code>IX</code>. There are six instances where subtraction is used:</p>
<ul>
<li><code>I</code> can be placed before <code>V</code> (5) and <code>X</code> (10) to make 4 and 9. </li>
<li><code>X</code> can be placed before <code>L</code> (50) and <code>C</code> (100) to make 40 and 90. </li>
<li><code>C</code> can be placed before <code>D</code> (500) and <code>M</code> (1000) to make 400 and 900.</li>
</ul>
<p>Given a roman numeral, convert it to an integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "III"
<strong>Output:</strong> 3
<strong>Explanation:</strong> III = 3.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "LVIII"
<strong>Output:</strong> 58
<strong>Explanation:</strong> L = 50, V= 5, III = 3.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "MCMXCIV"
<strong>Output:</strong> 1994
<strong>Explanation:</strong> M = 1000, CM = 900, XC = 90 and IV = 4.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 15</code></li>
<li><code>s</code> contains only the characters <code>('I', 'V', 'X', 'L', 'C', 'D', 'M')</code>.</li>
<li>It is <strong>guaranteed</strong> that <code>s</code> is a valid roman numeral in the range <code>[1, 3999]</code>.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n int romanToInt(string c) {\n unordered_map<char, int> m;\n \n m['I'] = 1;\n m['V'] = 5;\n m['X'] = 10;\n m['L'] = 50;\n m['C'] = 100;\n m['D'] = 500;\n m['M'] = 1000;\n int ans=0;\n\n for(int i=0;i<c.length();i++){\n \n if(m[c[i]]<m[c[i+1]]){\n \n ans-=m[c[i]];\n\n }else{\n ans+=m[c[i]];\n }\n \n }\n\n\n\nreturn ans;\n\n }\n};",
"memory": "14200"
} |
13 | <p>Roman numerals are represented by seven different symbols: <code>I</code>, <code>V</code>, <code>X</code>, <code>L</code>, <code>C</code>, <code>D</code> and <code>M</code>.</p>
<pre>
<strong>Symbol</strong> <strong>Value</strong>
I 1
V 5
X 10
L 50
C 100
D 500
M 1000</pre>
<p>For example, <code>2</code> is written as <code>II</code> in Roman numeral, just two ones added together. <code>12</code> is written as <code>XII</code>, which is simply <code>X + II</code>. The number <code>27</code> is written as <code>XXVII</code>, which is <code>XX + V + II</code>.</p>
<p>Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not <code>IIII</code>. Instead, the number four is written as <code>IV</code>. Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as <code>IX</code>. There are six instances where subtraction is used:</p>
<ul>
<li><code>I</code> can be placed before <code>V</code> (5) and <code>X</code> (10) to make 4 and 9. </li>
<li><code>X</code> can be placed before <code>L</code> (50) and <code>C</code> (100) to make 40 and 90. </li>
<li><code>C</code> can be placed before <code>D</code> (500) and <code>M</code> (1000) to make 400 and 900.</li>
</ul>
<p>Given a roman numeral, convert it to an integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "III"
<strong>Output:</strong> 3
<strong>Explanation:</strong> III = 3.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "LVIII"
<strong>Output:</strong> 58
<strong>Explanation:</strong> L = 50, V= 5, III = 3.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "MCMXCIV"
<strong>Output:</strong> 1994
<strong>Explanation:</strong> M = 1000, CM = 900, XC = 90 and IV = 4.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 15</code></li>
<li><code>s</code> contains only the characters <code>('I', 'V', 'X', 'L', 'C', 'D', 'M')</code>.</li>
<li>It is <strong>guaranteed</strong> that <code>s</code> is a valid roman numeral in the range <code>[1, 3999]</code>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int romanToInt(string s) {\n unordered_map<char,int>roman;\n roman['I']=1;\n roman['V']=5;\n roman['X']=10;\n roman['L']=50;\n roman['C']=100;\n roman['D']=500;\n roman['M']=1000;\n\n int ans=0;\n for(int i=0;i<s.size();i++){\n if(roman[s[i]]<roman[s[i+1]]){\n ans=ans-roman[s[i]];\n }\n else{\n ans=ans+roman[s[i]];\n }\n }\n return ans;\n }\n};",
"memory": "14300"
} |
13 | <p>Roman numerals are represented by seven different symbols: <code>I</code>, <code>V</code>, <code>X</code>, <code>L</code>, <code>C</code>, <code>D</code> and <code>M</code>.</p>
<pre>
<strong>Symbol</strong> <strong>Value</strong>
I 1
V 5
X 10
L 50
C 100
D 500
M 1000</pre>
<p>For example, <code>2</code> is written as <code>II</code> in Roman numeral, just two ones added together. <code>12</code> is written as <code>XII</code>, which is simply <code>X + II</code>. The number <code>27</code> is written as <code>XXVII</code>, which is <code>XX + V + II</code>.</p>
<p>Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not <code>IIII</code>. Instead, the number four is written as <code>IV</code>. Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as <code>IX</code>. There are six instances where subtraction is used:</p>
<ul>
<li><code>I</code> can be placed before <code>V</code> (5) and <code>X</code> (10) to make 4 and 9. </li>
<li><code>X</code> can be placed before <code>L</code> (50) and <code>C</code> (100) to make 40 and 90. </li>
<li><code>C</code> can be placed before <code>D</code> (500) and <code>M</code> (1000) to make 400 and 900.</li>
</ul>
<p>Given a roman numeral, convert it to an integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "III"
<strong>Output:</strong> 3
<strong>Explanation:</strong> III = 3.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "LVIII"
<strong>Output:</strong> 58
<strong>Explanation:</strong> L = 50, V= 5, III = 3.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "MCMXCIV"
<strong>Output:</strong> 1994
<strong>Explanation:</strong> M = 1000, CM = 900, XC = 90 and IV = 4.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 15</code></li>
<li><code>s</code> contains only the characters <code>('I', 'V', 'X', 'L', 'C', 'D', 'M')</code>.</li>
<li>It is <strong>guaranteed</strong> that <code>s</code> is a valid roman numeral in the range <code>[1, 3999]</code>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int romanToInt(string s) {\n unordered_map<char, int> m;\n \n m['I'] = 1;\n m['V'] = 5;\n m['X'] = 10;\n m['L'] = 50;\n m['C'] = 100;\n m['D'] = 500;\n m['M'] = 1000;\n \n int ans = 0;\n \n for(int i = 0; i < s.length(); i++){\n if(m[s[i]] < m[s[i+1]]){\n ans -= m[s[i]];\n }\n else{\n ans += m[s[i]];\n }\n }\n return ans;\n }\n};",
"memory": "14300"
} |
13 | <p>Roman numerals are represented by seven different symbols: <code>I</code>, <code>V</code>, <code>X</code>, <code>L</code>, <code>C</code>, <code>D</code> and <code>M</code>.</p>
<pre>
<strong>Symbol</strong> <strong>Value</strong>
I 1
V 5
X 10
L 50
C 100
D 500
M 1000</pre>
<p>For example, <code>2</code> is written as <code>II</code> in Roman numeral, just two ones added together. <code>12</code> is written as <code>XII</code>, which is simply <code>X + II</code>. The number <code>27</code> is written as <code>XXVII</code>, which is <code>XX + V + II</code>.</p>
<p>Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not <code>IIII</code>. Instead, the number four is written as <code>IV</code>. Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as <code>IX</code>. There are six instances where subtraction is used:</p>
<ul>
<li><code>I</code> can be placed before <code>V</code> (5) and <code>X</code> (10) to make 4 and 9. </li>
<li><code>X</code> can be placed before <code>L</code> (50) and <code>C</code> (100) to make 40 and 90. </li>
<li><code>C</code> can be placed before <code>D</code> (500) and <code>M</code> (1000) to make 400 and 900.</li>
</ul>
<p>Given a roman numeral, convert it to an integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "III"
<strong>Output:</strong> 3
<strong>Explanation:</strong> III = 3.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "LVIII"
<strong>Output:</strong> 58
<strong>Explanation:</strong> L = 50, V= 5, III = 3.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "MCMXCIV"
<strong>Output:</strong> 1994
<strong>Explanation:</strong> M = 1000, CM = 900, XC = 90 and IV = 4.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 15</code></li>
<li><code>s</code> contains only the characters <code>('I', 'V', 'X', 'L', 'C', 'D', 'M')</code>.</li>
<li>It is <strong>guaranteed</strong> that <code>s</code> is a valid roman numeral in the range <code>[1, 3999]</code>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n\n \n\n int romanToInt(string s) {\n\n map<char, int> numeralMap; // create map for conversion\n\n numeralMap['I'] = 1;\n numeralMap['V'] = 5;\n numeralMap['X'] = 10;\n numeralMap['L'] = 50;\n numeralMap['C'] = 100;\n numeralMap['D'] = 500;\n numeralMap['M'] = 1000;\n \n int num = 0;\n\n // reverse(s.begin(), s.end()); // reverse string\n\n // for(int i = 0; i < s.length(); i++){ // iterate through length of string\n // if ((s[i] == 'V' || s[i] == 'X') && s[i+1] == 'I'){ // check for subtraction cases\n // num += numeralMap[s[i]] - 1;\n // i++;\n // }\n // else if ((s[i] == 'L' || s[i] == 'C') && s[i+1] == 'X'){\n // num += numeralMap[s[i]] - 10;\n // i++;\n // }\n // else if ((s[i] == 'D' || s[i] == 'M') && s[i+1] == 'C'){\n // num += numeralMap[s[i]] - 100;\n // i++;\n // }\n // else{\n // num += numeralMap[s[i]];\n // }\n // cout << num << endl;\n // }\n // return num;\n\n for (int i = 0; i < s.length(); i++){\n if (numeralMap[s[i]] < numeralMap[s[i+1]]){\n num -= numeralMap[s[i]];\n }\n else{\n num += numeralMap[s[i]];\n }\n }\n return num;\n }\n};",
"memory": "14400"
} |
13 | <p>Roman numerals are represented by seven different symbols: <code>I</code>, <code>V</code>, <code>X</code>, <code>L</code>, <code>C</code>, <code>D</code> and <code>M</code>.</p>
<pre>
<strong>Symbol</strong> <strong>Value</strong>
I 1
V 5
X 10
L 50
C 100
D 500
M 1000</pre>
<p>For example, <code>2</code> is written as <code>II</code> in Roman numeral, just two ones added together. <code>12</code> is written as <code>XII</code>, which is simply <code>X + II</code>. The number <code>27</code> is written as <code>XXVII</code>, which is <code>XX + V + II</code>.</p>
<p>Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not <code>IIII</code>. Instead, the number four is written as <code>IV</code>. Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as <code>IX</code>. There are six instances where subtraction is used:</p>
<ul>
<li><code>I</code> can be placed before <code>V</code> (5) and <code>X</code> (10) to make 4 and 9. </li>
<li><code>X</code> can be placed before <code>L</code> (50) and <code>C</code> (100) to make 40 and 90. </li>
<li><code>C</code> can be placed before <code>D</code> (500) and <code>M</code> (1000) to make 400 and 900.</li>
</ul>
<p>Given a roman numeral, convert it to an integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "III"
<strong>Output:</strong> 3
<strong>Explanation:</strong> III = 3.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "LVIII"
<strong>Output:</strong> 58
<strong>Explanation:</strong> L = 50, V= 5, III = 3.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "MCMXCIV"
<strong>Output:</strong> 1994
<strong>Explanation:</strong> M = 1000, CM = 900, XC = 90 and IV = 4.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 15</code></li>
<li><code>s</code> contains only the characters <code>('I', 'V', 'X', 'L', 'C', 'D', 'M')</code>.</li>
<li>It is <strong>guaranteed</strong> that <code>s</code> is a valid roman numeral in the range <code>[1, 3999]</code>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int romanToInt(string s) {\n map<char,int>mp;\n mp['I']=1;\n mp['V']=5;\n mp['X']=10;\n mp['L']=50;\n mp['C']=100;\n mp['D']=500;\n mp['M']=1000;\n int res=0;\n for(int i=0;i<s.length();i++)\n {\n if(mp[s[i]]<mp[s[i+1]])\n res-=mp[s[i]];\n else\n res+=mp[s[i]];\n }\n return res;\n \n }\n};",
"memory": "14400"
} |
13 | <p>Roman numerals are represented by seven different symbols: <code>I</code>, <code>V</code>, <code>X</code>, <code>L</code>, <code>C</code>, <code>D</code> and <code>M</code>.</p>
<pre>
<strong>Symbol</strong> <strong>Value</strong>
I 1
V 5
X 10
L 50
C 100
D 500
M 1000</pre>
<p>For example, <code>2</code> is written as <code>II</code> in Roman numeral, just two ones added together. <code>12</code> is written as <code>XII</code>, which is simply <code>X + II</code>. The number <code>27</code> is written as <code>XXVII</code>, which is <code>XX + V + II</code>.</p>
<p>Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not <code>IIII</code>. Instead, the number four is written as <code>IV</code>. Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as <code>IX</code>. There are six instances where subtraction is used:</p>
<ul>
<li><code>I</code> can be placed before <code>V</code> (5) and <code>X</code> (10) to make 4 and 9. </li>
<li><code>X</code> can be placed before <code>L</code> (50) and <code>C</code> (100) to make 40 and 90. </li>
<li><code>C</code> can be placed before <code>D</code> (500) and <code>M</code> (1000) to make 400 and 900.</li>
</ul>
<p>Given a roman numeral, convert it to an integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "III"
<strong>Output:</strong> 3
<strong>Explanation:</strong> III = 3.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "LVIII"
<strong>Output:</strong> 58
<strong>Explanation:</strong> L = 50, V= 5, III = 3.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "MCMXCIV"
<strong>Output:</strong> 1994
<strong>Explanation:</strong> M = 1000, CM = 900, XC = 90 and IV = 4.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 15</code></li>
<li><code>s</code> contains only the characters <code>('I', 'V', 'X', 'L', 'C', 'D', 'M')</code>.</li>
<li>It is <strong>guaranteed</strong> that <code>s</code> is a valid roman numeral in the range <code>[1, 3999]</code>.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n int romanToInt(string str) {\n map<char, int> m;\n m.insert({ 'I', 1 });\n m.insert({ 'V', 5 });\n m.insert({ 'X', 10 });\n m.insert({ 'L', 50 });\n m.insert({ 'C', 100 });\n m.insert({ 'D', 500 });\n m.insert({ 'M', 1000 });\n int sum = 0;\n for (int i = 0; i < str.length(); i++) \n {\n if (m[str[i]] < m[str[i + 1]])\n {\n sum+=m[str[i+1]]-m[str[i]];\n i++;\n continue;\n }\n sum += m[str[i]];\n }\n return sum;\n }\n};",
"memory": "14500"
} |
13 | <p>Roman numerals are represented by seven different symbols: <code>I</code>, <code>V</code>, <code>X</code>, <code>L</code>, <code>C</code>, <code>D</code> and <code>M</code>.</p>
<pre>
<strong>Symbol</strong> <strong>Value</strong>
I 1
V 5
X 10
L 50
C 100
D 500
M 1000</pre>
<p>For example, <code>2</code> is written as <code>II</code> in Roman numeral, just two ones added together. <code>12</code> is written as <code>XII</code>, which is simply <code>X + II</code>. The number <code>27</code> is written as <code>XXVII</code>, which is <code>XX + V + II</code>.</p>
<p>Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not <code>IIII</code>. Instead, the number four is written as <code>IV</code>. Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as <code>IX</code>. There are six instances where subtraction is used:</p>
<ul>
<li><code>I</code> can be placed before <code>V</code> (5) and <code>X</code> (10) to make 4 and 9. </li>
<li><code>X</code> can be placed before <code>L</code> (50) and <code>C</code> (100) to make 40 and 90. </li>
<li><code>C</code> can be placed before <code>D</code> (500) and <code>M</code> (1000) to make 400 and 900.</li>
</ul>
<p>Given a roman numeral, convert it to an integer.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> s = "III"
<strong>Output:</strong> 3
<strong>Explanation:</strong> III = 3.
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> s = "LVIII"
<strong>Output:</strong> 58
<strong>Explanation:</strong> L = 50, V= 5, III = 3.
</pre>
<p><strong class="example">Example 3:</strong></p>
<pre>
<strong>Input:</strong> s = "MCMXCIV"
<strong>Output:</strong> 1994
<strong>Explanation:</strong> M = 1000, CM = 900, XC = 90 and IV = 4.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= s.length <= 15</code></li>
<li><code>s</code> contains only the characters <code>('I', 'V', 'X', 'L', 'C', 'D', 'M')</code>.</li>
<li>It is <strong>guaranteed</strong> that <code>s</code> is a valid roman numeral in the range <code>[1, 3999]</code>.</li>
</ul>
| 3 | {
"code": "#include <iostream>\n#include <string>\n#include <vector>\n\nusing namespace std;\n\nclass Solution {\npublic:\n int romanToInt(const string& s) {\n // Define the mapping from Roman numerals to integers\n vector<string> roman = {\"M\", \"CM\", \"D\", \"CD\", \"C\", \"XC\", \"L\", \"XL\", \"X\", \"IX\", \"V\", \"IV\", \"I\"};\n vector<int> values = {1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1};\n\n int result = 0;\n int i = 0;\n int n = s.length();\n\n while (i < n) {\n // Check for the largest Roman numeral substring at the current position\n for (int j = 0; j < roman.size(); ++j) {\n if (s.substr(i, roman[j].size()) == roman[j]) {\n result += values[j];\n i += roman[j].size();\n break; // Exit the loop once a match is found\n }\n }\n }\n\n return result;\n }\n};\n\n\n",
"memory": "14500"
} |
14 | <p>Write a function to find the longest common prefix string amongst an array of strings.</p>
<p>If there is no common prefix, return an empty string <code>""</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> strs = ["flower","flow","flight"]
<strong>Output:</strong> "fl"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> strs = ["dog","racecar","car"]
<strong>Output:</strong> ""
<strong>Explanation:</strong> There is no common prefix among the input strings.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= strs.length <= 200</code></li>
<li><code>0 <= strs[i].length <= 200</code></li>
<li><code>strs[i]</code> consists of only lowercase English letters.</li>
</ul>
| 0 | {
"code": "class Solution {\n public:\n string longestCommonPrefix(vector<string>& strs) {\n if (strs.empty())\n return \"\";\n\n for (int i = 0; i < strs[0].length(); ++i)\n for (int j = 1; j < strs.size(); ++j)\n if (i == strs[j].length() || strs[j][i] != strs[0][i])\n return strs[0].substr(0, i);\n\n return strs[0];\n }\n};\n\n\n \n\n",
"memory": "10700"
} |
14 | <p>Write a function to find the longest common prefix string amongst an array of strings.</p>
<p>If there is no common prefix, return an empty string <code>""</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> strs = ["flower","flow","flight"]
<strong>Output:</strong> "fl"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> strs = ["dog","racecar","car"]
<strong>Output:</strong> ""
<strong>Explanation:</strong> There is no common prefix among the input strings.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= strs.length <= 200</code></li>
<li><code>0 <= strs[i].length <= 200</code></li>
<li><code>strs[i]</code> consists of only lowercase English letters.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n string longestCommonPrefix(vector<string>& strs) {\n int sizeVector = strs.size();\n string subString = \"\";\n int mid = strs[0].length();\n \n for(int j = 0; j < mid; ++j){\n int mark = 0;\n for(int i = 1; i < sizeVector; ++i){\n if(strs[i][j] != strs[0][j]) {\n mark = 1;\n break;\n }\n }\n if(mark) break;\n else subString += strs[0][j];\n }\n return subString;\n \n }\n};",
"memory": "10800"
} |
14 | <p>Write a function to find the longest common prefix string amongst an array of strings.</p>
<p>If there is no common prefix, return an empty string <code>""</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> strs = ["flower","flow","flight"]
<strong>Output:</strong> "fl"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> strs = ["dog","racecar","car"]
<strong>Output:</strong> ""
<strong>Explanation:</strong> There is no common prefix among the input strings.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= strs.length <= 200</code></li>
<li><code>0 <= strs[i].length <= 200</code></li>
<li><code>strs[i]</code> consists of only lowercase English letters.</li>
</ul>
| 0 | {
"code": "class Solution\n{\npublic:\n string longestCommonPrefix(vector<string>& vct)\n {\n if (vct.size() == 1) { return vct[0]; }\n \n int size = vct[0].size(); string answer = \"\"; int count = 0; int index = 0;\n\n for (int i = 0; i < vct.size(); i++)\n {\n if (vct[i].size() < size) { size = vct[i].size(); }\n }\n \n while (index != size)\n {\n for (int j = 1; j < vct.size(); j++)\n {\n if (vct[0][index] == vct[j][index]) \n { \n count++;\n \n if (count == vct.size() - 1) \n { \n answer += vct[0][index]; \n } \n }\n\n if (j == vct.size() - 1) { index++; }\n }\n\n if (count != vct.size() - 1) { return answer; } count = 0;\n }\n\n return answer;\n \n }\n};",
"memory": "10800"
} |
14 | <p>Write a function to find the longest common prefix string amongst an array of strings.</p>
<p>If there is no common prefix, return an empty string <code>""</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> strs = ["flower","flow","flight"]
<strong>Output:</strong> "fl"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> strs = ["dog","racecar","car"]
<strong>Output:</strong> ""
<strong>Explanation:</strong> There is no common prefix among the input strings.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= strs.length <= 200</code></li>
<li><code>0 <= strs[i].length <= 200</code></li>
<li><code>strs[i]</code> consists of only lowercase English letters.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n string longestCommonPrefix(vector<string>& strs) {\n int min = INT_MAX;\n int that = 0;\n for (int i = 0;i<strs.size();i++){\n if (strs[i].size()<min){\n min = strs[i].size();\n that = i;\n }\n }\n string result = \"\";\n for (int i = 0;i<min;i++){\n char temp = strs[that][i];\n for (int j = 0;j<strs.size();j++){\n if (strs[j][i]!=temp){\n return result;\n }\n }\n result = result + temp;\n }\n \n return result;\n }\n};",
"memory": "10900"
} |
14 | <p>Write a function to find the longest common prefix string amongst an array of strings.</p>
<p>If there is no common prefix, return an empty string <code>""</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> strs = ["flower","flow","flight"]
<strong>Output:</strong> "fl"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> strs = ["dog","racecar","car"]
<strong>Output:</strong> ""
<strong>Explanation:</strong> There is no common prefix among the input strings.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= strs.length <= 200</code></li>
<li><code>0 <= strs[i].length <= 200</code></li>
<li><code>strs[i]</code> consists of only lowercase English letters.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n string longestCommonPrefix(vector<string>& strs) {\n int min = INT_MAX;\n int that = 0;\n for (int i = 0;i<strs.size();i++){\n if (strs[i].size()<min){\n min = strs[i].size();\n that = i;\n }\n }\n string result = \"\";\n for (int i = 0;i<min;i++){\n char temp = strs[that][i];\n for (int j = 0;j<strs.size();j++){\n if (strs[j][i]!=temp){\n return result;\n }\n }\n result = result + temp;\n }\n \n return result;\n }\n};",
"memory": "10900"
} |
14 | <p>Write a function to find the longest common prefix string amongst an array of strings.</p>
<p>If there is no common prefix, return an empty string <code>""</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> strs = ["flower","flow","flight"]
<strong>Output:</strong> "fl"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> strs = ["dog","racecar","car"]
<strong>Output:</strong> ""
<strong>Explanation:</strong> There is no common prefix among the input strings.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= strs.length <= 200</code></li>
<li><code>0 <= strs[i].length <= 200</code></li>
<li><code>strs[i]</code> consists of only lowercase English letters.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n string longestCommonPrefix(vector<string>& strs) {\n string result;\n bool stop = false;\n int prefix_size = 0;\n if(strs.empty())\n return result;\n while(stop == false && prefix_size<strs.at(0).size())\n {\n if(strs.at(0).size() == 0)\n {\n stop = true;\n break;\n }\n char temp = strs.at(0).at(prefix_size);\n for(int i = 1;i<strs.size();i++)\n {\n if(strs.at(i).size() <= prefix_size || strs.at(i).at(prefix_size) != temp)\n {\n stop = true;\n break;\n }\n }\n if(stop == false)\n result += temp;\n prefix_size++;\n }\n return result;\n }\n};",
"memory": "11000"
} |
14 | <p>Write a function to find the longest common prefix string amongst an array of strings.</p>
<p>If there is no common prefix, return an empty string <code>""</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> strs = ["flower","flow","flight"]
<strong>Output:</strong> "fl"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> strs = ["dog","racecar","car"]
<strong>Output:</strong> ""
<strong>Explanation:</strong> There is no common prefix among the input strings.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= strs.length <= 200</code></li>
<li><code>0 <= strs[i].length <= 200</code></li>
<li><code>strs[i]</code> consists of only lowercase English letters.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n string longestCommonPrefix(vector<string>& strs) {\n string result;\n if(strs.empty())\n return result;\n int loop_time = INT_MAX;\n int strs_size = strs.size();\n for(int i = 0;i < strs_size; i++)\n {\n if(strs.at(i).size() < loop_time)\n loop_time = strs.at(i).size();\n }\n\n int prefix_size = 0;\n while(prefix_size < loop_time)\n {\n char temp = strs.at(0).at(prefix_size);\n for(int i = 1;i<strs.size();i++)\n {\n if(strs.at(i).at(prefix_size) != temp)\n return result;\n }\n prefix_size++;\n result += temp;\n }\n return result;\n }\n};",
"memory": "11000"
} |
14 | <p>Write a function to find the longest common prefix string amongst an array of strings.</p>
<p>If there is no common prefix, return an empty string <code>""</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> strs = ["flower","flow","flight"]
<strong>Output:</strong> "fl"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> strs = ["dog","racecar","car"]
<strong>Output:</strong> ""
<strong>Explanation:</strong> There is no common prefix among the input strings.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= strs.length <= 200</code></li>
<li><code>0 <= strs[i].length <= 200</code></li>
<li><code>strs[i]</code> consists of only lowercase English letters.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n string longestCommonPrefix(vector<string>& strs) {\n sort(strs.begin(),strs.end());\n int n = strs.size();\n string a= strs[0];\n string b = strs[n-1];\n string c=\"\";\n for (int i = 0; i< min(a.size(), b.size()); i++){\n if(a[i]==b[i]){\n c += a[i];\n }else {\n return c;\n }\n }\n return c;\n }\n};\n\n",
"memory": "11100"
} |
14 | <p>Write a function to find the longest common prefix string amongst an array of strings.</p>
<p>If there is no common prefix, return an empty string <code>""</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> strs = ["flower","flow","flight"]
<strong>Output:</strong> "fl"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> strs = ["dog","racecar","car"]
<strong>Output:</strong> ""
<strong>Explanation:</strong> There is no common prefix among the input strings.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= strs.length <= 200</code></li>
<li><code>0 <= strs[i].length <= 200</code></li>
<li><code>strs[i]</code> consists of only lowercase English letters.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n string longestCommonPrefix(vector<string>& strs) {\n string baseStr = strs[0];\n int ans = 0 ;\n \n for(int j = 0 ; j < baseStr.size() ; j++){\n bool matched = true ; \n for(int i =1 ; i < strs.size() ; i++){\n if (baseStr[j] != strs[i][j]){\n matched &= false ;\n break ;\n } else {\n matched &= true ;\n }\n }\n\n if (matched){\n ans = j+1 ;\n continue;\n } else {\n break ;\n }\n }\n return baseStr.substr(0, ans) ;\n \n }\n};",
"memory": "11100"
} |
14 | <p>Write a function to find the longest common prefix string amongst an array of strings.</p>
<p>If there is no common prefix, return an empty string <code>""</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> strs = ["flower","flow","flight"]
<strong>Output:</strong> "fl"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> strs = ["dog","racecar","car"]
<strong>Output:</strong> ""
<strong>Explanation:</strong> There is no common prefix among the input strings.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= strs.length <= 200</code></li>
<li><code>0 <= strs[i].length <= 200</code></li>
<li><code>strs[i]</code> consists of only lowercase English letters.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n string longestCommonPrefix(vector<string>& strs) {\n string s=\"\";\n lon(strs,0,s);\n return s;\n }\n void lon(vector<string>&strs,int start ,string& s){\n for(int i=0;i<strs.size();i++){\n if(start>=strs[i].size()||strs[0][start]!=strs[i][start])return;\n }\n s = s+strs[0][start];\n lon(strs,start+1,s);\n }\n};",
"memory": "11200"
} |
14 | <p>Write a function to find the longest common prefix string amongst an array of strings.</p>
<p>If there is no common prefix, return an empty string <code>""</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> strs = ["flower","flow","flight"]
<strong>Output:</strong> "fl"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> strs = ["dog","racecar","car"]
<strong>Output:</strong> ""
<strong>Explanation:</strong> There is no common prefix among the input strings.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= strs.length <= 200</code></li>
<li><code>0 <= strs[i].length <= 200</code></li>
<li><code>strs[i]</code> consists of only lowercase English letters.</li>
</ul>
| 0 | {
"code": "class Solution {\npublic:\n string longestCommonPrefix(vector<string>& strs) {\n if (strs.size() == 0) return \"\"; \n string prefix = strs[0];\n\n for (int i = 1; i < strs.size(); i++) {\n while (strs[i].find(prefix) != 0) {\n prefix = prefix.substr(0, prefix.length() - 1);\n if (prefix.length() == 0) return \"\";\n }\n }\n\n return prefix;\n }\n};",
"memory": "11200"
} |
14 | <p>Write a function to find the longest common prefix string amongst an array of strings.</p>
<p>If there is no common prefix, return an empty string <code>""</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> strs = ["flower","flow","flight"]
<strong>Output:</strong> "fl"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> strs = ["dog","racecar","car"]
<strong>Output:</strong> ""
<strong>Explanation:</strong> There is no common prefix among the input strings.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= strs.length <= 200</code></li>
<li><code>0 <= strs[i].length <= 200</code></li>
<li><code>strs[i]</code> consists of only lowercase English letters.</li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n string longestCommonPrefix(vector<string>& strs) {\n \n \n int mini=INT_MAX;\n string small=\"\";\n for (auto i:strs){\n if (i.length()<mini){\n mini=i.length();\n small=i;\n }\n }\n string ans=\"\";\n for (int i=0;i<mini;i++){\n char c=small[i];\n int count=0;\n for (int j=0;j<strs.size();j++){\n if (c==strs[j][i]){\n count++;\n }\n }\n\n if (count==strs.size()){\n ans.push_back(c);\n }\n else {\n break;\n }\n }\n\n return ans;\n \n \n\n \n }\n};",
"memory": "11300"
} |
14 | <p>Write a function to find the longest common prefix string amongst an array of strings.</p>
<p>If there is no common prefix, return an empty string <code>""</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> strs = ["flower","flow","flight"]
<strong>Output:</strong> "fl"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> strs = ["dog","racecar","car"]
<strong>Output:</strong> ""
<strong>Explanation:</strong> There is no common prefix among the input strings.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= strs.length <= 200</code></li>
<li><code>0 <= strs[i].length <= 200</code></li>
<li><code>strs[i]</code> consists of only lowercase English letters.</li>
</ul>
| 1 | {
"code": "class Solution {\npublic:\n string longestCommonPrefix(vector<string>& strs) {\n if (strs.empty()) return \"\";\n string prefix = strs[0];\n for (int i = 1; i < strs.size(); i++) {\n while (strs[i].find(prefix) != 0) {\n prefix = prefix.substr(0, prefix.size() - 1); \n if (prefix.empty()) return \"\";\n }\n }\n return prefix;\n }\n};",
"memory": "11300"
} |
14 | <p>Write a function to find the longest common prefix string amongst an array of strings.</p>
<p>If there is no common prefix, return an empty string <code>""</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> strs = ["flower","flow","flight"]
<strong>Output:</strong> "fl"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> strs = ["dog","racecar","car"]
<strong>Output:</strong> ""
<strong>Explanation:</strong> There is no common prefix among the input strings.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= strs.length <= 200</code></li>
<li><code>0 <= strs[i].length <= 200</code></li>
<li><code>strs[i]</code> consists of only lowercase English letters.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n string longestCommonPrefix(vector<string>& v) {\n string ans=\"\";\n sort(v.begin(),v.end());\n int n=v.size();\n string first=v[0],last=v[n-1];\n for(int i=0;i<min(first.size(),last.size());i++){\n if(first[i]!=last[i]){\n return ans;\n }\n ans+=first[i];\n }\n return ans;\n }\n};",
"memory": "11400"
} |
14 | <p>Write a function to find the longest common prefix string amongst an array of strings.</p>
<p>If there is no common prefix, return an empty string <code>""</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> strs = ["flower","flow","flight"]
<strong>Output:</strong> "fl"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> strs = ["dog","racecar","car"]
<strong>Output:</strong> ""
<strong>Explanation:</strong> There is no common prefix among the input strings.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= strs.length <= 200</code></li>
<li><code>0 <= strs[i].length <= 200</code></li>
<li><code>strs[i]</code> consists of only lowercase English letters.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n string longestCommonPrefix(vector<string>& strs) {\n int len = INT_MAX;\n for(auto x:strs){\n len = len > x.size() ? x.size():len ;\n }\n\n cout << len;\n int cnt = 0;\n bool brk = false;\n for(int j = 0; j < len; j++){\n for(int i = 0; i < strs.size(); i++){\n // cout << strs[i][j] << \",\" << strs[i][0] << endl;\n if(strs[i][j] != strs[0][j]){\n brk = true;\n break;\n }\n }\n if(brk) break;\n cnt++;\n }\n cout << \" \" << cnt;\n return strs[0].substr(0, cnt);\n }\n};",
"memory": "11400"
} |
14 | <p>Write a function to find the longest common prefix string amongst an array of strings.</p>
<p>If there is no common prefix, return an empty string <code>""</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> strs = ["flower","flow","flight"]
<strong>Output:</strong> "fl"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> strs = ["dog","racecar","car"]
<strong>Output:</strong> ""
<strong>Explanation:</strong> There is no common prefix among the input strings.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= strs.length <= 200</code></li>
<li><code>0 <= strs[i].length <= 200</code></li>
<li><code>strs[i]</code> consists of only lowercase English letters.</li>
</ul>
| 2 | {
"code": "class Solution {\npublic:\n string longestCommonPrefix(vector<string>& strs) {\n string ans = \"\";\n sort(strs.begin(), strs.end());\n int n = strs.size();\n string first = strs[0], last = strs[n-1];\n for(int i=0; i<min(first.size(), last.size()); i++) {\n if(first[i] != last[i]) {\n return ans;\n }\n ans += first[i];\n }\n return ans;\n }\n};",
"memory": "11500"
} |
14 | <p>Write a function to find the longest common prefix string amongst an array of strings.</p>
<p>If there is no common prefix, return an empty string <code>""</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> strs = ["flower","flow","flight"]
<strong>Output:</strong> "fl"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> strs = ["dog","racecar","car"]
<strong>Output:</strong> ""
<strong>Explanation:</strong> There is no common prefix among the input strings.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= strs.length <= 200</code></li>
<li><code>0 <= strs[i].length <= 200</code></li>
<li><code>strs[i]</code> consists of only lowercase English letters.</li>
</ul>
| 3 | {
"code": "class Node{\n public:\n Node* links[26];\n bool flag;\n int childs;\n\n Node(){\n for(int i =0; i<26; i++) links[i] = NULL;\n flag = false;\n childs = 0;\n }\n\n bool containsKey(char c){\n return links[c-'a'] != NULL;\n }\n\n Node* get(char c){\n return links[c-'a'];\n }\n void put(char ch, Node* node) {\n links[ch - 'a'] = node;\n childs++;\n }\n\n\n bool isEnd(){\n return flag;\n }\n\n void setEnd(){\n flag = true;\n }\n int childCount(){\n return childs;\n }\n\n};\n\nclass Trie{\n public:\n Node* root;\n Trie(){\n root = new Node;\n \n }\n void insert(string word){\n Node* node = root;\n for(char c: word){\n if(!node->containsKey(c)){\n node->put(c, new Node);\n }\n node = node->get(c);\n }\n node->setEnd();\n }\n\n};\n\nclass Solution {\npublic:\n string longestCommonPrefix(vector<string>& strs) {\n Trie trie;\n for(auto it: strs){\n trie.insert(it);\n }\n\nstring ans = \"\";\n Node* node = trie.root;\n while(node -> childCount() == 1 && !node->isEnd()){\n for(int i =0; i<26; i++){\n if(node->links[i] != NULL){\n ans += (i+'a');\n node = node->links[i];\n break;\n }\n }\n }\n return ans;\n }\n};",
"memory": "11900"
} |
14 | <p>Write a function to find the longest common prefix string amongst an array of strings.</p>
<p>If there is no common prefix, return an empty string <code>""</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> strs = ["flower","flow","flight"]
<strong>Output:</strong> "fl"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> strs = ["dog","racecar","car"]
<strong>Output:</strong> ""
<strong>Explanation:</strong> There is no common prefix among the input strings.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= strs.length <= 200</code></li>
<li><code>0 <= strs[i].length <= 200</code></li>
<li><code>strs[i]</code> consists of only lowercase English letters.</li>
</ul>
| 3 | {
"code": "class trie\n{\n public:\n bool isLeaf;\n struct trie* child[26];\n trie()\n {\n isLeaf = false;\n for (int i = 0; i <26; i++)\n {\n child[i] = NULL;\n }\n }\n\n void insert(string input)\n {\n trie* cur = this;\n for (int i = 0; i <input.length(); i++)\n {\n int index = input[i] - 'a';\n if (cur->child[index] == NULL)\n {\n cur->child[index] = new trie();\n }\n\n cur = cur->child[index];\n }\n\n cur->isLeaf = true;\n }\n\n bool isOneChild()\n {\n int count = 0;\n trie* cur = this;\n\n for (int i = 0; i < 26; i++)\n {\n if (cur->child[i] != NULL)\n {\n count++;\n }\n \n }\n\n return count == 1 ? true : false;\n }\n string lcs(string input)\n {\n string res;\n trie* cur = this;\n\n for (int i = 0; i <input.length(); i++)\n {\n if (cur->isOneChild())\n {\n res.push_back(input[i]);\n cur = cur->child[input[i] - 'a'];\n }\n else\n {\n break;\n }\n\n if (cur->isLeaf)\n {\n break;\n }\n }\n\n return res;\n\n }\n};\n\n\nclass Solution {\npublic:\n string longestCommonPrefix(vector<string>& strs) {\n\n trie* root = new trie();\n\n\n for (int i = 0; i < strs.size(); i++)\n {\n string temp = strs[i];\n if (temp == \"\")\n {\n return temp;\n }\n root->insert(temp);\n }\n\n return root->lcs(strs[0]);\n \n }\n};\n",
"memory": "11900"
} |
14 | <p>Write a function to find the longest common prefix string amongst an array of strings.</p>
<p>If there is no common prefix, return an empty string <code>""</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> strs = ["flower","flow","flight"]
<strong>Output:</strong> "fl"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> strs = ["dog","racecar","car"]
<strong>Output:</strong> ""
<strong>Explanation:</strong> There is no common prefix among the input strings.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= strs.length <= 200</code></li>
<li><code>0 <= strs[i].length <= 200</code></li>
<li><code>strs[i]</code> consists of only lowercase English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n string longestCommonPrefix(vector<string>& strs) {\n int n = strs.size();\n int last = 0;\n if(strs.size() == 1) return strs[0];\n string res = find_common_prefix(strs[0], strs[1]);\n if( res == \"\") return res;\n for(int i=1; i<n; i++){\n res = find_common_prefix(res, strs[i]);\n if(res == \"\") return res;\n } \n return res;\n }\n string find_common_prefix(string s1, string s2)\n {\n int n1 = s1.length();\n int n2 = s2.length();\n string output = \"\";\n if(n1 == 0 || n2 == 0) return output;\n int l = min(n1, n2);\n for(int i=0; i<l; i++){\n if(s1[i] != s2[i]) break;\n else{\n output += s1[i];\n }\n }\n return output;\n }\n};",
"memory": "12000"
} |
14 | <p>Write a function to find the longest common prefix string amongst an array of strings.</p>
<p>If there is no common prefix, return an empty string <code>""</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> strs = ["flower","flow","flight"]
<strong>Output:</strong> "fl"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> strs = ["dog","racecar","car"]
<strong>Output:</strong> ""
<strong>Explanation:</strong> There is no common prefix among the input strings.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= strs.length <= 200</code></li>
<li><code>0 <= strs[i].length <= 200</code></li>
<li><code>strs[i]</code> consists of only lowercase English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n string longestCommonPrefix(vector<string>& strs) {\n if(strs.size()==0)return \"\";\n if(strs.size()==1)return strs[0];\n char ch;\n string ans=\"\";\n for(int i=0;i<200;i++){\n string str=strs[0];\n if(i>=str.size()) return ans;\n ch=str[i];\n for(int j=1;j<strs.size();j++){\n str=strs[j];\n if(i>str.size() || ch!=str[i]){\n return ans;\n }\n \n }\n ans=ans+ch;\n }\n return ans;\n }\n};",
"memory": "12100"
} |
14 | <p>Write a function to find the longest common prefix string amongst an array of strings.</p>
<p>If there is no common prefix, return an empty string <code>""</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> strs = ["flower","flow","flight"]
<strong>Output:</strong> "fl"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> strs = ["dog","racecar","car"]
<strong>Output:</strong> ""
<strong>Explanation:</strong> There is no common prefix among the input strings.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= strs.length <= 200</code></li>
<li><code>0 <= strs[i].length <= 200</code></li>
<li><code>strs[i]</code> consists of only lowercase English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n string longestCommonPrefix(vector<string>& strs) {\n int n = strs.size();\n if(!n) return \"\";\n string req = strs[0];\n for(int i=1;i<n;i++){\n string prev = strs[i-1];\n string curr = strs[i];\n string temp =\"\";\n for(int j = 0 ;j<min(prev.size(),curr.size());j++){\n if(prev[j]==curr[j]) temp += curr[j];\n else break;\n }\n if(i==0||req.size()>temp.size()){\n req = temp;\n \n }\n temp = \"\";\n\n }\n return req;\n \n }\n};",
"memory": "12100"
} |
14 | <p>Write a function to find the longest common prefix string amongst an array of strings.</p>
<p>If there is no common prefix, return an empty string <code>""</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> strs = ["flower","flow","flight"]
<strong>Output:</strong> "fl"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> strs = ["dog","racecar","car"]
<strong>Output:</strong> ""
<strong>Explanation:</strong> There is no common prefix among the input strings.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= strs.length <= 200</code></li>
<li><code>0 <= strs[i].length <= 200</code></li>
<li><code>strs[i]</code> consists of only lowercase English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n string longestCommonPrefix(vector<string>& strs) {\n if (strs.size() == 1) {\n return strs[0];\n }\n // int min_size = 201;\n // for (const auto& s : strs) {\n // min_size = std::min(min_size, s.size());\n // }\n const string min_size_str = *std::min_element(\n strs.begin(), strs.end(), [](const string s1, const string s2) -> bool { return s1.size() < s2.size(); }\n );\n if (min_size_str == \"\") {\n return \"\";\n }\n vector<string> prefixes(min_size_str.size() + 1, \"\");\n string curr_str = \"\";\n for (int i = 0; i < min_size_str.size(); ++i) {\n curr_str += min_size_str[i];\n prefixes[i + 1] = curr_str;\n }\n\n int prefix_id = prefixes.size() - 1;\n for (int i = 0; i < strs.size(); ++i) {\n while (!strs[i].starts_with(prefixes[prefix_id])) {\n --prefix_id;\n if (prefix_id <= 0) {\n return \"\";\n }\n }\n }\n return prefixes[prefix_id];\n }\n};",
"memory": "12200"
} |
14 | <p>Write a function to find the longest common prefix string amongst an array of strings.</p>
<p>If there is no common prefix, return an empty string <code>""</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> strs = ["flower","flow","flight"]
<strong>Output:</strong> "fl"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> strs = ["dog","racecar","car"]
<strong>Output:</strong> ""
<strong>Explanation:</strong> There is no common prefix among the input strings.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= strs.length <= 200</code></li>
<li><code>0 <= strs[i].length <= 200</code></li>
<li><code>strs[i]</code> consists of only lowercase English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n string func_CommonStr(string s1, string s2){\n \n int i = 0, n = s1.size(), m = s2.size() ;\n string ans = \"\" ;\n \n while(i < n && i < m){\n if(s1[i] == s2[i])\n ans += s1[i] ;\n else break ;\n i += 1 ;\n }\n return ans ;\n }\n \n string longestCommonPrefix(vector<string>& strs) {\n string common = strs[0] ;\n for(auto it : strs){\n common = func_CommonStr(it, common) ;\n }\n return common ;\n }\n};",
"memory": "12200"
} |
14 | <p>Write a function to find the longest common prefix string amongst an array of strings.</p>
<p>If there is no common prefix, return an empty string <code>""</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> strs = ["flower","flow","flight"]
<strong>Output:</strong> "fl"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> strs = ["dog","racecar","car"]
<strong>Output:</strong> ""
<strong>Explanation:</strong> There is no common prefix among the input strings.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= strs.length <= 200</code></li>
<li><code>0 <= strs[i].length <= 200</code></li>
<li><code>strs[i]</code> consists of only lowercase English letters.</li>
</ul>
| 3 | {
"code": "class Node{\n public:\n char val;\n vector<Node*>children;\n bool isend;\n Node(char val): val(val){\n children = vector<Node*>(26, nullptr);\n isend=false;\n }\n};\nclass Solution {\npublic:\n string longestCommonPrefix(vector<string>& strs) {\n if(strs.size()==0) return \"\";\n Node* trie = new Node(' ');\n for(auto str: strs){\n Node* curr = trie;\n for(int i=0;i<str.length();i++){\n if(curr->children[str[i]-'a']==nullptr){\n curr->children[str[i]-'a'] = new Node(str[i]);\n }\n curr=curr->children[str[i]-'a'];\n }\n curr->isend=true;\n }\n Node* curr = trie;\n string ans=\"\";\n while(curr!=nullptr){\n if(curr->isend==true)\n break;\n int count = 0;\n Node* temp=nullptr;\n for(int i=0;i<26;i++){\n if(count>1) break;\n if(curr->children[i]!=nullptr){\n count++;\n temp=curr->children[i];\n }\n }\n if(count!=1) break;\n ans+=temp->val;\n curr = temp;\n }\n return ans;\n }\n};",
"memory": "12300"
} |
14 | <p>Write a function to find the longest common prefix string amongst an array of strings.</p>
<p>If there is no common prefix, return an empty string <code>""</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> strs = ["flower","flow","flight"]
<strong>Output:</strong> "fl"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> strs = ["dog","racecar","car"]
<strong>Output:</strong> ""
<strong>Explanation:</strong> There is no common prefix among the input strings.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= strs.length <= 200</code></li>
<li><code>0 <= strs[i].length <= 200</code></li>
<li><code>strs[i]</code> consists of only lowercase English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n string longestCommonPrefix(vector<string>& strs) {\n\n string prefix = \"\";\n\n if (strs.size() == 0)\n {\n return prefix;\n }\n\n if (strs.size() == 1)\n {\n return strs[0];\n }\n\n int indexOfStringWithShortestLength = 0;\n int ShortestStringLength = strs[0].length();\n\n for (int i = 1; i < strs.size(); i++)\n {\n if (strs[i].length() < strs[indexOfStringWithShortestLength].length())\n {\n indexOfStringWithShortestLength = i;\n ShortestStringLength = strs[i].length();\n }\n }\n\n string checkString = strs[0];\n string thisChar = \"\";\n string checkChar = \"\";\n bool addToPrefix = true;\n for (int i = 0; i < ShortestStringLength; i++)\n {\n thisChar = checkString[i];\n for (int j = 1; j < strs.size(); j++)\n {\n checkChar = strs[j];\n checkChar = checkChar[i];\n if (thisChar != checkChar)\n {\n addToPrefix = false;\n }\n }\n if (addToPrefix == false)\n {\n break;\n }\n prefix.append(thisChar);\n }\n\n return prefix;\n }\n};",
"memory": "12300"
} |
14 | <p>Write a function to find the longest common prefix string amongst an array of strings.</p>
<p>If there is no common prefix, return an empty string <code>""</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> strs = ["flower","flow","flight"]
<strong>Output:</strong> "fl"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> strs = ["dog","racecar","car"]
<strong>Output:</strong> ""
<strong>Explanation:</strong> There is no common prefix among the input strings.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= strs.length <= 200</code></li>
<li><code>0 <= strs[i].length <= 200</code></li>
<li><code>strs[i]</code> consists of only lowercase English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n string compare_prefixes(string s1, string s2){\n int c=0, min;\n int n1 = s1.size(), n2 = s2.size();\n string minstr, maxstr;\n if (n1<n2){\n minstr = s1;\n maxstr = s2;\n min = n1;\n }\n else{\n minstr = s2;\n maxstr = s1;\n min = n2;\n }\n if (min==0){\n return \"\";\n }\n for (int i=0;i<min;i++){\n if (minstr[i]==maxstr[i])\n c++;\n else\n break;\n }\n return maxstr.substr(0,c);\n }\n string longestCommonPrefix(vector<string>& strs) {\n string x=strs[0];\n for (int i=1;i<strs.size();i++){\n x = compare_prefixes(x,strs[i]);\n }\n return x;\n }\n};",
"memory": "12400"
} |
14 | <p>Write a function to find the longest common prefix string amongst an array of strings.</p>
<p>If there is no common prefix, return an empty string <code>""</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> strs = ["flower","flow","flight"]
<strong>Output:</strong> "fl"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> strs = ["dog","racecar","car"]
<strong>Output:</strong> ""
<strong>Explanation:</strong> There is no common prefix among the input strings.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= strs.length <= 200</code></li>
<li><code>0 <= strs[i].length <= 200</code></li>
<li><code>strs[i]</code> consists of only lowercase English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n string longestCommonPrefix(vector<string>& strs) {\n if (strs.empty()) return \"\";\n\n int minLen = strs[0].length();\n for (const string& str : strs) {\n minLen = min(minLen, (int)str.length());\n }\n\n unordered_map<int, unordered_map<char, int>> charMap;\n\n for (int i = 0; i < minLen; ++i) {\n for (const string& str : strs) {\n charMap[i][str[i]]++;\n }\n }\n\n string result;\n for (int i = 0; i < minLen; ++i) {\n if (charMap[i].size() == 1 && charMap[i].begin()->second == strs.size()) {\n result += charMap[i].begin()->first;\n } else {\n break;\n }\n }\n\n return result;\n }\n};",
"memory": "12400"
} |
14 | <p>Write a function to find the longest common prefix string amongst an array of strings.</p>
<p>If there is no common prefix, return an empty string <code>""</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> strs = ["flower","flow","flight"]
<strong>Output:</strong> "fl"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> strs = ["dog","racecar","car"]
<strong>Output:</strong> ""
<strong>Explanation:</strong> There is no common prefix among the input strings.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= strs.length <= 200</code></li>
<li><code>0 <= strs[i].length <= 200</code></li>
<li><code>strs[i]</code> consists of only lowercase English letters.</li>
</ul>
| 3 | {
"code": "class Trie\n{\n public:\n int cnt;\n bool isLeaf;\n unordered_map<char, Trie*> son;\n \n Trie()\n {\n cnt = 0;\n isLeaf = false;\n }\n \n void insert(string& word)\n {\n Trie* current = this;\n int n = word.size();\n \n for(int i = 0; i < n; ++i)\n {\n char ch = word[i];\n \n if (current->son[ch] == NULL)\n {\n current->son[ch] = new Trie();\n }\n \n current = current->son[ch];\n }\n \n current->cnt++;\n current->isLeaf = true;\n }\n \n string getLcp(Trie* head)\n {\n Trie* current = head;\n string lcp = \"\";\n \n while(current != NULL && !current->isLeaf && current->son.size() == 1)\n {\n unordered_map<char, Trie*>::iterator data = current->son.begin();\n \n char ch = data->first;\n lcp += ch;\n \n current = current->son[ch];\n }\n \n return lcp;\n }\n};\n\nclass Solution {\npublic:\n string longestCommonPrefix(vector<string>& strs) {\n \n if (strs.size() == 0)\n return \"\";\n \n Trie* root = new Trie();\n \n for(int i = 0; i < strs.size(); ++i)\n {\n root->insert(strs[i]);\n }\n \n return root->getLcp(root);\n \n }\n};",
"memory": "12500"
} |
14 | <p>Write a function to find the longest common prefix string amongst an array of strings.</p>
<p>If there is no common prefix, return an empty string <code>""</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> strs = ["flower","flow","flight"]
<strong>Output:</strong> "fl"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> strs = ["dog","racecar","car"]
<strong>Output:</strong> ""
<strong>Explanation:</strong> There is no common prefix among the input strings.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= strs.length <= 200</code></li>
<li><code>0 <= strs[i].length <= 200</code></li>
<li><code>strs[i]</code> consists of only lowercase English letters.</li>
</ul>
| 3 | {
"code": "#include <bits/stdc++.h>\n\nusing namespace std;\n\nstatic const int nSize = 26;\nstruct Node{\n vector<int> children;\n bool isLast;\n Node(){\n children = vector<int>(nSize, -1);\n isLast = false;\n }\n};\n\nstruct Trie{\n vector<Node> mainTrie;\n string actualLongestPrefixTillNow=\"\";\n Trie(){\n mainTrie.emplace_back();\n }\n void addWord(string& w, bool isfirst = false){\n int rootTillHere = 0;\n string longestPrefixTillNow = \"\";\n\n for(auto &x: w){\n int index = x-'a';\n if(mainTrie[rootTillHere].children[index]==-1){\n if(longestPrefixTillNow.size()<actualLongestPrefixTillNow.size() && !isfirst){\n actualLongestPrefixTillNow = longestPrefixTillNow;\n }\n \n mainTrie[rootTillHere].children[index] = mainTrie.size();\n mainTrie.emplace_back();\n }\n longestPrefixTillNow+=x;\n rootTillHere = mainTrie[rootTillHere].children[index];\n }\n mainTrie[rootTillHere].isLast = true;\n if(longestPrefixTillNow.size()<actualLongestPrefixTillNow.size()){\n actualLongestPrefixTillNow = longestPrefixTillNow;\n }\n }\n};\n\nclass Solution {\npublic:\n string longestCommonPrefix(vector<string>& strs) {\n Trie t = Trie();\n string ans = strs[0];\n t.actualLongestPrefixTillNow = ans;\n int i=0;\n for(auto &x: strs){\n if(i==0){\n t.addWord(x, true);\n i++;\n continue;\n }\n t.addWord(x);\n }\n return t.actualLongestPrefixTillNow;\n }\n};",
"memory": "12600"
} |
14 | <p>Write a function to find the longest common prefix string amongst an array of strings.</p>
<p>If there is no common prefix, return an empty string <code>""</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> strs = ["flower","flow","flight"]
<strong>Output:</strong> "fl"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> strs = ["dog","racecar","car"]
<strong>Output:</strong> ""
<strong>Explanation:</strong> There is no common prefix among the input strings.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= strs.length <= 200</code></li>
<li><code>0 <= strs[i].length <= 200</code></li>
<li><code>strs[i]</code> consists of only lowercase English letters.</li>
</ul>
| 3 | {
"code": "class TrieNode {\npublic:\n std::unordered_map<char, TrieNode*> children;\n bool isEndOfWord;\n\n TrieNode() : isEndOfWord(false) {}\n};\n\n\nclass Trie {\npublic:\n TrieNode* root;\n\n Trie() {\n root = new TrieNode();\n }\n\n void insert(const std::string& word) {\n TrieNode* node = root;\n for (char c : word) {\n if (node->children.find(c) == node->children.end()) {\n node->children[c] = new TrieNode();\n }\n node = node->children[c];\n }\n node->isEndOfWord = true;\n }\n\n TrieNode* getRoot() {\n return root;\n }\n};\n\nclass Solution {\npublic:\n\n std::string FindLongestCommonPrefix(Trie* trie) {\n std::string prefix = \"\";\n TrieNode* node = trie->getRoot();\n while (node && !node->isEndOfWord && node->children.size() == 1) {\n auto it = node->children.begin();\n prefix += it->first;\n node = it->second;\n }\n return prefix;\n }\n string longestCommonPrefix(vector<string>& strs) {\n if (strs.empty()) return \"\";\n\n Trie trie;\n for (const std::string& word : strs) {\n trie.insert(word);\n }\n\n return FindLongestCommonPrefix(&trie);\n }\n};",
"memory": "12700"
} |
14 | <p>Write a function to find the longest common prefix string amongst an array of strings.</p>
<p>If there is no common prefix, return an empty string <code>""</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> strs = ["flower","flow","flight"]
<strong>Output:</strong> "fl"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> strs = ["dog","racecar","car"]
<strong>Output:</strong> ""
<strong>Explanation:</strong> There is no common prefix among the input strings.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= strs.length <= 200</code></li>
<li><code>0 <= strs[i].length <= 200</code></li>
<li><code>strs[i]</code> consists of only lowercase English letters.</li>
</ul>
| 3 | {
"code": "\n class TrieNode{\n public:\n unordered_map<char, TrieNode*> children;\n char val;\n bool isWord = false;\n\n TrieNode(){}\n TrieNode(char v){\n this->val = v;\n }\n };\n\n class Trie{\n private:\n TrieNode* root;\n public:\n Trie(){\n root = new TrieNode();\n }\n TrieNode* getTrieNode(){\n return root;\n }\n void insert(string word){\n TrieNode* temp = root;\n for (auto ch: word){\n if (temp->children.find(ch) == temp->children.end()){\n temp->children[ch] = new TrieNode(ch);\n }\n temp = temp->children[ch];\n }\n temp->isWord = true;\n }\n };\nclass Solution {\npublic:\n string longestCommonPrefix(vector<string>& strs) {\n /*if (strs.empty())\n return \"\";\n string prefix = strs[0];\n for (string s: strs)\n while (s.find(prefix) != 0) \n prefix = prefix.substr(0, prefix.length() - 1);\n return prefix;\n }*/\n\n Trie* pTrie = new Trie();\n for(auto str: strs){\n pTrie->insert(str);\n }\n\n string lcp(\"\");\n TrieNode* curr = pTrie->getTrieNode();\n\n while (curr && !curr->isWord && (curr->children.size() == 1)){\n auto it = curr->children.begin();\n lcp += it->first;\n curr = it->second;\n }\n return lcp;\n }\n\n};\n\n",
"memory": "12800"
} |
14 | <p>Write a function to find the longest common prefix string amongst an array of strings.</p>
<p>If there is no common prefix, return an empty string <code>""</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> strs = ["flower","flow","flight"]
<strong>Output:</strong> "fl"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> strs = ["dog","racecar","car"]
<strong>Output:</strong> ""
<strong>Explanation:</strong> There is no common prefix among the input strings.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= strs.length <= 200</code></li>
<li><code>0 <= strs[i].length <= 200</code></li>
<li><code>strs[i]</code> consists of only lowercase English letters.</li>
</ul>
| 3 | {
"code": "class TrieNode{\n public:\n int val;\n unordered_map<int,TrieNode*> children;\n bool isTerminal;\n \n TrieNode(int val){\n this->val = val;\n this->isTerminal = false;\n }\n};\n\nvoid insert(TrieNode* root,string word) {\n int index = 0;\n TrieNode* ptr = root;\n\n while(index < word.length()){\n char ch = word[index];\n if((ptr->children).find(ch) != (ptr->children).end()){\n ptr = (ptr->children)[ch];\n }\n else{\n // not found;\n TrieNode* newNode = new TrieNode(ch);\n (ptr->children)[ch] = newNode;\n ptr = newNode;\n }\n index++;\n }\n ptr->isTerminal = true;\n}\n\nclass Solution {\npublic:\n string longestCommonPrefix(vector<string>& strs) {\n TrieNode* root = new TrieNode('-');\n\n // insert into trie\n for(int i=0;i<strs.size();i++){\n insert(root,strs[i]);\n }\n\n string ans = \"\";\n // main logic -> until root has single child -> push that char in ans\n TrieNode* ptr = root;\n while(ptr->isTerminal == false && (ptr->children).size() == 1){\n // push that single child in ans string\n // but how to access tat single char -> ???\n // better to use loop although we know it only runs once\n for(auto it : ptr->children){\n char ch = it.first;\n ans.push_back(ch);\n ptr = it.second;\n }\n }\n\n return ans;\n }\n};",
"memory": "12800"
} |
14 | <p>Write a function to find the longest common prefix string amongst an array of strings.</p>
<p>If there is no common prefix, return an empty string <code>""</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> strs = ["flower","flow","flight"]
<strong>Output:</strong> "fl"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> strs = ["dog","racecar","car"]
<strong>Output:</strong> ""
<strong>Explanation:</strong> There is no common prefix among the input strings.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= strs.length <= 200</code></li>
<li><code>0 <= strs[i].length <= 200</code></li>
<li><code>strs[i]</code> consists of only lowercase English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n string findPrefix(string str1, string str2) {\n string ans;\n \n int i = 0, j = 0;\n int n = str1.size(), m = str2.size();\n \n while (i < n && j < m) {\n if (str1[i] == str2[j]) {\n ans += str1[i];\n i++, j++;\n } else {\n break;\n }\n }\n \n return ans;\n }\n string longestCommonPrefix(vector<string>& strs) {\n if(strs.size()==0 )\n {\n return \"\";\n }\n if(strs.size()==1)\n {\n return strs[0];\n }\n string prefix = findPrefix(strs[0],strs[1]);\n for(int i=0;i<strs.size()-1;i++)\n {\n string pre = findPrefix(strs[i],strs[i+1]);\n prefix= findPrefix(pre,prefix);\n }\n \n return prefix;\n }\n};",
"memory": "12900"
} |
14 | <p>Write a function to find the longest common prefix string amongst an array of strings.</p>
<p>If there is no common prefix, return an empty string <code>""</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> strs = ["flower","flow","flight"]
<strong>Output:</strong> "fl"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> strs = ["dog","racecar","car"]
<strong>Output:</strong> ""
<strong>Explanation:</strong> There is no common prefix among the input strings.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= strs.length <= 200</code></li>
<li><code>0 <= strs[i].length <= 200</code></li>
<li><code>strs[i]</code> consists of only lowercase English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n string findPrefix(string str1, string str2) {\n string ans;\n \n int i = 0, j = 0;\n int n = str1.size(), m = str2.size();\n \n while (i < n && j < m) {\n if (str1[i] == str2[j]) {\n ans += str1[i];\n i++, j++;\n } else {\n break;\n }\n }\n \n return ans;\n }\n string longestCommonPrefix(vector<string>& strs) {\n if(strs.size()==0 )\n {\n return \"\";\n }\n if(strs.size()==1)\n {\n return strs[0];\n }\n string prefix = findPrefix(strs[0],strs[1]);\n for(int i=0;i<strs.size()-1;i++)\n {\n string pre = findPrefix(strs[i],strs[i+1]);\n prefix= findPrefix(pre,prefix);\n }\n \n return prefix;\n }\n};",
"memory": "12900"
} |
14 | <p>Write a function to find the longest common prefix string amongst an array of strings.</p>
<p>If there is no common prefix, return an empty string <code>""</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> strs = ["flower","flow","flight"]
<strong>Output:</strong> "fl"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> strs = ["dog","racecar","car"]
<strong>Output:</strong> ""
<strong>Explanation:</strong> There is no common prefix among the input strings.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= strs.length <= 200</code></li>
<li><code>0 <= strs[i].length <= 200</code></li>
<li><code>strs[i]</code> consists of only lowercase English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n string longestCommonPrefix(vector<string>& strs) \n {\n int n=strs.size();\n string res=\"\";\n unordered_map<string, int> mp;\n for(int i=0;i<n;i++)\n {\n string temp=\"\";\n for(auto it:strs[i])\n {\n temp+=it;\n mp[temp]++;\n if(mp[temp]==n)\n {\n if(temp.size()>res.size()) res=temp;\n }\n }\n }\n return res;\n }\n};",
"memory": "13000"
} |
14 | <p>Write a function to find the longest common prefix string amongst an array of strings.</p>
<p>If there is no common prefix, return an empty string <code>""</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> strs = ["flower","flow","flight"]
<strong>Output:</strong> "fl"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> strs = ["dog","racecar","car"]
<strong>Output:</strong> ""
<strong>Explanation:</strong> There is no common prefix among the input strings.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= strs.length <= 200</code></li>
<li><code>0 <= strs[i].length <= 200</code></li>
<li><code>strs[i]</code> consists of only lowercase English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n string longestCommonPrefix(vector<string>& strs) \n {\n int n=strs.size();\n string res=\"\";\n unordered_map<string, int> mp;\n for(int i=0;i<n;i++)\n {\n string temp=\"\";\n for(auto it:strs[i])\n {\n temp+=it;\n mp[temp]++;\n if(mp[temp]==n)\n {\n if(temp.size()>res.size()) res=temp;\n }\n }\n }\n return res;\n }\n};",
"memory": "13000"
} |
14 | <p>Write a function to find the longest common prefix string amongst an array of strings.</p>
<p>If there is no common prefix, return an empty string <code>""</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> strs = ["flower","flow","flight"]
<strong>Output:</strong> "fl"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> strs = ["dog","racecar","car"]
<strong>Output:</strong> ""
<strong>Explanation:</strong> There is no common prefix among the input strings.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= strs.length <= 200</code></li>
<li><code>0 <= strs[i].length <= 200</code></li>
<li><code>strs[i]</code> consists of only lowercase English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n string longestCommonPrefix(vector<string>& strs) \n {\n int n=strs.size();\n string res=\"\";\n unordered_map<string, int> mp;\n for(int i=0;i<n;i++)\n {\n string temp=\"\";\n for(auto it:strs[i])\n {\n temp+=it;\n mp[temp]++;\n if(mp[temp]==n)\n {\n if(temp.size()>res.size()) res=temp;\n }\n }\n }\n return res;\n }\n};",
"memory": "13100"
} |
14 | <p>Write a function to find the longest common prefix string amongst an array of strings.</p>
<p>If there is no common prefix, return an empty string <code>""</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> strs = ["flower","flow","flight"]
<strong>Output:</strong> "fl"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> strs = ["dog","racecar","car"]
<strong>Output:</strong> ""
<strong>Explanation:</strong> There is no common prefix among the input strings.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= strs.length <= 200</code></li>
<li><code>0 <= strs[i].length <= 200</code></li>
<li><code>strs[i]</code> consists of only lowercase English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n string longestCommonPrefix(vector<string>& strs) \n {\n int n=strs.size();\n string res=\"\";\n unordered_map<string, int> mp;\n for(int i=0;i<n;i++)\n {\n string temp=\"\";\n for(auto it:strs[i])\n {\n temp+=it;\n mp[temp]++;\n if(mp[temp]==n)\n {\n if(temp.size()>res.size()) res=temp;\n }\n }\n }\n return res;\n }\n};",
"memory": "13100"
} |
14 | <p>Write a function to find the longest common prefix string amongst an array of strings.</p>
<p>If there is no common prefix, return an empty string <code>""</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> strs = ["flower","flow","flight"]
<strong>Output:</strong> "fl"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> strs = ["dog","racecar","car"]
<strong>Output:</strong> ""
<strong>Explanation:</strong> There is no common prefix among the input strings.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= strs.length <= 200</code></li>
<li><code>0 <= strs[i].length <= 200</code></li>
<li><code>strs[i]</code> consists of only lowercase English letters.</li>
</ul>
| 3 | {
"code": "class TrieNode {\npublic:\n unordered_map<char, TrieNode*> children;\n};\n\nclass Trie {\nprivate:\n TrieNode* root = new TrieNode;\npublic:\n void insert(string word) {\n TrieNode* curr = this->root;\n\n for (char c : word) {\n if (!curr->children.count(c)) {\n curr->children[c] = new TrieNode;\n }\n curr = curr->children[c];\n }\n }\n\n int search(string word) {\n TrieNode* curr = this->root;\n int len = 0;\n\n for (char c : word) {\n if (curr->children.size() != 1) {\n return len;\n }\n len += 1;\n curr = curr->children[c];\n }\n return len;\n }\n};\n\n\nclass Solution {\npublic:\n string longestCommonPrefix(vector<string>& strs) {\n Trie trie;\n\n int len = INT_MAX;\n\n for (string word : strs) {\n trie.insert(word);\n }\n\n for (string word : strs) {\n len = min(len, trie.search(word));\n }\n\n return strs[0].substr(0, len);\n }\n};",
"memory": "13200"
} |
14 | <p>Write a function to find the longest common prefix string amongst an array of strings.</p>
<p>If there is no common prefix, return an empty string <code>""</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> strs = ["flower","flow","flight"]
<strong>Output:</strong> "fl"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> strs = ["dog","racecar","car"]
<strong>Output:</strong> ""
<strong>Explanation:</strong> There is no common prefix among the input strings.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= strs.length <= 200</code></li>
<li><code>0 <= strs[i].length <= 200</code></li>
<li><code>strs[i]</code> consists of only lowercase English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n // Helper function to find the common prefix between two strings\n string commonPrefix(const string& str1, const string& str2) {\n int minLength = min(str1.length(), str2.length());\n for (int i = 0; i < minLength; ++i) {\n if (str1[i] != str2[i]) {\n return str1.substr(0, i); // Return the common prefix found so far\n }\n }\n return str1.substr(0, minLength); // Return the entire shorter string if all characters match\n }\n\n // Main function to find the longest common prefix among an array of strings\n string longestCommonPrefix(vector<string>& strs) {\n if (strs.empty()) return \"\"; // If the input vector is empty, return an empty string\n return longestCommonPrefixHelper(strs, 0, strs.size() - 1); // Call the helper function\n }\n\n // Helper function for divide and conquer\n string longestCommonPrefixHelper(vector<string>& strs, int left, int right) {\n if (left == right) {\n return strs[left]; // Base case: only one string\n }\n int mid = left + (right - left) / 2; // Find the midpoint\n string leftPrefix = longestCommonPrefixHelper(strs, left, mid); // Recursively find prefix in the left half\n string rightPrefix = longestCommonPrefixHelper(strs, mid + 1, right); // Recursively find prefix in the right half\n return commonPrefix(leftPrefix, rightPrefix); // Combine the results\n }\n};",
"memory": "13300"
} |
14 | <p>Write a function to find the longest common prefix string amongst an array of strings.</p>
<p>If there is no common prefix, return an empty string <code>""</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> strs = ["flower","flow","flight"]
<strong>Output:</strong> "fl"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> strs = ["dog","racecar","car"]
<strong>Output:</strong> ""
<strong>Explanation:</strong> There is no common prefix among the input strings.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= strs.length <= 200</code></li>
<li><code>0 <= strs[i].length <= 200</code></li>
<li><code>strs[i]</code> consists of only lowercase English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n string longestCommonPrefix(vector<string>& strs) \n {\n int n=strs.size();\n string res=\"\";\n map<string, int> mp;\n for(int i=0;i<n;i++)\n {\n string temp=\"\";\n for(auto it:strs[i])\n {\n temp+=it;\n mp[temp]++;\n if(mp[temp]==n)\n {\n if(temp.size()>res.size()) res=temp;\n }\n }\n }\n return res;\n }\n};",
"memory": "13300"
} |
14 | <p>Write a function to find the longest common prefix string amongst an array of strings.</p>
<p>If there is no common prefix, return an empty string <code>""</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> strs = ["flower","flow","flight"]
<strong>Output:</strong> "fl"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> strs = ["dog","racecar","car"]
<strong>Output:</strong> ""
<strong>Explanation:</strong> There is no common prefix among the input strings.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= strs.length <= 200</code></li>
<li><code>0 <= strs[i].length <= 200</code></li>
<li><code>strs[i]</code> consists of only lowercase English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n string divideAndSolve(vector<string>& str, int start, int end) {\n if (start == end) return str[start];\n\n int mid = start + (end - start) / 2;\n string LCPofLeft = divideAndSolve(str, start, mid);\n string LCPofRight = divideAndSolve(str, mid + 1, end);\n\n int i = 0;\n while (i < min(LCPofLeft.size(), LCPofRight.size())) {\n if (LCPofLeft[i] != LCPofRight[i])\n break;\n i++;\n }\n\n return i == 0? \"\" : LCPofLeft.substr(0, i);\n }\n string longestCommonPrefix(vector<string>& strs) {\n return divideAndSolve(strs, 0, strs.size() - 1);\n }\n};\n// aa aab aabc\n// 0 2\n\n// aa aab (0, 1)--> aa (0, 0) --> aa --> aa\n// --> aab(1, 1) --> aab\n// aabc (2, 2) --> aabc",
"memory": "13400"
} |
14 | <p>Write a function to find the longest common prefix string amongst an array of strings.</p>
<p>If there is no common prefix, return an empty string <code>""</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> strs = ["flower","flow","flight"]
<strong>Output:</strong> "fl"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> strs = ["dog","racecar","car"]
<strong>Output:</strong> ""
<strong>Explanation:</strong> There is no common prefix among the input strings.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= strs.length <= 200</code></li>
<li><code>0 <= strs[i].length <= 200</code></li>
<li><code>strs[i]</code> consists of only lowercase English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\nprivate:\n string lcp(vector<string>& strs, int begin, int end) {\n if (end == begin + 1) {\n return strs[begin];\n }\n\n int mid = (begin + end) / 2;\n string lcpLeft = lcp(strs, begin, mid);\n string lcpRight = lcp(strs, mid, end);\n\n int minLen = min(lcpLeft.size(), lcpRight.size());\n int idx;\n for (idx = 0; idx < minLen; idx++) {\n if (lcpLeft[idx] != lcpRight[idx]) {\n break;\n }\n }\n\n return lcpLeft.substr(0, idx);\n }\npublic:\n string longestCommonPrefix(vector<string>& strs) {\n return lcp(strs, 0, strs.size());\n }\n};",
"memory": "13500"
} |
14 | <p>Write a function to find the longest common prefix string amongst an array of strings.</p>
<p>If there is no common prefix, return an empty string <code>""</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> strs = ["flower","flow","flight"]
<strong>Output:</strong> "fl"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> strs = ["dog","racecar","car"]
<strong>Output:</strong> ""
<strong>Explanation:</strong> There is no common prefix among the input strings.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= strs.length <= 200</code></li>
<li><code>0 <= strs[i].length <= 200</code></li>
<li><code>strs[i]</code> consists of only lowercase English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n string longestCommonPrefix(vector<string> &strs) {\n return aux(strs, 0, strs.size());\n }\n\nprivate:\n string aux(vector<string> &strs, size_t begin, size_t end) {\n size_t n = end - begin;\n if (n == 1) {\n return strs[begin];\n } else if (n == 2) {\n size_t i;\n for (i = 0; i < min(strs[begin].size(), strs[end - 1].size()); ++i) {\n if (strs[begin][i] != strs[end - 1][i]) {\n return strs[begin].substr(0, i);\n }\n }\n if (i == strs[begin].size()) {\n return strs[begin];\n } else {\n return strs[end - 1];\n }\n } else {\n n /= 2;\n vector<string> tmp{aux(strs, begin, begin + n),\n aux(strs, begin + n, end)};\n return aux(tmp, 0, 2);\n }\n }\n};\n",
"memory": "13600"
} |
14 | <p>Write a function to find the longest common prefix string amongst an array of strings.</p>
<p>If there is no common prefix, return an empty string <code>""</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> strs = ["flower","flow","flight"]
<strong>Output:</strong> "fl"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> strs = ["dog","racecar","car"]
<strong>Output:</strong> ""
<strong>Explanation:</strong> There is no common prefix among the input strings.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= strs.length <= 200</code></li>
<li><code>0 <= strs[i].length <= 200</code></li>
<li><code>strs[i]</code> consists of only lowercase English letters.</li>
</ul>
| 3 | {
"code": "class Solution;\nclass Node{\n char data;\n int rep;\n unordered_map<char, Node*> m;\n bool isTerminal;\n public:\n Node(char d){\n data = d;\n rep = 0;\n isTerminal = false;\n }\n\n friend class Solution; \n};\n\nclass Solution {\nNode* root;\npublic:\n Solution(){\n root = new Node('\\0');\n }\n\n // Prefix{\n string longg(Node* tmp,int limit, string ab){\n // Node* tmp = root;\n for(auto x:tmp->m){\n // cout<<x.first<<\" \"<<x.second->rep<<endl;\n if(x.second->rep == limit){\n ab.push_back(x.first);\n ab = longg(x.second,limit,ab);\n return ab;\n }\n }\n // cout<<tmp->m.size()<<\" \"<<tmp->rep<<endl;\n return ab;\n }\n\n // Insertion\n void insert(Node* tmp, string word){\n // Node* tmp = root;\n for(char ch: word){\n if(tmp->m.count(ch) == 0){\n Node* n = new Node(ch);\n tmp->m[ch] = n;\n }\n tmp = tmp->m[ch];\n tmp->rep++;\n // cout<<tmp->data<<\" \"<<tmp->rep<<endl;\n }\n tmp->isTerminal = true;\n\n \n }\n\n\n\n string run(vector<string> strs){\n Node* tmp = root;\n Solution n;\n for(auto s: strs){\n n.insert(tmp,s);\n } \n\n string ac = \"\";\n // Node* tmp = root;\n return longg(tmp, strs.size(), ac);\n }\n\n string longestCommonPrefix(vector<string>& strs) {\n // Solution n;\n // Node* tmp = root;\n // for(auto s: strs){\n // n.insert(s);\n // }\n\n \n\n return run(strs);\n\n\n }\n};",
"memory": "13700"
} |
14 | <p>Write a function to find the longest common prefix string amongst an array of strings.</p>
<p>If there is no common prefix, return an empty string <code>""</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> strs = ["flower","flow","flight"]
<strong>Output:</strong> "fl"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> strs = ["dog","racecar","car"]
<strong>Output:</strong> ""
<strong>Explanation:</strong> There is no common prefix among the input strings.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= strs.length <= 200</code></li>
<li><code>0 <= strs[i].length <= 200</code></li>
<li><code>strs[i]</code> consists of only lowercase English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n string longestCommonPrefix(vector<string>& strs) {\n if (strs.size() == 1) return strs[0];\n int len = strs.size();\n string s1 = strs[len-1];\n string s2 = strs[len-2];\n\n if (s1.length() == 0 || s2.length() == 0) return \"\";\n\n string substr = \"\";\n string subbie = \"\";\n int i=0;\n int c1_counter = 0;\n int c2_counter = 0;\n \n char c1 = s1[c1_counter];\n char c2 = s2[c2_counter];\n while (c1 == c2) {\n subbie.push_back(c1);\n c1_counter++;\n c2_counter++;\n if (c1_counter == s1.length() || c2_counter == s2.length()) break;\n\n c1 = s1[c1_counter];\n c2 = s2[c2_counter];\n }\n if (subbie == \"\") return subbie;\n\n substr = subbie;\n strs.pop_back();\n strs.pop_back();\n strs.push_back(substr);\n substr = longestCommonPrefix(strs); \n\n return substr;\n }\n};",
"memory": "13800"
} |
14 | <p>Write a function to find the longest common prefix string amongst an array of strings.</p>
<p>If there is no common prefix, return an empty string <code>""</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> strs = ["flower","flow","flight"]
<strong>Output:</strong> "fl"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> strs = ["dog","racecar","car"]
<strong>Output:</strong> ""
<strong>Explanation:</strong> There is no common prefix among the input strings.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= strs.length <= 200</code></li>
<li><code>0 <= strs[i].length <= 200</code></li>
<li><code>strs[i]</code> consists of only lowercase English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n string lcp(string &s1,string &s2){\n int i,j,n=s1.length(),m=s2.length();\n i=0;j=0;\n while(i<n && j<m){\n if(s1[i] != s2[j]) break;\n i++;j++;\n }\n return s1.substr(0, min(i,j));\n }\n string get(vector<string> &v,int low,int high){\n if(low == high) return v[low];\n if(low > high) return \"\";\n int mid = (low+ high)/2;\n string s1,s2;\n s1 = get(v,low,mid);\n s2= get(v,mid+1,high);\n return lcp(s1,s2);\n }\n string longestCommonPrefix(vector<string>& v) {\n if(v.size() == 0) return \"\";\n return get(v,0,v.size() -1);\n }\n};",
"memory": "13900"
} |
14 | <p>Write a function to find the longest common prefix string amongst an array of strings.</p>
<p>If there is no common prefix, return an empty string <code>""</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> strs = ["flower","flow","flight"]
<strong>Output:</strong> "fl"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> strs = ["dog","racecar","car"]
<strong>Output:</strong> ""
<strong>Explanation:</strong> There is no common prefix among the input strings.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= strs.length <= 200</code></li>
<li><code>0 <= strs[i].length <= 200</code></li>
<li><code>strs[i]</code> consists of only lowercase English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n bool areEqual(string str1, string str2){\n if(str1.size() != str2.size()) return 0;\n for(int i=0; i<str1.size(); i++){\n if(str1[i] != str2[i]) return 0;\n }\n return 1;\n }\n string longestCommonPrefix(vector<string>& strs) {\n string prefix = strs[0];\n\n for(int i = 1; i<strs.size(); i++){\n string x = strs[i];\n while(1){\n cout<<prefix<<endl;\n if(areEqual(prefix,x)) break;\n \n int s = prefix.size();\n if(prefix.size() == 0) return \"\";\n\n if(prefix.size() > x.size()) prefix = prefix.substr(0,s-1);\n else if(prefix.size() == x.size()){\n prefix = prefix.substr(0,s-1);\n x = x.substr(0,x.size()-1);\n }\n else x = x.substr(0,x.size()-1);\n }\n \n }\n return prefix;\n }\n};",
"memory": "14000"
} |
14 | <p>Write a function to find the longest common prefix string amongst an array of strings.</p>
<p>If there is no common prefix, return an empty string <code>""</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> strs = ["flower","flow","flight"]
<strong>Output:</strong> "fl"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> strs = ["dog","racecar","car"]
<strong>Output:</strong> ""
<strong>Explanation:</strong> There is no common prefix among the input strings.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= strs.length <= 200</code></li>
<li><code>0 <= strs[i].length <= 200</code></li>
<li><code>strs[i]</code> consists of only lowercase English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n string longestCommonPrefix(vector<string>& strs) {\n string str;\n\n for(size_t i = strs[0].length(); i > 0; i--)\n {\n str = string(strs[0], 0, i);\n size_t j = 1;\n for(; j < strs.size() and !str.compare(string(strs[j], 0, i)) ; j++)\n {}\n if(j == strs.size())\n return str;\n }\n return \"\";\n\n }\n};",
"memory": "14200"
} |
14 | <p>Write a function to find the longest common prefix string amongst an array of strings.</p>
<p>If there is no common prefix, return an empty string <code>""</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> strs = ["flower","flow","flight"]
<strong>Output:</strong> "fl"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> strs = ["dog","racecar","car"]
<strong>Output:</strong> ""
<strong>Explanation:</strong> There is no common prefix among the input strings.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= strs.length <= 200</code></li>
<li><code>0 <= strs[i].length <= 200</code></li>
<li><code>strs[i]</code> consists of only lowercase English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n static bool compare(string s1, string s2){\n if(s1.length() < s2.length())\n return true;\n else return false;\n }\n string longestCommonPrefix(vector<string>& strs) {\n sort(strs.begin(), strs.end(), compare);\n int n = strs[0].length();\n int len = strs.size();\n int i = 0;\n string ans = \"\";\n while(i < n){\n int j = 0;\n bool isAns = true;\n while(j < len - 1){\n if(strs[j][i] != strs[j + 1][i]){\n isAns = false;\n break;\n }\n j++;\n }\n if(!isAns) break;\n ans += strs[j][i++];\n }\n return ans;\n }\n};",
"memory": "14300"
} |
14 | <p>Write a function to find the longest common prefix string amongst an array of strings.</p>
<p>If there is no common prefix, return an empty string <code>""</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> strs = ["flower","flow","flight"]
<strong>Output:</strong> "fl"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> strs = ["dog","racecar","car"]
<strong>Output:</strong> ""
<strong>Explanation:</strong> There is no common prefix among the input strings.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= strs.length <= 200</code></li>
<li><code>0 <= strs[i].length <= 200</code></li>
<li><code>strs[i]</code> consists of only lowercase English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n string longestCommonPrefix(vector<string>& strs) {\n // size_t longestPrefix = 0;\n string str;\n\n for(size_t i = strs[0].length(); i > 0; i--)\n {\n str = string(strs[0], 0, i);\n // cout << \"---> \"<< str << endl;\n size_t j = 1;\n for(; j < strs.size() and !str.compare(string(strs[j], 0, i)) ; j++)\n {\n // cout << \"-----> here \" << endl;\n }\n if(j == strs.size())\n return str;\n }\n return \"\";\n\n }\n};",
"memory": "14400"
} |
14 | <p>Write a function to find the longest common prefix string amongst an array of strings.</p>
<p>If there is no common prefix, return an empty string <code>""</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> strs = ["flower","flow","flight"]
<strong>Output:</strong> "fl"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> strs = ["dog","racecar","car"]
<strong>Output:</strong> ""
<strong>Explanation:</strong> There is no common prefix among the input strings.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= strs.length <= 200</code></li>
<li><code>0 <= strs[i].length <= 200</code></li>
<li><code>strs[i]</code> consists of only lowercase English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n string longestCommonPrefix(vector<string>& strs) {\n std::string st = strs[0], answer = \"\";\n int k = 0;\n for (int i = st.length(); i > 0; i--) {\n for (int j = 1; j < strs.size(); j++) {\n if (strs[j].find(st.substr(0, i)) == 0) {\n k++;\n } else {\n k = 0;\n break;\n }\n }\n if (k == strs.size() - 1) {\n answer = st.substr(0, i);\n return answer;\n }\n }\n return answer;\n }\n};",
"memory": "14400"
} |
14 | <p>Write a function to find the longest common prefix string amongst an array of strings.</p>
<p>If there is no common prefix, return an empty string <code>""</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> strs = ["flower","flow","flight"]
<strong>Output:</strong> "fl"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> strs = ["dog","racecar","car"]
<strong>Output:</strong> ""
<strong>Explanation:</strong> There is no common prefix among the input strings.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= strs.length <= 200</code></li>
<li><code>0 <= strs[i].length <= 200</code></li>
<li><code>strs[i]</code> consists of only lowercase English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\n static bool cmp(string s1,string s2){\n return s1.length()< s2.length();\n }\npublic:\n string longestCommonPrefix(vector<string>& strs) {\n int n=strs.size();\n if(n==0) return \"\";\n if(n==1) return strs[0];\n string ans=\"\";\n sort(strs.begin(),strs.end(),cmp);\n cout<<strs[0]<<\" \" <<strs[n-1]<<endl;\n \n for(int i=0;i<strs[0].size();i++){\n char ch=strs[0][i];\n for(int j=1;j<n;j++){\n if(ch!=strs[j][i]){\n return ans;\n }\n \n }\n ans.push_back(ch);\n }\n return ans;\n \n }\n};",
"memory": "14500"
} |
14 | <p>Write a function to find the longest common prefix string amongst an array of strings.</p>
<p>If there is no common prefix, return an empty string <code>""</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> strs = ["flower","flow","flight"]
<strong>Output:</strong> "fl"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> strs = ["dog","racecar","car"]
<strong>Output:</strong> ""
<strong>Explanation:</strong> There is no common prefix among the input strings.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= strs.length <= 200</code></li>
<li><code>0 <= strs[i].length <= 200</code></li>
<li><code>strs[i]</code> consists of only lowercase English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\n static bool cmp(string s1,string s2){\n return s1.length()< s2.length();\n }\npublic:\n string longestCommonPrefix(vector<string>& strs) {\n int n=strs.size();\n if(n==0) return \"\";\n if(n==1) return strs[0];\n string ans=\"\";\n sort(strs.begin(),strs.end(),cmp);\n //cout<<strs[0]<<\" \" <<strs[n-1]<<endl;\n \n for(int i=0;i<strs[0].size();i++){\n char ch=strs[0][i];\n for(int j=1;j<n;j++){\n if(ch!=strs[j][i]){\n return ans;\n }\n \n }\n ans.push_back(ch);\n }\n return ans;\n \n }\n};",
"memory": "14500"
} |
14 | <p>Write a function to find the longest common prefix string amongst an array of strings.</p>
<p>If there is no common prefix, return an empty string <code>""</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> strs = ["flower","flow","flight"]
<strong>Output:</strong> "fl"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> strs = ["dog","racecar","car"]
<strong>Output:</strong> ""
<strong>Explanation:</strong> There is no common prefix among the input strings.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= strs.length <= 200</code></li>
<li><code>0 <= strs[i].length <= 200</code></li>
<li><code>strs[i]</code> consists of only lowercase English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n static bool comparator(string str1, string str2){\n return str1.size() < str2.size();\n }\n string longestCommonPrefix(vector<string>& strs) {\n string substring = \"\";\n sort(strs.begin(),strs.end(),comparator);\n // the largest prefix can be of the strs[0]\n string prefix = strs[0];\n int flag = 0;\n while(prefix.size() >= 0){\n flag = 0;\n for(int i = 1; i < strs.size(); i++){\n if(prefix == strs[i].substr(0,prefix.size())){\n flag++;\n continue;\n }\n else{\n break;\n }\n }\n if(flag == strs.size()-1){\n return prefix;\n }\n prefix = prefix.substr(0,prefix.size()-1);\n }\n return \"\";\n }\n};",
"memory": "14600"
} |
14 | <p>Write a function to find the longest common prefix string amongst an array of strings.</p>
<p>If there is no common prefix, return an empty string <code>""</code>.</p>
<p> </p>
<p><strong class="example">Example 1:</strong></p>
<pre>
<strong>Input:</strong> strs = ["flower","flow","flight"]
<strong>Output:</strong> "fl"
</pre>
<p><strong class="example">Example 2:</strong></p>
<pre>
<strong>Input:</strong> strs = ["dog","racecar","car"]
<strong>Output:</strong> ""
<strong>Explanation:</strong> There is no common prefix among the input strings.
</pre>
<p> </p>
<p><strong>Constraints:</strong></p>
<ul>
<li><code>1 <= strs.length <= 200</code></li>
<li><code>0 <= strs[i].length <= 200</code></li>
<li><code>strs[i]</code> consists of only lowercase English letters.</li>
</ul>
| 3 | {
"code": "class Solution {\npublic:\n string longestCommonPrefix(vector<string>& strs) {\n sort(strs.begin(),strs.end(),[](string A,string B){return A.size()<B.size();});\n string str=strs[0];\n str.push_back('0');\n for(int i=0;i<str.size()+2;i++){\n str.pop_back();\n bool found=true;\n for(auto s:strs){\n if(s.compare(0,str.size(),str)!=0){\n found=false;\n break;\n }\n } \n if(found)return str;\n } \n return \"\";\n }\n};",
"memory": "14700"
} |
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.