arjunatapadkar / codeteria

Ed-tech and software development website to provide the free coding resources & useful software tools/products.
https://codeteria.vercel.app
33 stars 63 forks source link

New Page : DS Problem specific page #142

Open arjunatapadkar opened 1 month ago

arjunatapadkar commented 1 month ago

Screenshot (262)

on clicking the "read more" of each question the new page should be opened

Each problem data looks like this :

{ "_id": "66e00c1edc1b019d344e2673", "title": "1768. Merge Strings Alternately", "statement": "You are given two strings word1 and word2. Merge the strings by adding letters in alternating order, starting with word1. If a string is longer than the other, append the additional letters onto the end of the merged string.\n\nReturn the merged string.\n\n\nCode\nTestcase\nTest Result\nTest Result\n1768. Merge Strings Alternately\nSolved\nEasy\nTopics\nCompanies\nHint\nYou are given two strings word1 and word2. Merge the strings by adding letters in alternating order, starting with word1. If a string is longer than the other, append the additional letters onto the end of the merged string.\n\nReturn the merged string.\n\nExample 1:\nInput: word1 = \"abc\", word2 = \"pqr\"\nOutput: \"apbqcr\"\nExplanation: The merged string will be merged as so:\nword1: a b c\nword2: p q r\nmerged: a p b q c r\n\n",

"IO": [ { "_id": "66e00c5adc1b019d344e2676", "input": "word1 = \"abc\", word2 = \"pqr\"", "output": " \"apbqcr\"", "v": 0 }, { "_id": "66e00c75dc1b019d344e267c", "input": "word1 = \"ab\", word2 = \"pqrs\"", "output": "\"apbqrs\"", "__v": 0 }, { "_id": "66e00c93dc1b019d344e2682", "input": "word1 = \"abcd\", word2 = \"pq\"", "output": "\"apbqcd\"", "v": 0 } ],

"constraints": [ { "_id": "66e00ca3dc1b019d344e2688", "words": "1 <= word1.length, word2.length <= 100", "isComp": false, "__v": 0 }, { "_id": "66e00cb4dc1b019d344e268f", "words": "word1 and word2 consist of lowercase English letters.", "isComp": false, "__v": 0 } ],

"answers": [ { "_id": "66e00ed3dc1b019d344e26ae", "title": "Optimal", "code": "class Solution {\npublic:\n string mergeAlternately(string word1, string word2) {\n string result = \"\";\n int i = 0;\n while (i < word1.length() || i < word2.length()) {\n if (i < word1.length()) {\n result += word1[i];\n }\n if (i < word2.length()) {\n result += word2[i];\n }\n i++;\n }\n return result;\n }\n};", "explain": "Intuition\nThe problem asks to merge two strings alternately. Therefore, we can traverse both strings at the same time and pick each character alternately from both strings.\n\nApproach\nInitialize an empty string to store the merged result.\nTraverse both input strings together, picking each character alternately from both strings and appending it to the merged result string.\nContinue the traversal until the end of the longer string is reached.\nReturn the merged result string.\nComplexity\nTime complexity:\nSince we traverse both strings once and pick each character alternately, the time complexity of this approach is O(n), where n is the length of the longer string.\n\nSpace complexity:\nWe use a StringBuilder to store the merged string, so the space complexity of this approach is O(n), where n is the length of the longer string.", "v": 0 }, { "_id": "66e00f38dc1b019d344e26b7", "title": "Brute Force Solution: Iterating Through Characters", "code": "#include \n#include \nusing namespace std;\n\nstring mergeAlternately(string word1, string word2) {\n string merged;\n int i = 0, j = 0;\n\n // Add characters alternately from both words\n while (i < word1.size() && j < word2.size()) {\n merged.push_back(word1[i++]);\n merged.push_back(word2[j++]);\n }\n\n // Append the remaining characters if any\n while (i < word1.size()) {\n merged.push_back(word1[i++]);\n }\n\n while (j < word2.size()) {\n merged.push_back(word2[j++]);\n }\n\n return merged;\n}\n\nint main() {\n string word1 = \"abc\", word2 = \"pqr\";\n cout << mergeAlternately(word1, word2) << endl;\n return 0;\n}\n", "explain": "Explanation:\n\nWe use two pointers, i and j, to iterate through both strings word1 and word2.\nCharacters are alternately appended to the merged string.\nIf one string is longer, the remaining characters from that string are appended at the end.\nTime Complexity: O(n), where n is the total length of word1 and word2.", "__v": 0 }, { "_id": "66e00f6adc1b019d344e26c0", "title": "Optimized Solution: Using a Single Loop and Substring", "code": "#include \n#include \nusing namespace std;\n\nstring mergeAlternately(string word1, string word2) {\n string merged;\n int i = 0, n1 = word1.size(), n2 = word2.size();\n\n // Iterate through both words and append alternating characters\n while (i < n1 && i < n2) {\n merged.push_back(word1[i]);\n merged.push_back(word2[i]);\n ++i;\n }\n\n // Append the remaining part of the longer string\n if (i < n1) merged += word1.substr(i);\n if (i < n2) merged += word2.substr(i);\n\n return merged;\n}\n\nint main() {\n string word1 = \"ab\", word2 = \"pqrs\";\n cout << mergeAlternately(word1, word2) << endl;\n return 0;\n}\n", "explain": "Explanation:\n\nThe main loop runs while both strings have characters to merge.\nAfter exiting the loop, if there are any remaining characters in word1 or word2, we use the substr method to append them to the result.\nTime Complexity: O(n), where n is the total length of word1 and word2.", "v": 0 }, { "_id": "66e00f91dc1b019d344e26c9", "title": "Optimal Solution: Using Index and Concatenation Efficiently", "code": "#include \n#include \nusing namespace std;\n\nstring mergeAlternately(string word1, string word2) {\n string merged;\n int i = 0, len1 = word1.size(), len2 = word2.size();\n\n // Using a single loop to merge characters alternately\n while (i < len1 || i < len2) {\n if (i < len1) merged.push_back(word1[i]);\n if (i < len2) merged.push_back(word2[i]);\n ++i;\n }\n\n return merged;\n}\n\nint main() {\n string word1 = \"abcd\", word2 = \"pq\";\n cout << mergeAlternately(word1, word2) << endl;\n return 0;\n}\n", "explain": "Explanation:\n\nIn this approach, the loop runs until both strings are fully processed.\nWe append characters from word1 and word2 as long as there are characters left in either string.\nThis solution avoids checking string lengths multiple times.\nTime Complexity: O(n), where n is the total length of word1 and word2.", "__v": 0 } ], "tag": "leet75",

"companies": [ { "_id": "66e00cd4dc1b019d344e2696", "words": "Amazon ", "isComp": true, "v": 0 }, { "_id": "66e00ce5dc1b019d344e269e", "words": "Microsoft", "isComp": true, "__v": 0 }, { "_id": "66e00cf0dc1b019d344e26a6", "words": "Google", "isComp": true, "v": 0 } ], "__v": 0 }

arjunatapadkar commented 1 month ago

@J-B-Mugundh you should try this

J-B-Mugundh commented 1 month ago

@arjunatapadkar Okay sure.. please assign it to me!

arjunatapadkar commented 1 month ago

inside the page somewhere around code snippets there should be a "try it yourself" or similar button which should redirect to playground page

J-B-Mugundh commented 1 month ago

inside the page somewhere around code snippets there should be a "try it yourself" or similar button which should redirect to playground page

Okay!

arjunatapadkar commented 3 weeks ago

try this @AswaniBolisetti

AswaniBolisetti commented 3 weeks ago

@arjunatapadkar Could you please tell me why the page is in loading state only

dsapage

arjunatapadkar commented 3 weeks ago

Checkout your internet connection It's working fine - Check it at live at codeteria.vercel.app/dsproblem

AswaniBolisetti commented 3 weeks ago

@arjunatapadkar In the live page, it is working but when i run the code and it is showing like that