SkylineWebZ

Regular Expression Matching In C,CPP,JAVA,PYTHON,C#,JS

Problem Statement: Given a string s and a pattern p, implement a regular expression matching algorithm to determine if s matches p. The pattern may include the characters: The matching should cover the entire string (not just a substring). Example: Input: Output: Explanation: The pattern a* matches zero or more a‘s. In this case, the pattern a* matches the string “aa”. Input: Output: Explanation: The pattern doesn’t match the string. Approach: This problem can be efficiently solved using Dynamic Programming (DP). The idea is to build a 2D DP table where each entry dp[i][j] represents whether the substring s[0..i-1] matches the pattern p[0..j-1]. Time Complexity: Space Complexity: Algorithm: Code Implementation: 1. C #include <stdio.h>#include <stdbool.h>#include <string.h>bool isMatch(char* s, char* p) { int m = strlen(s), n = strlen(p); bool dp[m+1][n+1]; dp[0][0] = true; for (int j = 1; j <= n; j++) { dp[0][j] = (p[j-1] == ‘*’ && dp[0][j-2]); } for (int i = 1; i <= m; i++) { for (int j = 1; j <= n; j++) { if (p[j-1] == s[i-1] || p[j-1] == ‘.’) { dp[i][j] = dp[i-1][j-1]; } else if (p[j-1] == ‘*’) { dp[i][j] = dp[i][j-2] || ((s[i-1] == p[j-2] || p[j-2] == ‘.’) && dp[i-1][j]); } else { dp[i][j] = false; } } } return dp[m][n];}int main() { char s[] = “aa”; char p[] = “a*”; printf(“%s\n”, isMatch(s, p) ? “True” : “False”); return 0;} 2. C++ #include <iostream>#include <vector>#include <cstring>using namespace std;bool isMatch(string s, string p) { int m = s.size(), n = p.size(); vector<vector<bool>> dp(m+1, vector<bool>(n+1, false)); dp[0][0] = true; for (int j = 1; j <= n; j++) { if (p[j-1] == ‘*’) { dp[0][j] = dp[0][j-2]; } } for (int i = 1; i <= m; i++) { for (int j = 1; j <= n; j++) { if (p[j-1] == s[i-1] || p[j-1] == ‘.’) { dp[i][j] = dp[i-1][j-1]; } else if (p[j-1] == ‘*’) { dp[i][j] = dp[i][j-2] || (dp[i-1][j] && (s[i-1] == p[j-2] || p[j-2] == ‘.’)); } } } return dp[m][n];}int main() { string s = “aa”; string p = “a*”; cout << (isMatch(s, p) ? “True” : “False”) << endl; return 0;} 3. Java public class Solution { public boolean isMatch(String s, String p) { int m = s.length(), n = p.length(); boolean[][] dp = new boolean[m + 1][n + 1]; dp[0][0] = true; for (int j = 1; j <= n; j++) { if (p.charAt(j – 1) == ‘*’) { dp[0][j] = dp[0][j – 2]; } } for (int i = 1; i <= m; i++) { for (int j = 1; j <= n; j++) { if (p.charAt(j – 1) == s.charAt(i – 1) || p.charAt(j – 1) == ‘.’) { dp[i][j] = dp[i – 1][j – 1]; } else if (p.charAt(j – 1) == ‘*’) { dp[i][j] = dp[i][j – 2] || (dp[i – 1][j] && (s.charAt(i – 1) == p.charAt(j – 2) || p.charAt(j – 2) == ‘.’)); } } } return dp[m][n]; } public static void main(String[] args) { Solution solution = new Solution(); System.out.println(solution.isMatch(“aa”, “a*”)); // Output: true }} 4. Python def isMatch(s: str, p: str) -> bool: m, n = len(s), len(p) dp = [[False] * (n + 1) for _ in range(m + 1)] dp[0][0] = True for j in range(1, n + 1): if p[j – 1] == ‘*’: dp[0][j] = dp[0][j – 2] for i in range(1, m + 1): for j in range(1, n + 1): if p[j – 1] == s[i – 1] or p[j – 1] == ‘.’: dp[i][j] = dp[i – 1][j – 1] elif p[j – 1] == ‘*’: dp[i][j] = dp[i][j – 2] or (dp[i – 1][j] and (s[i – 1] == p[j – 2] or p[j – 2] == ‘.’)) return dp[m][n]# Test the functionprint(isMatch(“aa”, “a*”)) # Output: True 5. C# using System;public class Solution { public bool IsMatch(string s, string p) { int m = s.Length, n = p.Length; bool[,] dp = new bool[m + 1, n + 1]; dp[0, 0] = true; for (int j = 1; j <= n; j++) { if (p[j – 1] == ‘*’) { dp[0, j] = dp[0, j – 2]; } } for (int i = 1; i <= m; i++) { for (int j = 1; j <= n; j++) { if (p[j – 1] == s[i – 1] || p[j – 1] == ‘.’) { dp[i, j] = dp[i – 1, j – 1]; } else if (p[j – 1] == ‘*’) { dp[i, j] = dp[i, j – 2] || (dp[i – 1, j] && (s[i – 1] == p[j – 2] || p[j – 2] == ‘.’)); } } } return dp[m, n]; } public static void Main() { Solution solution = new Solution(); Console.WriteLine(solution.IsMatch(“aa”, “a*”)); // Output: True }} 6. JavaScript var isMatch = function(s, p) { const m = s.length, n = p.length; const dp = Array.from(Array(m + 1), () => Array(n + 1).fill(false)); dp[0][0] = true; for (let j = 1; j <= n; j++) { if (p[j – 1] === ‘*’) { dp[0][j] = dp[0][j – 2]; } } for (let i = 1; i <= m; i++) { for (let j = 1; j <= n; j++) { if (p[j – 1] === s[i – 1] || p[j – 1] === ‘.’) { dp[i][j] = dp[i – 1][j – 1]; } else if (p[j – 1] === ‘*’) { dp[i][j] = dp[i][j – 2] || (dp[i – 1][j] && (s[i – 1] === p[j – 2] || p[j – 2] === ‘.’)); } } } return dp[m][n];};// Test the functionconsole.log(isMatch(“aa”, “a*”)); // Output: true Conclusion: This algorithm efficiently solves the problem of regular expression matching using Dynamic Programming (DP). The DP approach ensures that we check all possible ways a substring of s can match the pattern p. The time and space complexities are O(m * n), where m and n are the lengths of the string and the pattern, respectively.

Regular Expression Matching In C,CPP,JAVA,PYTHON,C#,JS Read More »

Longest Palindromic Substring In C,CPP,JAVA,PYTHON,C#,JS

Problem Statement: Given a string s, find the longest palindromic substring in s. A palindrome is a string that reads the same forwards and backwards. You need to return the longest palindromic substring. Example: Example 1: Input:s = “babad” Output:”bab”(Note: “aba” is also a valid answer.) Example 2: Input:s = “cbbd” Output:”bb” Approach: We can solve this problem using Dynamic Programming (DP). The idea is to build a 2D table dp, where each entry dp[i][j] represents whether the substring s[i…j] is a palindrome. If dp[i][j] is True, it means that the substring s[i…j] is a palindrome. The transitions are as follows: Time Complexity: Algorithm: Code Implementation in Different Languages: 1. C: #include <stdio.h>#include <string.h>char* longestPalindrome(char* s) { int n = strlen(s); if (n == 0) return “”; int dp[n][n]; memset(dp, 0, sizeof(dp)); int start = 0, maxLength = 1; for (int i = 0; i < n; i++) { dp[i][i] = 1; } for (int length = 2; length <= n; length++) { for (int i = 0; i < n – length + 1; i++) { int j = i + length – 1; if (s[i] == s[j] && (length == 2 || dp[i + 1][j – 1])) { dp[i][j] = 1; if (length > maxLength) { start = i; maxLength = length; } } } } char* result = (char*)malloc((maxLength + 1) * sizeof(char)); strncpy(result, s + start, maxLength); result[maxLength] = ‘\0’; return result;}int main() { char s[] = “babad”; printf(“Longest Palindromic Substring: %s\n”, longestPalindrome(s)); return 0;} 2. C++: #include <iostream>#include <vector>#include <cstring>using namespace std;string longestPalindrome(string s) { int n = s.length(); if (n == 0) return “”; vector<vector<bool>> dp(n, vector<bool>(n, false)); int start = 0, maxLength = 1; for (int i = 0; i < n; i++) { dp[i][i] = true; } for (int length = 2; length <= n; length++) { for (int i = 0; i < n – length + 1; i++) { int j = i + length – 1; if (s[i] == s[j] && (length == 2 || dp[i + 1][j – 1])) { dp[i][j] = true; if (length > maxLength) { start = i; maxLength = length; } } } } return s.substr(start, maxLength);}int main() { string s = “babad”; cout << “Longest Palindromic Substring: ” << longestPalindrome(s) << endl; return 0;} 3. Java: public class Solution { public String longestPalindrome(String s) { int n = s.length(); if (n == 0) return “”; boolean[][] dp = new boolean[n][n]; int start = 0, maxLength = 1; for (int i = 0; i < n; i++) { dp[i][i] = true; } for (int length = 2; length <= n; length++) { for (int i = 0; i < n – length + 1; i++) { int j = i + length – 1; if (s.charAt(i) == s.charAt(j) && (length == 2 || dp[i + 1][j – 1])) { dp[i][j] = true; if (length > maxLength) { start = i; maxLength = length; } } } } return s.substring(start, start + maxLength); } public static void main(String[] args) { Solution sol = new Solution(); String s = “babad”; System.out.println(“Longest Palindromic Substring: ” + sol.longestPalindrome(s)); }} 4. Python: def longestPalindrome(s: str) -> str: n = len(s) if n == 0: return “” dp = [[False] * n for _ in range(n)] start, maxLength = 0, 1 for i in range(n): dp[i][i] = True for length in range(2, n + 1): for i in range(n – length + 1): j = i + length – 1 if s[i] == s[j] and (length == 2 or dp[i + 1][j – 1]): dp[i][j] = True if length > maxLength: start = i maxLength = length return s[start:start + maxLength]# Tests = “babad”print(“Longest Palindromic Substring:”, longestPalindrome(s)) 5. C#: using System;public class Solution { public string LongestPalindrome(string s) { int n = s.Length; if (n == 0) return “”; bool[,] dp = new bool[n, n]; int start = 0, maxLength = 1; for (int i = 0; i < n; i++) { dp[i, i] = true; } for (int length = 2; length <= n; length++) { for (int i = 0; i < n – length + 1; i++) { int j = i + length – 1; if (s[i] == s[j] && (length == 2 || dp[i + 1, j – 1])) { dp[i, j] = true; if (length > maxLength) { start = i; maxLength = length; } } } } return s.Substring(start, maxLength); } public static void Main(string[] args) { Solution sol = new Solution(); string s = “babad”; Console.WriteLine(“Longest Palindromic Substring: ” + sol.LongestPalindrome(s)); }} 6. JavaScript: var longestPalindrome = function(s) { const n = s.length; if (n === 0) return “”; const dp = Array.from({ length: n }, () => Array(n).fill(false)); let start = 0, maxLength = 1; for (let i = 0; i < n; i++) { dp[i][i] = true; } for (let length = 2; length <= n; length++) { for (let i = 0; i < n – length + 1; i++) { const j = i + length – 1; if (s[i] === s[j] && (length === 2 || dp[i + 1][j – 1])) { dp[i][j] = true; if (length > maxLength) { start = i; maxLength = length; } } } } return s.substring(start, start + maxLength);};// Testlet s = “babad”;console.log(“Longest Palindromic Substring:”, longestPalindrome(s)); Summary:

Longest Palindromic Substring In C,CPP,JAVA,PYTHON,C#,JS Read More »

SEO Techniques | Introduction, Tips and How It helps to Increase Traffic and Ranking?

A decent website does not ensure visits. Given millions of websites available, how can you make sure yours is unique? This is where Search Engine Optimization, or SEO, finds application. Like a guidebook for your website, SEO shows search engines like Google or Bing what your site is about and why it’s important. Following this approach can help your website show up higher in search results, increasing the likelihood of click-through. SEO is applying clever techniques to make your website understandable and easily found. It’s sort of like getting your house ready for visitors by ensuring the rooms are friendly and the address is clear. Usually, people click on the first few links that show up while searching for anything on the internet. Therefore, you are probably going to obtain more visitors the higher your website ranks show in those search results.Describe SEO methods. Strategies and methods applied to raise a website’s exposure and rating on search engine results pages are SEO (Search Engine Optimization) tactics. These strategies seek to make a website more appealing and relevant to search engines like Google, Bing, or Yahoo, therefore raising the possibility of it showing higher in search results. SEO methods mostly aim to simplify the information and purpose of a website so that search engines may grasp it. SEO helps search engines identify the relevance and quality of a website by optimizing many factors including keywords, content, website structure, and links, therefore enhancing its rating. Improve for clicks as well as rankings: informative material: Write something for your readers that offers worthwhile knowledge. This guarantees that readers find your material helpful in addition to improving SEO.Enterprising Design: Employ an aesthetically pleasing and intuitive layout. This covers simple typefaces, headings clearly, and multimedia components including photographs and videos. Make sure your website is navigable intuitively. Moving from one page to another should come naturally for users, therefore encouraging longer dwell times and lowering bounce rates. Look for excellent material by means of competitor analysis: List the major rivals in your sector.Examine their content strategy, areas of coverage, and most engaging kinds of material they use.Create material that not only fits but also exceeds what your rivals are providing to improve and innovate. Add your special viewpoint and analysis. Analyze keywords in terms of gaps: List Competitor Keywords: Examine keywords for which your rivals rank.List possible opportunities. Look for keywords they might be lacking or poorly optimizing.Optimize Content: Write around these found keywords to seize fresh possibilities. Create Excellent Backlinks: Create excellent material others would naturally want to link to.Actively seek for backlinks by contacting pertinent websites.Guest posting on credible websites in your niche will help you. Handle your backlink profile. Superior quality above quantity: Rather of seeking a lot of links, concentrate on obtaining links from credible and relevant websites. Periodically review your backlinks to be sure they come from reliable sources.Use the disavow tool to remove damaging or spammy backlinks as needed. Tune for Core Web Vitals: Page speed will help you to maximize your website’s loading times.Make sure your website seems well on several devices and is responsive.Easy navigation will help to give a flawless browsing experience. Generate material appealing to people. Research keywords: Know the ones your readers are referencing.Trending Topics: Keep current on sector developments and produce material around them.Address issues of pain points: Address issues or provide answers for inquiries your readers often have. Both internal and outside links: Link pertinent pages of your own website to enhance navigation and enable search engines to grasp your content hierarchy.To give your material legitimacy, link to reputable outside sources. Maintaining the current content of your website: Frequent updates—add fresh material often to let search engines know your site is current.Add evergreen material that stays current across time. Update previous material: Review your earlier content often to find outdated items.Add fresh data, figures, or ideas to keep the material current.Check and adjust headers, meta tags, and other SEO aspects to maximize. How may search engine optimization boost ranking and traffic? Increasing a website’s traffic and raising its search engine rating depend much on SEO strategies. They guide customers and search engines to your website, functioning somewhat as a map. Let’s explore the several ways in which these approaches support one another:Increased Visibility: SEO techniques enhance a website’s visibility in search results, making it more likely for users to click on it. The higher a site appears, the more traffic it tends to receive. Improved Website Understanding: SEO signals to search engines what the website is about, helping them recognize its relevance to users’ searches. By using keywords and relevant content, SEO techniques assist search engines in understanding and categorizing the website correctly. Enhanced User Experience: Implementing SEO techniques improves the overall quality of a website. Optimizing site speed, navigation, and content makes it more user-friendly, encouraging visitors to stay longer and return, ultimately signaling to search engines that the site is valuable and relevant. Quality Content Engagement: SEO encourages the creation of high-quality, informative content. When users find helpful information on a website, they are more likely to engage, spend more time on the site, and possibly share it. Search engines notice this engagement and rank the website higher. Increased Credibility: Employing SEO techniques such as link building from reputable websites signals trust and authority to search engines. When other trusted sites link to your website, it boosts the credibility and trustworthiness of your site, positively impacting its ranking. Local and Global Reach: SEO techniques cater to local searches by ensuring the business is visible in local directories and listings. This expands the website’s reach to both local and global audiences. Constant Improvement: SEO techniques need regular updating to keep up with changes in search engine algorithms and user preferences. By continuously refining and implementing these techniques, a website can maintain and improve its position in search rankings.

SEO Techniques | Introduction, Tips and How It helps to Increase Traffic and Ranking? Read More »

Construct Tree from given Inorder and Preorder traversals

Given pre-order and in-order traversals of a Binary Tree, the challenge is to build the tree and retrieve its root. Table of Content [Naive Approach] Using Pre-order traversal – O(n^2) Time and O(h) Space Pre-order traversal will help one build the tree. Create root node by first element of the pre-order array. In the in-order array, determine this node’s index. Create the left subtree in-order using the components on the left side of the root node. Likewise build the appropriate subtree in-order using the items on the right side of the root node. C++ Java Python C# Output 3 4 1 5 2 0

Construct Tree from given Inorder and Preorder traversals Read More »

Merge two sorted arrays Solutions-Using Merge of MergeSort

Making Use of Merge of MergeSortMerge function of Merge sort is supposed to be used here. Consider two sorted arrays, Array 1 and Array 2, together with an empty array, Array 3. Here “i” pointer refers towards 0th index of Array1, similarly “j” and “k” pointer points towards 0th index of Array2 and Array3 for comparisons. C++ C Java Python JavaScript Output 1 2 3 4 5 6 7 8 Time Complexity : O(n1 + n2) Auxiliary Space : O(n1 + n2)

Merge two sorted arrays Solutions-Using Merge of MergeSort Read More »

What is influencer marketing: An influencer strategy guide for 2025

Influencers are here to remain. More than 80% of marketers believe, according to a Q3 2023 Sprout Pulse Survey, are influencers absolutely vital to their whole social media approach. But the realm of influencer marketing is always changing. Ten years ago, the field of influencer marketing was limited to celebrities and a small group of committed bloggers; today, social media influencers abound on every social network. Though their numbers might differ, these bloggers have great impact. Their close-knit networks promote real connections and impact buying behavior, therefore increasing brand interaction and finally sales. Working with digital artists and influencers, though, calls for a smart and well-prepared strategy. And this book seeks to guide you across it. Read on for advice on how to create a successful influencer marketing plan, what mistakes to stay clear of and how to choose the correct influencers for your brand. Influencer marketing is what? Influencer marketing in social media is the method whereby product mentions from influencers and endorsements are used. These people are seen as experts in their field and have a committed social following. Because of the great trust social influencers have developed over time with their following, influencer marketing works. For the prospective consumers of your brand, recommendations from these influencers act as social evidence. Different kinds of influencers based on reach and sizeAlthough it may sound like a dream come true, working with an influencer with millions of followers could not be the greatest fit for your brand. Certain social media influencers have big, wide followers across many different groups. Others claim lesser but more focused and involved communities. Selecting the appropriate influencers for your company depends on knowing what each kind can provide you in terms of reach, range, cost and involvement. Let’s examine more closely the several kinds of influencers available: Mega or well-known influences Often include well-known actors, artists, athletes, and other prominent personalities, these influencers have a large following—more than a million. Large-scale brand awareness initiatives would find them perfect as their celebrity status lets them enthrall a varied audience. Think: Cristiano Ronaldo. Although working with mega influencers might be somewhat costly, they can provide your brand unmatched exposure. Moreover, while their audience is typically large, their involvement rates could not be as great as those of influencers with smaller, more specialized following. Working with mega influencers could help some companies listed here: Big businesses with the means and finances thatCompanies aiming at a wide audience with diverse traitsLuxury or high-end brands seeking a feeling of uniquenessMacro InfluencersUsually ranging from 100,000 to 1 million, macro-influencers are established personalities within their particular areas. These influencers are now thought leaders in their field since they have developed their reputation over time by consistent material production and interaction. Because their followers typically have similar interests, macro-influencers present a more focused approach than celebrities. Working with macro-influencers will provide your company great exposure, but depending on your budget it may still be somewhat expensive. Following are some brands that might fit macro-influencers: Startups looking for quick reputation, exposure, and growth—like CanvaNonprofit groups aiming at awareness-raising and fundingAirlines and hotels aiming for a particular yet sizable audienceMicro- influencersMicro-influencers are the emerging stars of influencer marketing with 10,000 to 100,000 extremely involved followers. Usually, these influencers are quite visible on particular sites including Instagram, YouTube, and TikHub. Working with micro-influencers appeals to marketers since their unique material, relevant advice, and real-world interactions enthralls a niche, driven audience. They also cost less than more seasoned celebrities. Nano-based influencers Nano-influencers run between 1,000 and 10,000 followers. Thanks to the close-knit community they have created and their personable material, these influencers usually have a great relationship with their audience. Although they have a limited reach, nano-influencers can be great allies for companies looking to target particular groups and demographics without going broke. Actually, compared to 39% in 2023, 44% of companies want to collaborate with nano influencers in 2024 according to the most recent Influencer Marketing Hub statistics.This is for several reasons, mostly as nano-influencers commit more time and effort to personal collaborations since they operate on a tiny scale. This implies more customized material for your business and personal contacts inside specialized groups. They are ideal for companies including: Local companies aiming at certain areas, towns or citiesLimited budget small businesses looking to run affordable campaignsHome-based or specialist food companies catering to a niche market drawn in by their unique offeringsWhy use influencer marketing?For you, influencer marketing can be a very effective marketing technique; as these influencer marketing examples demonstrate, companies are already making use of it. The 2024 Influencer Marketing Report claims that almost half of all customers (49%) buy at least once a month because of influencer posts; nearly all consumers (86%), buy motivated by an influencer at least once a year. Based on the latest data from the Influencer Marketing Hub, the influencer marketing sector is predicted to rise to $24 billion in 2024 rather predictably. Here are some arguments in favor of including influencers into your marketing mix. Raised brand recognition Working with an influencer increases the audience your brand will attract. Influencers expose your business to fresh consumers who would not have known it by including it into their work. Moreover, the confidence that trust-builders hold for their followers improves the legitimacy and reputation of your business and raises market awareness. Correct audience targeting By matching companies to influencers whose followers fit their target market, influencer marketing enables marketers with exact audience targeting. This guarantees that their message finds the correct target and is presented from a viewpoint the audience will find appealing, so strengthening the efficacy of the campaign. Greater conversions After witnessing an influencer campaign, social proof is a major determinant of the chance of purchase since it can persuade uncertain consumers in your advantage. In the same vein, influencers frequently provide interactive materials including contests, challenges, and live events that increase interaction and inspire purchases or deal registration. They also provide unique discount codes or special

What is influencer marketing: An influencer strategy guide for 2025 Read More »

Merge two sorted arrays Solutions

Merging two sorted arrays is the challenge here in a sorted way. Examples:  Input: arr1[] = { 1, 3, 4, 5}, arr2[] = {2, 4, 6, 8} Output: arr3[] = {1, 2, 3, 4, 4, 5, 6, 8} Input: arr1[] = { 5, 8, 9}, arr2[] = {4, 7, 8} Output: arr3[] = {4, 5, 7, 8, 8, 9}  Table of Content The naïve approach to accomplish the same is physical force. Combine all of arr 1’s and arr 2’s components in arr 3. Sort the arr3 then just once more. C++ C Java Python Output 1 2 3 4 5 6 7 8 Time Complexity : O((n1 + n2) log(n1 + n2)) , the whole size of arr3 is m+nAuxiliary Space: O(1)

Merge two sorted arrays Solutions Read More »

Maximum size rectangle binary sub-Using Dynamic Programming

Storing the width of consecutive 1’s ending at every cell (i, j) in a 2D array is the aim. Beginning from every cell (i, j) with value 1, iterate upwards row by row, determine the minimal width of 1’s in the column to guarantee the rectangle stays valid. The minimal width multiplied by the height yields the size of the rectangle, which updates the maximum area found thus far. Initialize a 2D matrix memo of size nm to hold the width of successive 1s ending at every cell (i, j). Starting at 0 Go iteratively across every cell (i, j) in the input matrix. Should the value at (i, j) be 1, use these guidelines: Should j = 0, set memo[i][j] = 1; otherwise, set memo[i][j] = 1 + memo[i][j – 1]. Define width = memo[i][j]. Update width = min(width, memo[k][j] and get the rectangle area width(i – k + 1) for every row k from i to 0. Add to the maximum area discovered thus far.Get back the computed maximum area. C++ Java Python C# JavaScript Output 8

Maximum size rectangle binary sub-Using Dynamic Programming Read More »