Esempio di intersezione di contatore e dizionario Python (crea una stringa usando delete e swap)

Esempi:

 Input: s1 = ABHISHEKsinGH: s2 = gfhfBHkooIHnfndSHEKsiAnG Output: Possibile Input: s1 = Hello: s2 = dnaKfhelddf Output: Non possibile Input: s1 = GeekfogforGeeks: s1 = GeeksforGeeks: s2 = rtehes OutputGeks / pre> 

Abbiamo una soluzione a questo problema, fare riferimento a Crea una stringa da un’altra rimuovendo e riordinando i caratteri . Risolveremo rapidamente questo problema in Python. L’approccio è molto semplice,

  1. Converti entrambe le stringhe in un dizionario usando intersezione due dizionari e confronta l’output risultante con il dizionario di la prima riga, se sono entrambe uguali, significa che altrimenti puoi convertire la stringa.

# Codice Python per trovare se possiamo creare la prima riga
# dal secondo, rimuovendo alcuni caratteri da
# Secondo e riordina i caratteri rimanenti.

da raccolte import Contatore

def makeString ( str1, str2):

# converte entrambe le righe in dizionari

# l’output sarà simile a str1 = & quot; aabbcc & quot ;,

# dict1 = {& # 39 ; a & # 39 ;: 2, & # 39; b & # 39 ;: 2, & # 39; c & # 39 ;: 2}

# str2 = & # 39; abbbcc & # 39 ;, dict2 = {& # 39; a & # 39 ;: 1, & # 39; b & # 39 ;: 3, & # 39; c & # 39 ;: 2}

dict1 = Counter (str1)

dict2 = Counter (str2)

# prendi l’intersezione di due dizionari

# l’output sarà il risultato = {& # 39; a & # 39 ;: 1, & # 39; b & # 39 ;: 2, & # 39; c & # 39 ;: 2}

risultato = dict1 & amp; dict2

# confronta il dizionario risultante con il primo

# confronta il dizionario prima confronta le chiavi

# e poi confronta i rispettivi valori ​​

ritorno risultato = = dict1


# Programma driver

if __ nome__ = = "__ main__" :

str1 = ’ABHISHEKsinGH’

str2 = ’ gfhfBHkooIHnfndSHEKsiAnG’

< p> if (makeString (str1, str2) = = Vero ):

print ( "Possibile" )

else :

stampa ( "Non possibile" )

Uscita:

 Possibile 

Ci auguriamo che questo articolo ti abbia aiutato a risolvere il problema. Oltre a Esempio di intersezione di contatore e dizionario Python (crea una stringa usando delete e swap), controlla altri argomenti relativi a Python functions.

Vuoi eccellere in Python? Consulta la nostra recensione dei migliori corsi online Python 2023. Se sei interessato alla scienza dei dati, controlla anche come imparare a programmare in R.

A proposito, questo materiale è disponibile anche in altre lingue:



Julia Schteiner

London | 2023-03-22

Grazie per le spiegazioni! Sono rimasto bloccato con Esempio di intersezione di contatore e dizionario Python (crea una stringa usando delete e swap) per alcune ore, finalmente l'ho fatto 🤗. Non sono proprio sicuro che sia il metodo migliore

Cornwall Lehnman

Prague | 2023-03-22

Forse ci sono altre risposte? Cosa significa esattamente Esempio di intersezione di contatore e dizionario Python (crea una stringa usando delete e swap) significa esattamente?. Tornerò domani con un feedback

Anna Zelotti

Milan | 2023-03-22

Forse ci sono altre risposte? Cosa significa esattamente Esempio di intersezione di contatore e dizionario Python (crea una stringa usando delete e swap) significa esattamente?. Non sono proprio sicuro che sia il metodo migliore

Shop

Gifts for programmers

Learn programming in R: courses

$FREE
Gifts for programmers

Best Python online courses for 2022

$FREE
Gifts for programmers

Best laptop for Fortnite

$399+
Gifts for programmers

Best laptop for Excel

$
Gifts for programmers

Best laptop for Solidworks

$399+
Gifts for programmers

Best laptop for Roblox

$399+
Gifts for programmers

Best computer for crypto mining

$499+
Gifts for programmers

Best laptop for Sims 4

$

Latest questions

PythonStackOverflow

Common xlabel/ylabel for matplotlib subplots

1947 answers

PythonStackOverflow

Check if one list is a subset of another in Python

1173 answers

PythonStackOverflow

How to specify multiple return types using type-hints

1002 answers

PythonStackOverflow

Printing words vertically in Python

909 answers

PythonStackOverflow

Python Extract words from a given string

798 answers

PythonStackOverflow

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

606 answers

PythonStackOverflow

Python os.path.join () method

384 answers

PythonStackOverflow

Flake8: Ignore specific warning for entire file

360 answers

News


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

How to specify multiple return types using type-hints

Printing words vertically in Python

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically