Dadas dos strings, encuentre si podemos hacer la primera string a partir de la segunda eliminando algunos caracteres de la segunda y reorganizando los caracteres restantes.
Ejemplos:
Input : s1 = ABHISHEKsinGH : s2 = gfhfBHkooIHnfndSHEKsiAnG Output : Possible Input : s1 = Hello : s2 = dnaKfhelddf Output : Not Possible Input : s1 = GeeksforGeeks : s2 = rteksfoGrdsskGeggehes Output : Possible
Básicamente, necesitamos encontrar si una string contiene caracteres que son un subconjunto de caracteres en la segunda string. Primero contamos las ocurrencias de todos los caracteres en la segunda string. Luego recorremos la primera string y reducimos el conteo de cada carácter que está presente en la primera. Si en algún momento, la cuenta se vuelve menor que 0, devolvemos falso. Si todos los recuentos siguen siendo mayores o iguales a 0, devolvemos verdadero.
Implementación:
C++
// CPP program to find if it is possible to // make a string from characters present in // other string. #include <bits/stdc++.h> using namespace std; const int MAX_CHAR = 256; // Returns true if it is possible to make // s1 from characters present in s2. bool isPossible(string& s1, string& s2) { // Count occurrences of all characters // present in s2.. int count[MAX_CHAR] = { 0 }; for (int i = 0; i < s2.length(); i++) count[s2[i]]++; // For every character, present in s1, // reduce its count if count is more // than 0. for (int i = 0; i < s1.length(); i++) { if (count[s1[i]] == 0) return false; count[s1[i]]--; } return true; } // Driver code int main() { string s1 = "GeeksforGeeks", s2 = "rteksfoGrdsskGeggehes"; if (isPossible(s1, s2)) cout << "Possible"; else cout << "Not Possible\n"; return 0; }
Java
// Java program to find if it is possible to // make a string from characters present in // other string. class StringfromCharacters { static final int MAX_CHAR = 256; // Returns true if it is possible to make // s1 from characters present in s2. static boolean isPossible(String s1, String s2) { // Count occurrences of all characters // present in s2.. int count[] = new int[MAX_CHAR]; for (int i = 0; i < s2.length(); i++) count[(int)s2.charAt(i)]++; // For every character, present in s1, // reduce its count if count is more // than 0. for (int i = 0; i < s1.length(); i++) { if (count[(int)s1.charAt(i)] == 0) return false; count[(int)s1.charAt(i)]--; } return true; } // Driver code public static void main(String args[]) { String s1 = "GeeksforGeeks", s2 = "rteksfoGrdsskGeggehes"; if (isPossible(s1, s2)) System.out.println("Possible"); else System.out.println("Not Possible"); } } /* This code is contributed by Danish Kaleem */
Python 3
# Python 3 program to find if it is possible # to make a string from characters present # in other string. MAX_CHAR = 256 # Returns true if it is possible to make # s1 from characters present in s2. def isPossible(s1, s2): # Count occurrences of all characters # present in s2.. count = [0] * MAX_CHAR for i in range(len(s2)): count[ord(s2[i])] += 1 # For every character, present in s1, # reduce its count if count is more # than 0. for i in range(len(s1)): if (count[ord(s1[i])] == 0): return False count[ord(s1[i])] -= 1 return True # Driver code if __name__ == "__main__": s1 = "GeeksforGeeks" s2 = "rteksfoGrdsskGeggehes" if (isPossible(s1, s2)): print("Possible") else: print("Not Possible") # This code is contributed by ita_c
C#
// C# program to find if it is possible to // make a string from characters present // in other string. using System; class GFG { static int MAX_CHAR = 256; // Returns true if it is possible to // make s1 from characters present // in s2. static bool isPossible(String s1, String s2) { // Count occurrences of all characters // present in s2.. int []count = new int[MAX_CHAR]; for (int i = 0; i < s2.Length; i++) count[(int)s2[i]]++; // For every character, present in s1, // reduce its count if count is more // than 0. for (int i = 0; i < s1.Length; i++) { if (count[(int)s1[i]] == 0) return false; count[(int)s1[i]]--; } return true; } // Driver code public static void Main() { string s1 = "GeeksforGeeks", s2 = "rteksfoGrdsskGeggehes"; if (isPossible(s1, s2)) Console.WriteLine("Possible"); else Console.WriteLine("Not Possible"); } } // This code is contributed by vt_m.
Javascript
<script> // Javascript program to find if it is possible to // make a string from characters present in // other string. let MAX_CHAR = 256; // Returns true if it is possible to make // s1 from characters present in s2. function isPossible(s1, s2) { // Count occurrences of all characters // present in s2.. let count = new Array(MAX_CHAR); for(let i = 0; i < count.length; i++) { count[i] = 0; } for (let i = 0; i < s2.length; i++) count[s2[i].charCodeAt(0)]++; // For every character, present in s1, // reduce its count if count is more // than 0. for (let i = 0; i < s1.length; i++) { if (count[s1[i].charCodeAt(0)] == 0) return false; count[s1[i].charCodeAt(0)]--; } return true; } // Driver code let s1 = "GeeksforGeeks", s2 = "rteksfoGrdsskGeggehes"; if (isPossible(s1, s2)) document.write("Possible"); else document.write("Not Possible"); // This code is contributed by avanitrachhadiya2155 </script>
Possible
La complejidad del tiempo es: O(n)
Este artículo es una contribución de Prabhat kumar singh . Si te gusta GeeksforGeeks y te gustaría contribuir, también puedes escribir un artículo usando write.geeksforgeeks.org o enviar tu artículo por correo a review-team@geeksforgeeks.org. Vea su artículo que aparece en la página principal de GeeksforGeeks y ayude a otros Geeks.
Publicación traducida automáticamente
Artículo escrito por GeeksforGeeks-1 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA