Wednesday, February 22, 2023
HomeSoftware DevelopmentVerify if Prefix String exists within the Array

Verify if Prefix String exists within the Array


Given an array of strings, the duty is to print “Sure” if it accommodates a string that could be a prefix of one other string in any other case, print “No”.

Examples:

Enter: arr[] = {“rud”, “rudra”, “rahi”}
Output: Sure
Rationalization: arr[0] = “rud” is a prefix of arr[1] = “rudra”, that’s why “Sure” is the output.

Enter: arr[] = {“baj”, “mnc”, “rahi”, “banjo”}
Output: No

Enter: arr[] = {“bahe”, “baj”, “jabf”, “bahej”}
Output: Sure

Method: To resolve the issue comply with the beneath concept:

The method principally requires us to match one string to a different, such that checking whether or not one accommodates prefix of one other, with the assistance of discover technique which returns the primary place the place there’s a prefix match, and in case of prefix it might positively be zero.

Steps to comply with to implement the method:

  • The “hasPrefix” perform takes an array of strings “arr” and its dimension “n” as enter.
  • It then iterates via every string within the array, and for every string, it checks if it’s a prefix of another string within the array utilizing the “discover” technique of the “string” class. Whether it is, the perform instantly returns “true”.
  • If no string is discovered to be a prefix of one other string, the perform returns “false”.
  • The principle perform creates an instance string array, calls the “hasPrefix” perform, and prints “Sure” if the perform returns “true”, and “No” in any other case.

Beneath is the implementation of the above method:

C++

#embody <iostream>

#embody <string>

  

utilizing namespace std;

  

bool hasPrefix(string arr[], int n)

{

    for (int i = 0; i < n; i++) {

        for (int j = 0; j < n; j++) {

  

            

            

            

            if (i != j && arr[j].discover(arr[i]) == 0) {

                return true;

            }

        }

    }

    return false;

}

  

int fundamental()

{

    string arr[] = { "rud", "rudra", "rahi" };

    int n = sizeof(arr) / sizeof(arr[0]);

  

    if (hasPrefix(arr, n)) {

        cout << "Sure" << endl;

    }

    else {

        cout << "No" << endl;

    }

  

    return 0;

}

Java

import java.util.Arrays;

  

public class Essential {

    public static boolean hasPrefix(String[] arr)

    {

        for (int i = 0; i < arr.size; i++) {

            for (int j = 0; j < arr.size; j++) {

                

                

                if (i != j && arr[j].indexOf(arr[i]) == 0) {

                    return true;

                }

            }

        }

        return false;

    }

  

    

    public static void fundamental(String[] args)

    {

        String[] arr = { "rud", "rudra", "rahi" };

  

        if (hasPrefix(arr)) {

            System.out.println("Sure");

        }

        else {

            System.out.println("No");

        }

    }

}

C#

utilizing System;

  

public class MainClass {

    public static bool HasPrefix(string[] arr)

    {

        for (int i = 0; i < arr.Size; i++) {

            for (int j = 0; j < arr.Size; j++) {

                

                

                if (i != j && arr[j].IndexOf(arr[i]) == 0) {

                    return true;

                }

            }

        }

        return false;

    }

  

    

    public static void Essential()

    {

        string[] arr = { "rud", "rudra", "rahi" };

  

        if (HasPrefix(arr)) {

            Console.WriteLine("Sure");

        }

        else {

            Console.WriteLine("No");

        }

    }

}

Time Complexity: O(n^2 * m), the place n is the size of the enter array and m is the utmost size of a string within the array. It’s because the code makes use of nested loops to match each pair of strings within the array, and for every pair, it makes use of the discover technique to test if one string is a prefix of the opposite. The discover technique has a time complexity of O(m).
Auxiliary House: O(1), because it solely makes use of a hard and fast quantity of reminiscence to retailer the enter array and some loop variables. The area used doesn’t depend upon the dimensions of the enter.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments