Join Regular Classroom : Visit ClassroomTech

Accenture Coding Question | String Within a String | CodeWindow

String within String

Given two strings, ‘X’ and ‘Y’ (length(X)>=1, length(Y)<=10000), find out if ‘Y’ is contained in ‘X’

Input specification:
Input1: The string ‘X’.
Input2: The string ‘Y’.

Output Specification:
Return “yes” if ‘Y’ is contained in ‘X’ else return no.

Example 1:
Input 1: abac
Input 2: ab

Output: yes
Explanation:
ab is present with abac

Example 2:
Input 1: xyac
Input 2: xz

Output: no
Explanation:
xz is not present within xyac

Solution in Python 3:

#https://codewindow.in
#join our telegram channel

n=input()
m=input()

if m in n:
    print("yes")
    
else:
    print("No")
    
# Telegram @codewindow

Solution in C++ :

//https://codewindow.in
//join our telegram channel @codewindow

#include <bits/stdc++.h>
#include <iostream>
using namespace std;

int isSubstring(string s1, string s2)
{
    int M = s1.length();
    int N = s2.length();

    for (int i = 0; i <= N - M; i++) {
        int j;
        for (j = 0; j < M; j++)
            if (s2[i + j] != s1[j])
                break;
 
        if (j == M)
            return i;
    }
 
    return -1;
}

int main()
{
    string s1;
    string s2;
    cin >> s1;
    cin >> s2;
    int res = isSubstring(s2, s1);
    if (res == -1)
        cout << "No";
    else
        cout << "Yes";
    return 0;
}

Solution in JAVA :

//https://codewindow.in
//join our telegram channel @codewindow

import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
class Codechef
{
    static int isSubstring(String s1, String s2)
    {
        int M = s1.length();
        int N = s2.length();
        for (int i = 0; i <= N - M; i++) {
            int j;
            for (j = 0; j < M; j++)
                if (s2.charAt(i + j) != s1.charAt(j))
                    break;
            if (j == M)
                return i;
        }
        return -1;
    }

    public static void main(String args[]) throws Exception
    {
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        String str1 = br.readLine();
        String str2 = br.readLine();
 
        int res = isSubstring(str2, str1);
        if (res == -1)
            System.out.println("No");
        else
            System.out.println("Yes");
    }
}

Output:

codewindow
wi
yes
codewindow
wo
No
Recent Posts
Pages