Wednesday, April 13, 2022

Implement stsStr Leetcode solution

 28. Implement strStr()

Easy

Implement strStr().

Given two strings needle and haystack, return the index of the first occurrence of needle in haystack, or -1 if needle is not part of haystack.

Clarification:

What should we return when needle is an empty string? This is a great question to ask during an interview.

For the purpose of this problem, we will return 0 when needle is an empty string. This is consistent to C's strstr() and Java's indexOf().

 

Example 1:

Input: haystack = "hello", needle = "ll"
Output: 2

Example 2:

Input: haystack = "aaaaa", needle = "bba"
Output: -1

 

Constraints:

  • 1 <= haystack.length, needle.length <= 104
  • haystack and needle consist of only lowercase English characters.

Practice here:

Solution :

class Solution {
public:
    int strStr(string haystack, string needle) {
        int i=0;
        int maxInd = haystack.length()-needle.length();
        while(i<=maxInd)
        {
            if(haystack[i]==needle[0])
            {
                int j=0;
                while(j<needle.length())
                {
                    if(needle[j]!=haystack[j+i])
                        break;
                    j++;
                }
                if(j==needle.length())
                    return i;
                
            }
            
            i++;
        }
        
        return -1;
    }
};

Monday, March 21, 2022

Integer to Roman Leetcode Solution

 12. Integer to Roman Leetcode Solution

Medium

Roman numerals are represented by seven different symbols: IVXLCD and M.

Symbol       Value
I             1
V             5
X             10
L             50
C             100
D             500
M             1000

For example, 2 is written as II in Roman numeral, just two one's added together. 12 is written as XII, which is simply X + II. The number 27 is written as XXVII, which is XX + V + II.

Roman numerals are usually written largest to smallest from left to right. However, the numeral for four is not IIII. Instead, the number four is written as IV. Because the one is before the five we subtract it making four. The same principle applies to the number nine, which is written as IX. There are six instances where subtraction is used:

  • I can be placed before V (5) and X (10) to make 4 and 9. 
  • X can be placed before L (50) and C (100) to make 40 and 90. 
  • C can be placed before D (500) and M (1000) to make 400 and 900.

Given an integer, convert it to a roman numeral.

 

Example 1:

Input: num = 3
Output: "III"
Explanation: 3 is represented as 3 ones.

Example 2:

Input: num = 58
Output: "LVIII"
Explanation: L = 50, V = 5, III = 3.

Example 3:

Input: num = 1994
Output: "MCMXCIV"
Explanation: M = 1000, CM = 900, XC = 90 and IV = 4.

 

Constraints:

  • 1 <= num <= 3999
Practice here :

Solution:

class Solution {
public:
    string intToRoman(int num) {
        string ans ="";
        
        while(num>0)
        {
            
        if(num>=1000)
        {
            ans+='M';
            num-=1000;
        }else if(num>=900)
        {
            ans+="CM";
            num-=900;
        }else if(num>=500)
        {
            ans+='D';
            num-=500;
        }else if(num>=400)
        {
            ans+="CD";
            num-=400;
        }else if(num>=100)
        {
            ans+='C';
            num-=100;
        }
            else if(num>=90)
        {
            ans+="XC";
            num-=90;
        }
            else if(num>=50)
        {
            ans+='L';
            num-=50;
        }
            else if(num>=40)
        {
            ans+="XL";
            num-=40;
        }
            else if(num>=10)
        {
            ans+='X';
            num-=10;
        }else if(num>=9)
        {
            ans+="IX";
            num-=9;
        }
             else if(num>=5)
        {
            ans+='V';
            num-=5;
        }else if(num>=4)
        {
            ans+="IV";
            num-=4;
        }
            else if(num>=1)
        {
            ans+='I';
            num-=1;
        }
            
        }
        return ans;
    }
};

Leetcode 38 Count and say solution

38. Count and Say

Medium

The count-and-say sequence is a sequence of digit strings defined by the recursive formula:

  • countAndSay(1) = "1"
  • countAndSay(n) is the way you would "say" the digit string from countAndSay(n-1), which is then converted into a different digit string.

To determine how you "say" a digit string, split it into the minimal number of groups so that each group is a contiguous section all of the same character. Then for each group, say the number of characters, then say the character. To convert the saying into a digit string, replace the counts with a number and concatenate every saying.

For example, the saying and conversion for digit string "3322251":

Given a positive integer n, return the nth term of the count-and-say sequence.

 

Example 1:

Input: n = 1
Output: "1"
Explanation: This is the base case.

Example 2:

Input: n = 4
Output: "1211"
Explanation:
countAndSay(1) = "1"
countAndSay(2) = say "1" = one 1 = "11"
countAndSay(3) = say "11" = two 1's = "21"
countAndSay(4) = say "21" = one 2 + one 1 = "12" + "11" = "1211"

 

Constraints:

  • 1 <= n <= 30

Practice Here :

(9) Count and Say - LeetCode

Solution :

#include <bits/stdc++.h>

class Solution {

public:

    string say(string n)

    {   string ans="";

        int ct =1;

        char c = n[0];

        for(int i=1;i<n.length();i++)

        {

            if(n[i]!=c)

            {

               ans+=(ct+'0');

                ans+=(c);

                ct =1;

                c = n[i];

            }else {

                ct++;

            }

        }

     ans+=(ct+'0');

        ans+=(c);

     return ans;

    }

    string countAndSay(int n) {

       vector<string> v;

        v.push_back("0");

        v.push_back("1");

        for(int i=2;i<=n;i++)

        {  

            v.push_back(say(v[i-1]));

        }

        return v[n];

    }

};

    Leetcode pow(x,n) solution

     50. Pow(x, n)

    Medium

    Implement pow(x, n), which calculates x raised to the power n (i.e., xn).

     

    Example 1:

    Input: x = 2.00000, n = 10
    Output: 1024.00000
    

    Example 2:

    Input: x = 2.10000, n = 3
    Output: 9.26100
    

    Example 3:

    Input: x = 2.00000, n = -2
    Output: 0.25000
    Explanation: 2-2 = 1/22 = 1/4 = 0.25
    

     

    Constraints:

    • -100.0 < x < 100.0
    • -231 <= n <= 231-1
    • -104 <= xn <= 104
    Practice Here :

    Solution :

    class Solution {
    public:
        double myPow2(double x,long long int n) {
            if(n==0||x==1)
                return 1;
            else if(n==1)
                return x;
            else if(n<0)
            {
                return 1/myPow2(x,n/-1);
            }
            else if(n%2==0)
                return myPow2(x*x,n/2);
            else return myPow2(x*x,n/2)*x;
        }
        double myPow(double x, int n) {
            return myPow2(x,n);
        }
    };

    Implement stsStr Leetcode solution

      28.   Implement strStr() Easy Implement  strStr() . Given two strings  needle  and  haystack , return the index of the first occurrence of...