Dada una string str y una array de palabras word[] , la tarea es encontrar si str es una string de prefijos de word[] .
Ejemplos:
Entrada: str = “indiaismycountry”,
palabra[] = {“india”, “es”, “mi”, “país”, “y”, “yo”, “amor”, “india”}
Salida: verdadero
Explicación: La string str se puede hacer concatenando «india», «es», «mi» y «país» juntos.Entrada: str = “indianismo”,
palabra[] = {“india”, “es”, “mi”, “país”, “y”, “yo”, “amor”, “india”}
Salida: false
Explicación: Es imposible hacer str usando los prefijos de la array de palabras.
Enfoque: Este es un problema simple relacionado con la implementación. Siga los pasos que se mencionan a continuación:
- Tome una string vacía llamada ans .
- Repita la array de palabras y siga agregando cada elemento de la array de palabras a ans .
- Después de agregar a ans comparándolo con s , si ambos coinciden, simplemente devuelven verdadero; de lo contrario, continúa.
- Si la iteración finaliza y ans no coincide con la s , devuelve falso.
A continuación se muestra el programa C++ para implementar el enfoque anterior:
C++
// C++ program to implement the // given approach #include <bits/stdc++.h> using namespace std; // Function to check whether string // is prefix bool isPrefixString(string s, vector<string>& word) { // ans is taken as an empty string string ans = ""; // N is used to store // the size of word array int N = word.size(); // Iterating over the word array for (int i = 0; i < N; i++) { // Adding element by element // of the array ans += word[i]; // If ans and str are same // return true if (ans == s) return true; } // As iteration is ending which means // string is not prefix so return false. return false; } // Driver code int main() { string str = "indiaismycountry"; vector<string> word = { "india", "is", "my", "country", "and", "i", "love", "india" }; bool ans = isPrefixString(str, word); if (ans) cout << "True"; else cout << "False"; return 0; }
Java
// Java program for the above approach import java.io.*; import java.lang.*; import java.util.*; class GFG { // Function to check whether string // is prefix static Boolean isPrefixString(String s, String word[]) { // ans is taken as an empty string String ans = ""; // N is used to store // the size of word array int N = word.length; // Iterating over the word array for (int i = 0; i < N; i++) { // Adding element by element // of the array ans += word[i]; // If ans and str are same // return true if (ans.equals(s)) return true; } // As iteration is ending which means // string is not prefix so return false. return false; } // Driver code public static void main (String[] args) { String str = "indiaismycountry"; String word[] = { "india", "is", "my", "country", "and", "i", "love", "india" }; Boolean ans = isPrefixString(str, word); if (ans) System.out.println("True"); else System.out.println("False"); } } // This code is contributed by hrithikgarg03188.
Python
# Pyhton program to implement the # given approach # Function to check whether string # is prefix def isPrefixString(s, word): # ans is taken as an empty string ans = "" # N is used to store # the size of word array N = len(word) # Iterating over the word array for i in range(0, N): # Adding element by element # of the array ans = ans + (word[i]) # If ans and str are same # return true if (ans == s): return True # As iteration is ending which means # string is not prefix so return false. return False # Driver code str = "indiaismycountry" word = ["india", "is", "my", "country", "and", "i", "love", "india"] ans = isPrefixString(str, word) if (ans == True): print("True") else: print("False") # This code is contributed by Samim Hossain Mondal.
C#
// C# program for the above approach using System; class GFG { // Function to check whether string // is prefix static bool isPrefixString(string s, string []word) { // ans is taken as an empty string string ans = ""; // N is used to store // the size of word array int N = word.Length; // Iterating over the word array for (int i = 0; i < N; i++) { // Adding element by element // of the array ans += word[i]; // If ans and str are same // return true if (ans.Equals(s)) return true; } // As iteration is ending which means // string is not prefix so return false. return false; } // Driver code public static void Main () { string str = "indiaismycountry"; string []word = { "india", "is", "my", "country", "and", "i", "love", "india" }; bool ans = isPrefixString(str, word); if (ans) Console.WriteLine("True"); else Console.WriteLine("False"); } } // This code is contributed by Samim Hossain Mondal.
Javascript
<script> // JavaScript code for the above approach // Function to check whether string // is prefix function isPrefixString(s, word) { // ans is taken as an empty string let ans = ""; // N is used to store // the size of word array let N = word.length; // Iterating over the word array for (let i = 0; i < N; i++) { // Adding element by element // of the array ans += word[i]; // If ans and str are same // return true if (ans == s) return true; } // As iteration is ending which means // string is not prefix so return false. return false; } // Driver code let str = "indiaismycountry"; let word = ["india", "is", "my", "country", "and", "i", "love", "india"]; let ans = isPrefixString(str, word); if (ans) document.write("True"); else document.write("False"); // This code is contributed by Potta Lokesh </script>
True
Complejidad de tiempo: O(N), N es el tamaño de la array de palabras.
Complejidad espacial: O(M) donde M es la longitud de str