Improve Article
Save Article
Improve Article
Save Article
Given an array of strings, nan task is to people “Yes” if it contains a drawstring that is simply a prefix of different drawstring otherwise, people “No”.
Examples:
Input: arr[] = {“rud”, “rudra”, “rahi”}
Output: Yes
Explanation: arr[0] = “rud” is simply a prefix of arr[1] = “rudra”, that’s why “Yes” is nan output.
Input: arr[] = {“baj”, “mnc”, “rahi”, “banjo”}
Output: No
Input: arr[] = {“bahe”, “baj”, “jabf”, “bahej”}
Output: Yes
Approach: To lick nan problem travel nan beneath idea:
The attack fundamentally requires america to lucifer 1 drawstring to another, specified that checking whether 1 contains prefix of another, pinch nan thief of find method which returns nan first position wherever location is simply a prefix match, and successful lawsuit of prefix it would decidedly beryllium zero.
Steps to travel to instrumentality nan approach:
- The “hasPrefix” usability takes an array of strings “arr” and its size “n” arsenic input.
- It past iterates done each drawstring successful nan array, and for each string, it checks if it is simply a prefix of immoderate different drawstring successful nan array utilizing nan “find” method of nan “string” class. If it is, nan usability instantly returns “true”.
- If nary drawstring is recovered to beryllium a prefix of different string, nan usability returns “false”.
- The main usability creates an illustration drawstring array, calls nan “hasPrefix” function, and prints “Yes” if nan usability returns “true”, and “No” otherwise.
Below is nan implementation of nan supra approach:
C++
#include <iostream>
#include <string>
using namespace std;
bool hasPrefix(string arr[], int n)
{
for (int one = 0; one < n; i++) {
for (int j = 0; j < n; j++) {
if (i != j && arr[j].find(arr[i]) == 0) {
return true;
}
}
}
return false;
}
int main()
{
string arr[] = { "rud", "rudra", "rahi" };
int n = sizeof(arr) / sizeof(arr[0]);
if (hasPrefix(arr, n)) {
cout << "Yes" << endl;
}
else {
cout << "No" << endl;
}
return 0;
}
Java
import java.util.Arrays;
public people Main {
public fixed boolean hasPrefix(String[] arr)
{
for (int one = 0; one < arr.length; i++) {
for (int j = 0; j < arr.length; j++) {
if (i != j && arr[j].indexOf(arr[i]) == 0) {
return true;
}
}
}
return false;
}
public fixed void main(String[] args)
{
String[] arr = { "rud", "rudra", "rahi" };
if (hasPrefix(arr)) {
System.out.println("Yes");
}
else {
System.out.println("No");
}
}
}
C#
using System;
public people MainClass {
public fixed bool HasPrefix(string[] arr)
{
for (int one = 0; one < arr.Length; i++) {
for (int j = 0; j < arr.Length; j++) {
if (i != j && arr[j].IndexOf(arr[i]) == 0) {
return true;
}
}
}
return false;
}
public fixed void Main()
{
string[] arr = { "rud", "rudra", "rahi" };
if (HasPrefix(arr)) {
Console.WriteLine("Yes");
}
else {
Console.WriteLine("No");
}
}
}
Time Complexity: O(n^2 * m), wherever n is nan magnitude of nan input array and m is nan maximum magnitude of a drawstring successful nan array. This is because nan codification uses nested loops to comparison each brace of strings successful nan array, and for each pair, it uses nan find method to cheque if 1 drawstring is simply a prefix of nan other. The find method has a clip complexity of O(m).
Auxiliary Space: O(1), arsenic it only uses a fixed magnitude of representation to shop nan input array and a fewer loop variables. The abstraction utilized does not dangle connected nan size of nan input.