Diberikan sebuah string, kita perlu menemukan karakter berulang pertama dalam string, kita perlu menemukan karakter yang muncul lebih dari satu kali dan yang indeks kemunculan pertamanya paling sedikit dengan pemrograman Python Show
Jika tidak ada karakter yang berulang, cetak -1 Saya harap, Anda mengerti apa yang sebenarnya akan kami lakukan. Jadi mari kita lanjutkan… Metode kekerasanLintasi seluruh string dari awal hingga akhir Untuk setiap karakter periksa apakah berulang atau tidak Jika tidak ada pengulangan cetak karakter -1 Kompleksitas Waktu-O(N^2) str="codespeedy" a=0 for i in range(0 , len(str) ): #traversing through the entire string if a==1: break for j in range(i+1 , len(str)): #traversing characters after the current one if str[i]==str[j]: print(str[i]) a=1 #this character is the first repeating character break if a==0: print(-1) KELUARAN- d
Menggunakan Hashing -Dua lintasan stringGunakan kamus untuk menghitung berapa kali setiap karakter muncul dalam string – kuncinya adalah karakter dan nilainya adalah frekuensi
Pada traversal kedua, untuk setiap karakter periksa apakah berulang atau tidak dengan mencentang dict[str[i]] Jika kami menemukan karakter berulang pertama, kami keluar dari loop Kompleksitas Waktu-O(N) Di bawah ini adalah kode Python yang mengimplementasikan metode ini untuk tugas kita str="codespeedy" dict={} n=len(str) for i in range(0 , n): if str[i] in dict: dict[str[i]]+=1; else: dict[str[i]]=1 a=0 for i in range(0 , n): if dict[str[i]]>1: print(str[i]) a=1 break if a==0: print(-1)_ KELUARAN d
Menggunakan Hashing -Satu traversal dari stringMulailah dengan menginisialisasi ans ke len(str)+1, yang akan digunakan untuk menyimpan indeks dari karakter berulang pertama Dalam kalimat tertentu mungkin ada kata yang diulang sebelum kalimat berakhir. Dalam program python ini, kita akan menangkap kata yang diulang dalam kalimat. Di bawah ini adalah langkah-langkah logis yang akan kita ikuti untuk mendapatkan hasil ini
Program - Temukan Kata yang DiulangiDalam program di bawah ini kami menggunakan metode penghitung dari paket koleksi untuk menghitung jumlah kata Satu string diberikan. Tugas kita adalah menemukan kata berulang pertama dalam string yang diberikan. Untuk mengimplementasikan masalah ini kami menggunakan Koleksi Python. Dari koleksi tersebut, kita bisa mendapatkan metode Counter() AlgoritmaRepeatedword(n) /* n is the string */ Step 1: first split given string separated by space into words. Step 2: now convert the list of words into a dictionary. Step 3: traverse list of words and check which the first word has frequency >1 Kode contoh# To Find the first repeated word in a string from collections import Counter def repeatedword(n): # first split given string separated by space into words w = n.split(' ') con = Counter(w) for key in w: if con[key]>1: print ("REPEATED WORD IS ::>",key) return # Driver program if __name__ == "__main__": n=input("Enter the String ::") repeatedword(n) KeluaranEnter the String ::We are all peaceful soul and blissful soul and loveful soul happy soul REPEATED WORD IS ::> soul_ sumber pertanyaan. https. // www. geeksforgeeks. org/goldman-sachs-wawancara-pengalaman-set-29-magang/ Direkomendasikan. Silakan selesaikan pada "PRAKTEK" terlebih dahulu, sebelum beralih ke solusi Pendekatan Sederhana. Mulai iterasi dari belakang dan untuk setiap kata baru, simpan di unordered_map. Untuk setiap kata yang muncul lebih dari satu , perbarui ans menjadi kata itu, terakhir balik ans dan cetaklah Penerapan C++
had No Repetition he0 had No Repetition he1 had No Repetition he2 had No Repetition he3 had No Repetition he_4 had No Repetition he5 had No Repetition he6 had No Repetition he7 had No Repetition he8 had No Repetition he9 had No Repetition he5 First repeated word :: had1 First repeated word :: had2 First repeated word :: had3 First repeated word :: had2 First repeated word :: had5 had No Repetition he5 First repeated word :: had7 had No Repetition he5 First repeated word :: had9 had0 had No Repetition he7 had2 had No Repetition he5 had No Repetition he4 had5 had6 had5 had8 had9 had No Repetition he0 had No Repetition he1 had5 had No Repetition he4 had No Repetition he4 had No Repetition he5 had No Repetition he_4 had5 had No Repetition he8 had5 had0 had5 had2 had5 had No Repetition he4 had No Repetition he4 had6 had No Repetition he4 had8 had No Repetition he4 had8 First repeated word :: had1 ________16 Repeatedword(n) /* n is the string */ Step 1: first split given string separated by space into words. Step 2: now convert the list of words into a dictionary. Step 3: traverse list of words and check which the first word has frequency >1_16_______3 had No Repetition he4 First repeated word :: had5 had No Repetition he4 First repeated word :: had7 First repeated word :: had2 First repeated word :: had5 had No Repetition he_4 had5 had No Repetition he8 had No Repetition he5 had No Repetition he8 had No Repetition he5 had No Repetition he5 // Cpp program to find first repeated word in a string 7had No Repetition he4 had6 had No Repetition he4 had8 First repeated word :: had1 ________16 Repeatedword(n) /* n is the string */ Step 1: first split given string separated by space into words. Step 2: now convert the list of words into a dictionary. Step 3: traverse list of words and check which the first word has frequency >1_16_______3
had No Repetition he5 had8 #include<bits/stdc++.h> 8First repeated word :: had2 had No Repetition he1 had No Repetition he5 had No Repetition he4 had5 using 4had5 using 6had5 using 8using 9First repeated word :: had5 had No Repetition he5 had No Repetition he8 had No Repetition he5 had2 had No Repetition he5 had No Repetition he06 had No Repetition he07 First repeated word :: had5 had No Repetition he_8 had No Repetition he7 had No Repetition he11 had No Repetition he_4 had No Repetition he5 had No Repetition he14 had No Repetition he15 First repeated word :: had5 had No Repetition he5 had No Repetition he18 had No Repetition he19 First repeated word :: had5 had No Repetition he5 had No Repetition he22 had No Repetition he23 First repeated word :: had5 had No Repetition he5 had No Repetition he26 had No Repetition he5 had No Repetition he28 had No Repetition he5 had No Repetition he30 had No Repetition he5 had No Repetition he5 had No Repetition he5 had No Repetition he34 had No Repetition he35 had No Repetition he5 had No Repetition he_8 Jawahad No Repetition he38 had No Repetition he39
had No Repetition he40 had No Repetition he41 had No Repetition he42 had No Repetition he43 had No Repetition he44 had No Repetition he43 had No Repetition he40 had No Repetition he47 had No Repetition he2 had No Repetition he49 had No Repetition he43 had No Repetition he4 had No Repetition he5 had No Repetition he53 had No Repetition he54 had No Repetition he55 had No Repetition he56 had No Repetition he57 had No Repetition he5 had No Repetition he59 had No Repetition he60 had No Repetition he5 had No Repetition he62 had No Repetition he5 had No Repetition he64 First repeated word :: had2 First repeated word :: had5 had No Repetition he5 had No Repetition he68 First repeated word :: had2 First repeated word :: had5 had No Repetition he5 had No Repetition he72 had No Repetition he5 had No Repetition he74 had No Repetition he5 First repeated word :: had9 had0 had No Repetition he7 had No Repetition he79 had No Repetition he80 had No Repetition he81 had No Repetition he82 had No Repetition he83 had No Repetition he54 had No Repetition he85 had No Repetition he54 had No Repetition he87 had No Repetition he54 had8 had No Repetition he90 had No Repetition he0________5______92 had5 had No Repetition he_94 had No Repetition he54 had No Repetition he8 had No Repetition he54 had No Repetition he98 had No Repetition he54 First repeated word :: had00 had No Repetition he54 had2 had No Repetition he4 had5 had8 First repeated word :: had06 First repeated word :: had_07 First repeated word :: had08 had No Repetition he80 First repeated word :: had10 had5 had No Repetition he8 had5 had2 had No Repetition he4 First repeated word :: had07 First repeated word :: had17 had No Repetition he80 First repeated word :: had10 had5 had No Repetition he8
had5 First repeated word :: had23 ________25 had No Repetition he_16_______25 had5 had8 First repeated word :: had28 had No Repetition he80________5______92 First repeated word :: had_07 First repeated word :: had32 had5 had No Repetition he8 ________25 had No Repetition he_16_______36 had5 First repeated word :: had38 First repeated word :: had2 First repeated word :: had5 had No Repetition he54 had No Repetition he8 had No Repetition he5 had No Repetition he8
had No Repetition he5 // Cpp program to find first repeated word in a string 7had No Repetition he5 had8 First repeated word :: had06 had No Repetition he54 First repeated word :: had08 had No Repetition he80 First repeated word :: had10 had No Repetition he5 had No Repetition he8 had No Repetition he5 had2 had No Repetition he4 had No Repetition he54 First repeated word :: had17 had No Repetition he80 First repeated word :: had10 had No Repetition he5 had No Repetition he8 had No Repetition he5 had8 First repeated word :: had28 had No Repetition he80 had No Repetition he92 had No Repetition he54 First repeated word :: had32 had No Repetition he5 had No Repetition he8
had No Repetition he5 had8 First repeated word :: had76 First repeated word :: had2 First repeated word :: had78 had No Repetition he54 First repeated word :: had80 had No Repetition he54 First repeated word :: had82 had No Repetition he54 First repeated word :: had84 had No Repetition he56 First repeated word :: had86
had No Repetition he54 First repeated word :: had88 had No Repetition he54 First repeated word :: had90
had No Repetition he54 First repeated word :: had92 had No Repetition he54 First repeated word :: had94
had No Repetition he54 First repeated word :: had96 had No Repetition he5 had No Repetition he8 had No Repetition he5 had2 had No Repetition he4 had No Repetition he54 had03 had No Repetition he07 First repeated word :: had10 had No Repetition he5 had No Repetition he8 had No Repetition he43 had No Repetition he8 had No Repetition he43 had No Repetition he40 had No Repetition he47 had No Repetition he2 had14 had No Repetition he43 had No Repetition he4 had No Repetition he5 had18 had No Repetition he54 had No Repetition he55 had No Repetition he15 First repeated word :: had5 had No Repetition he5 had24 had No Repetition he19 First repeated word :: had5 had No Repetition he5 had28 had No Repetition he23 First repeated word :: had5 had No Repetition he5 had No Repetition he26 had No Repetition he5 had No Repetition he28 had No Repetition he5 had No Repetition he30 had No Repetition he43 had No Repetition he8 had No Repetition he_8
had_40 Python3had_41 had_42 had43
had No Repetition he5 had45 had No Repetition he55 had47________25______48 had No Repetition he5 had50 had No Repetition he55 First repeated word :: had2 had No Repetition he5 had54 had No Repetition he55 First repeated word :: had2 had No Repetition he5 had No Repetition he5 had59 had No Repetition he5 First repeated word :: had9 had62 had63 had64 had0 had66 had67 had68 had No Repetition he80 had70 had68 had No Repetition he80 had70 had68 had No Repetition he80 had76 had No Repetition he_54 ________25 had No Repetition he_25_______79 had5 had8 had82 had No Repetition he55 had No Repetition he0 had76 had No Repetition he4 had50 had88 had No Repetition he55 had90 had_5 had5 had93 had5 had2 had96 had_5 had No Repetition he4 had99 had No Repetition he4 had8 had No Repetition he02 had63 had No Repetition he04 had No Repetition he05 had54 had No Repetition he55 had50 had No Repetition he4 had2 had96 had No Repetition he05 had No Repetition he13 had No Repetition he55 had No Repetition he80 had No Repetition he_4 had No Repetition he4 had No Repetition he18 had No Repetition he4 had50 had No Repetition he55 First repeated word :: had2 had No Repetition he_4 had No Repetition he5 had No Repetition he25 had No Repetition he5 had8 had No Repetition he02 had63 had No Repetition he04 had5 had54 had No Repetition he55 had50 had No Repetition he_35 had No Repetition he5 had8 had No Repetition he38 had No Repetition he55 had No Repetition he40
had5 had No Repetition he42 had5 had54 had No Repetition he55 had No Repetition he46 had68 had No Repetition he80 had No Repetition he49 had5 had No Repetition he51 had No Repetition he52 had No Repetition he5 had2 had96 had5 had No Repetition he51 had0 had No Repetition he59 had No Repetition he1
had No Repetition he_61 had No Repetition he62 had No Repetition he55 had No Repetition he15 had No Repetition he65 had No Repetition he55 had No Repetition he19 had No Repetition he68 had No Repetition he55 had No Repetition he23 had No Repetition he_71 had No Repetition he_72 had No Repetition he_73
had No Repetition he_74 C#had No Repetition he_75
had No Repetition he77
had No Repetition he79
had No Repetition he41 had No Repetition he42 had No Repetition he43 had No Repetition he47 had No Repetition he2 had No Repetition he85 had No Repetition he86 had No Repetition he87 had No Repetition he43 had No Repetition he4 had No Repetition he5 had No Repetition he91 had No Repetition he86 had70 had No Repetition he7 had No Repetition he95 had No Repetition he56 had No Repetition he91 had No Repetition he54 had No Repetition he86 had70 had No Repetition he7 had02 had No Repetition he9 had No Repetition he5 had No Repetition he86 First repeated word :: had38 First repeated word :: had2________16______5 had No Repetition he5 had No Repetition he86 had11 First repeated word :: had2 First repeated word :: had5 had No Repetition he5 had No Repetition he72 had No Repetition he5 had No Repetition he74 had No Repetition he5 First repeated word :: had9 had0 had No Repetition he7 had22 had No Repetition he54 had No Repetition he85 had No Repetition he54 had No Repetition he87 had No Repetition he54 had8 had29 had No Repetition he0 had No Repetition he92 ________25 had No Repetition he_25_______33 had No Repetition he54 had No Repetition he8 had No Repetition he54 had No Repetition he98 had No Repetition he54 First repeated word :: had00 had No Repetition he54 had2 had No Repetition he4 had5 had8 had45 First repeated word :: had07 had47 had5 had No Repetition he8 had5 had2 had No Repetition he4 First repeated word :: had07 had54 had5 had No Repetition he8 had5 First repeated word :: had23 ________25 had No Repetition he_16_______25 had5 had8 had63 First repeated word :: had_07 First repeated word :: had32 had5 had No Repetition he8 ________25 had No Repetition he_16_______36 had5 First repeated word :: had38 First repeated word :: had2 First repeated word :: had5 had No Repetition he54 had No Repetition he8 had No Repetition he5 had No Repetition he8
had No Repetition he5 // Cpp program to find first repeated word in a string 7had No Repetition he5 had8 had45 had No Repetition he54 had47 had No Repetition he5 had No Repetition he8 had No Repetition he5 had2 had No Repetition he4 had No Repetition he54 had54 had No Repetition he5 had No Repetition he8
had No Repetition he5 had8 had63 had No Repetition he54 First repeated word :: had32 had No Repetition he5 had No Repetition he8
had No Repetition he5 had8 First repeated word :: had03 First repeated word :: had2 had No Repetition he92 had No Repetition he54 First repeated word :: had80 had No Repetition he54 First repeated word :: had82 had No Repetition he54 First repeated word :: had11 First repeated word :: had12 had No Repetition he54 First repeated word :: had14 had No Repetition he54 First repeated word :: had16 had No Repetition he56 had No Repetition he86________16______19 had No Repetition he5 had No Repetition he8 had No Repetition he5 had2 had No Repetition he4 had No Repetition he54 First repeated word :: had26 had No Repetition he07 First repeated word :: had10 had No Repetition he5 had No Repetition he8 had No Repetition he43 had No Repetition he8 had No Repetition he43 had No Repetition he40 had No Repetition he47 had No Repetition he2 First repeated word :: had37 had No Repetition he43 had No Repetition he4 had No Repetition he5 had No Repetition he86 had No Repetition he62 had No Repetition he54 had No Repetition he55 had No Repetition he15 First repeated word :: had5 had No Repetition he5 had No Repetition he86 First repeated word :: had49 had No Repetition he19 First repeated word :: had5 had No Repetition he5 had No Repetition he86 First repeated word :: had54 had No Repetition he23 First repeated word :: had5 had No Repetition he5 had No Repetition he26 had No Repetition he5 had No Repetition he28 had No Repetition he5 had No Repetition he30 had No Repetition he43 had No Repetition he8 had No Repetition he_8
had_40 JavascriptFirst repeated word :: had_67 First repeated word :: had_68
First repeated word :: had_69 First repeated word :: had70 had No Repetition he_4 had No Repetition he5 First repeated word :: had73 had No Repetition he56 First repeated word :: had75 had No Repetition he9 had No Repetition he5 First repeated word :: had78 First repeated word :: had2 First repeated word :: had5 had No Repetition he5 First repeated word :: had82 First repeated word :: had2 First repeated word :: had5 had No Repetition he5 had No Repetition he5 First repeated word :: had7 had No Repetition he5 First repeated word :: had9 First repeated word :: had90 had No Repetition he5 had No Repetition he4 had5 had6 had5 had8 had29 had No Repetition he0 had No Repetition he1 had5 had No Repetition he4 had No Repetition he4 had33 had No Repetition he_4 had5 had No Repetition he8 had_5 had5 had0 had5 had2 had5 had No Repetition he4 had_5 had No Repetition he4 had8 had No Repetition he4 had8 // Cpp program to find first repeated word in a string 19________16 Repeatedword(n) /* n is the string */ Step 1: first split given string separated by space into words. Step 2: now convert the list of words into a dictionary. Step 3: traverse list of words and check which the first word has frequency >1_16_______32 had No Repetition he4 had2 // Cpp program to find first repeated word in a string 24had No Repetition he_4 had No Repetition he4 First repeated word :: had5 had No Repetition he4 First repeated word :: had38 First repeated word :: had2 First repeated word :: had5 had No Repetition he_4 had5 had No Repetition he8 had No Repetition he5 had No Repetition he8 had No Repetition he5 had No Repetition he5 // Cpp program to find first repeated word in a string 7had No Repetition he5 had8 // Cpp program to find first repeated word in a string 42
had No Repetition he5 had8 #include<bits/stdc++.h> 8First repeated word :: had2 had No Repetition he1 had No Repetition he5 had No Repetition he4 had5 using 4had5 // Cpp program to find first repeated word in a string 54First repeated word :: had2 First repeated word :: had10 had5 // Cpp program to find first repeated word in a string 58had No Repetition he5 had No Repetition he8 had No Repetition he5 had2 had No Repetition he5 // Cpp program to find first repeated word in a string 64had No Repetition he59 First repeated word :: had10 had No Repetition he_8
had No Repetition he15 First repeated word :: had5
had No Repetition he19 First repeated word :: had5
had No Repetition he23 First repeated word :: had5 had No Repetition he_26 had No Repetition he_28 had No Repetition he_30
Keluaran had No Repetition he_ Pendekatan lain. Idenya adalah untuk menandai string dan menyimpan setiap kata dan hitungannya di peta hash. Kemudian telusuri string lagi dan untuk setiap kata string, periksa jumlahnya di peta hash yang dibuat. Penerapan BPK
had No Repetition he0 had No Repetition he1
had No Repetition he_4 had No Repetition he5 // Cpp program to find first repeated word in a string 93had No Repetition he5 // Cpp program to find first repeated word in a string 95had No Repetition he5 // Cpp program to find first repeated word in a string 97had No Repetition he5 // Cpp program to find first repeated word in a string 99
had No Repetition he5 #include<bits/stdc++.h> 01had No Repetition he5 #include<bits/stdc++.h> 03
had No Repetition he5 #include<bits/stdc++.h> 05had No Repetition he5 #include<bits/stdc++.h> 07had No Repetition he5 had No Repetition he6 had No Repetition he7 #include<bits/stdc++.h> 11
had No Repetition he5 #include<bits/stdc++.h> 13had No Repetition he5 #include<bits/stdc++.h> 15
had No Repetition he5 #include<bits/stdc++.h> 17 #include<bits/stdc++.h> 18had No Repetition he0 First repeated word :: had78 had5 had8 #include<bits/stdc++.h> 23had No Repetition he4 #include<bits/stdc++.h> 25#include<bits/stdc++.h> 26had5 had2 had No Repetition he4 #include<bits/stdc++.h> 30had No Repetition he4 #include<bits/stdc++.h> 32had No Repetition he5 had No Repetition he8
had No Repetition he5 #include<bits/stdc++.h> 36had No Repetition he5 #include<bits/stdc++.h> 38
had No Repetition he5 #include<bits/stdc++.h> 40had No Repetition he5 #include<bits/stdc++.h> 42had No Repetition he5 #include<bits/stdc++.h> 44had No Repetition he5 #include<bits/stdc++.h> 17 #include<bits/stdc++.h> 47had No Repetition he0 First repeated word :: had78 had5 had No Repetition he7 #include<bits/stdc++.h> 52had5 had8 #include<bits/stdc++.h> 55had No Repetition he4 had No Repetition he34 #include<bits/stdc++.h> 58had5 had No Repetition he8 had No Repetition he5 had No Repetition he8
had No Repetition he5 had No Repetition he34 #include<bits/stdc++.h> 65First repeated word :: had5 had No Repetition he_8
had No Repetition he7 had No Repetition he11 had No Repetition he_4 had No Repetition he5 #include<bits/stdc++.h> 73had No Repetition he15 First repeated word :: had10 had No Repetition he5 #include<bits/stdc++.h> 77had No Repetition he5 had8 #include<bits/stdc++.h> 80#include<bits/stdc++.h> 65had No Repetition he1 ________25 had No Repetition he_1609_______84 #include<bits/stdc++.h> 85
had No Repetition he5 had2 had5 #include<bits/stdc++.h> 84#include<bits/stdc++.h> 92First repeated word :: had5 had No Repetition he5 had No Repetition he34 had No Repetition he35 had No Repetition he_8 Jawa
had No Repetition he38 had No Repetition he39
had No Repetition he41 using 03had No Repetition he5 had No Repetition he5 // Cpp program to find first repeated word in a string 89had No Repetition he5 had No Repetition he47 using 09had No Repetition he5 had No Repetition he4 had5 // Cpp program to find first repeated word in a string 93had5 // Cpp program to find first repeated word in a string 95had5 // Cpp program to find first repeated word in a string 97had5 // Cpp program to find first repeated word in a string 99had No Repetition he5 had5 using 22using 23First repeated word :: had10 had No Repetition he5 had5 #include<bits/stdc++.h> 05had5 #include<bits/stdc++.h> 07had5 using 31had No Repetition he56 using 33had No Repetition he5 had5 #include<bits/stdc++.h> 13had5 #include<bits/stdc++.h> 15had No Repetition he5 had5 First repeated word :: had9 had0 had No Repetition he7 using 44had No Repetition he82 using 46had No Repetition he4 had8 using 49had No Repetition he05 using 51had No Repetition he80 First repeated word :: had10 #include<bits/stdc++.h> 26had No Repetition he4 had2 had No Repetition he05 #include<bits/stdc++.h> 30had No Repetition he05 using 60had No Repetition he80 using 62had5 had No Repetition he8 had No Repetition he5 had5 #include<bits/stdc++.h> 36had5 #include<bits/stdc++.h> 38had No Repetition he5 had5 #include<bits/stdc++.h> 40had5 #include<bits/stdc++.h> 42had5 First repeated word :: had9 had0 had No Repetition he7 using 44had No Repetition he82 using 46had No Repetition he4 had No Repetition he7 using 84had No Repetition he4 had8 using 87had No Repetition he80 had No Repetition he92 had No Repetition he05 had No Repetition he34 using 92had No Repetition he4 had No Repetition he8 had5 had No Repetition he8 had No Repetition he5 had5 had No Repetition he34 #include<bits/stdc++.h> 65First repeated word :: had5 had No Repetition he5 had No Repetition he8 had No Repetition he5 had No Repetition he5 #include<bits/stdc++.h> 68had No Repetition he5 had No Repetition he40 had No Repetition he47 had No Repetition he2 had No Repetition he011 had No Repetition he5 had No Repetition he4 had5 had No Repetition he015 had No Repetition he15 First repeated word :: had5 had5 had No Repetition he019 had5 had8 had No Repetition he022 #include<bits/stdc++.h> 65had No Repetition he024 had No Repetition he4 had No Repetition he026 #include<bits/stdc++.h> 85 had No Repetition he028 had5 had2 had No Repetition he4 had No Repetition he026 #include<bits/stdc++.h> 92First repeated word :: had10 had No Repetition he5 had No Repetition he8 had No Repetition he_8 Javascripthad No Repetition he_038 had No Repetition he_4 had No Repetition he5 // Cpp program to find first repeated word in a string 89had No Repetition he5 had No Repetition he043 had No Repetition he5 had No Repetition he4 had5 // Cpp program to find first repeated word in a string 93had5 // Cpp program to find first repeated word in a string 95had5 // Cpp program to find first repeated word in a string 97had5 // Cpp program to find first repeated word in a string 99________25______5 had No Repetition he055 had No Repetition he056 using 23First repeated word :: had10 had_5 had5 had No Repetition he061 had5 #include<bits/stdc++.h> 07________25______5 had No Repetition he055 had No Repetition he066 had No Repetition he56 had No Repetition he068 had5 #include<bits/stdc++.h> 13had5 had No Repetition he072 had5 First repeated word :: had9 had No Repetition he075 had5 had No Repetition he4 had No Repetition he4 had8 had No Repetition he080 had No Repetition he4 had No Repetition he4 had No Repetition he05 had No Repetition he084 had No Repetition he4 had No Repetition he8 had No Repetition he4 had2 had No Repetition he4 had No Repetition he4 had No Repetition he05 had No Repetition he092 had No Repetition he05 had No Repetition he094 had No Repetition he4 had No Repetition he8 had5 had No Repetition he8 had5 #include<bits/stdc++.h> 36had5 #include<bits/stdc++.h> 38had5 #include<bits/stdc++.h> 40had5 had No Repetition he106 had5 First repeated word :: had9 had No Repetition he075 had5 had No Repetition he4 had No Repetition he4 had No Repetition he055 using 84had No Repetition he4 had8 had No Repetition he117 had No Repetition he4 had No Repetition he4 had No Repetition he05 had No Repetition he34 using 92had No Repetition he4 had No Repetition he8 had5 had No Repetition he8 had5 had No Repetition he34 #include<bits/stdc++.h> 65First repeated word :: had5 had No Repetition he5 had No Repetition he8 had No Repetition he5 #include<bits/stdc++.h> 68had No Repetition he5 had No Repetition he136 had No Repetition he5 had No Repetition he4 had5 had No Repetition he055 had No Repetition he141 had No Repetition he15 First repeated word :: had5 had5 had No Repetition he055 had No Repetition he146 had5 had8 had No Repetition he149 #include<bits/stdc++.h> 65had No Repetition he1 had5 had No Repetition he4 had No Repetition he4 had No Repetition he155 #include<bits/stdc++.h> 85 had No Repetition he028 had5 had No Repetition he8 had5 had2 had5 had No Repetition he4 had No Repetition he4 had No Repetition he155 #include<bits/stdc++.h> 92First repeated word :: had10 had5 had No Repetition he8 had No Repetition he5 had No Repetition he8 had No Repetition he_8 had No Repetition he_173
had No Repetition he_174 Keluaran First repeated word :: had Metode #2. Menggunakan fungsi python bawaan
Penerapan Python3had No Repetition he_175 had No Repetition he176 had No Repetition he177 had No Repetition he38 had No Repetition he179
had No Repetition he_180 had No Repetition he_181 had_42 had No Repetition he183
had No Repetition he5 had No Repetition he185 had No Repetition he5 had No Repetition he187 had No Repetition he55 had No Repetition he189 had No Repetition he190 using 23had No Repetition he024 had No Repetition he5 had No Repetition he5 had No Repetition he195 had No Repetition he5 had No Repetition he197 had No Repetition he55 had No Repetition he199 had No Repetition he5 had No Repetition he5________5______202 had No Repetition he5 First repeated word :: had9 had62 had63 had No Repetition he207 had No Repetition he_54 had5 had No Repetition he210 had_5 had5 had8 had No Repetition he214 had No Repetition he80________25______76 had No Repetition he4 had No Repetition he218 had No Repetition he4 had No Repetition he34 had62
had No Repetition he_222 had No Repetition he223 had No Repetition he55 had No Repetition he225 had No Repetition he51 had No Repetition he227 had No Repetition he_228 Keluaran had Pendekatan lain.Alih-alih melacak jumlah token(kata) tertentu, kita dapat melacak kemunculan pertama token(kata) menggunakan peta yang tidak diurutkan. Ini tidak memerlukan loop tambahan untuk dilintasi dalam hashmap atau string untuk menemukan string yang berulang. Dengan demikian, pada akhirnya mengubah kompleksitas waktu dari O(2*n) menjadi O(n) sedangkan kompleksitas ruang tetap sama Penerapan C++had No Repetition he_229
had No Repetition he0 had No Repetition he1
had No Repetition he2 had No Repetition he3 had No Repetition he_4 had No Repetition he5 had No Repetition he7 had No Repetition he239 had No Repetition he240 had No Repetition he5 had No Repetition he6 had No Repetition he7 had No Repetition he244 had5 had No Repetition he246 had No Repetition he247 had No Repetition he5 had No Repetition he249 First repeated word :: had2 had No Repetition he251 First repeated word :: had2 First repeated word :: had5 had No Repetition he5 had No Repetition he7 had No Repetition he256 had No Repetition he257 had No Repetition he258 had No Repetition he259
had No Repetition he5 had No Repetition he7 had No Repetition he262 had5 had No Repetition he264 had No Repetition he265 First repeated word :: had2 had No Repetition he267
had No Repetition he5 had No Repetition he269 had No Repetition he5 had No Repetition he271 had No Repetition he5 #include<bits/stdc++.h> 17 had No Repetition he274
had5 had No Repetition he276 had5 had No Repetition he278 had5 had8 had No Repetition he281 had No Repetition he0 had No Repetition he283 had No Repetition he4 had8 had No Repetition he286 had No Repetition he287 had No Repetition he288 had No Repetition he289 had No Repetition he05 had No Repetition he291 had No Repetition he4 had No Repetition he8 had No Repetition he4 had2 had No Repetition he4 had No Repetition he297 had No Repetition he298 had No Repetition he299 had No Repetition he05 had8 had No Repetition he302 had No Repetition he303 had No Repetition he304 had No Repetition he303 First repeated word :: had32 had No Repetition he05 had No Repetition he8 had No Repetition he4 had No Repetition he8
had No Repetition he4 had No Repetition he312 had No Repetition he4 First repeated word :: had38 First repeated word :: had2 First repeated word :: had5 had No Repetition he4 had No Repetition he318 had No Repetition he4 had No Repetition he320 had5 had No Repetition he8 had5 had2 had No Repetition he4 had No Repetition he4 had No Repetition he327 had No Repetition he4 had No Repetition he329 had5 had No Repetition he8 had No Repetition he5 had No Repetition he8
had No Repetition he5 had No Repetition he335 had No Repetition he5 had No Repetition he337 had No Repetition he5 had8 had No Repetition he340 First repeated word :: had2 had No Repetition he1 had5 #include<bits/stdc++.h> 84had No Repetition he59 had No Repetition he346 had No Repetition he5 had2 had5 had No Repetition he350 had No Repetition he_8
had No Repetition he7 had No Repetition he11 had No Repetition he_4 had No Repetition he5 had No Repetition he356 had5 had No Repetition he55 had No Repetition he15 First repeated word :: had5 had No Repetition he5 had No Repetition he362 had No Repetition he19 First repeated word :: had5 had No Repetition he5 had No Repetition he366 had No Repetition he23 First repeated word :: had5
had No Repetition he5 had No Repetition he370 had No Repetition he5________5______372 had No Repetition he5 had No Repetition he374
had No Repetition he5 had No Repetition he34 had No Repetition he35 had No Repetition he_8 Keluaran had No Repetition he_ Pendekatan yang DioptimalkanAlih-alih menghitung jumlah kemunculan setiap kata yang akan memiliki kompleksitas waktu dan ruang O(N), di mana N adalah jumlah kata, kita dapat berhenti ketika jumlah kata apa pun menjadi 2. Itu tidak perlu mengulangi semua kata dalam string Katakanlah kata berulang pertama kita ada di indeks Mth, lalu Dengan menggunakan pendekatan ini, kompleksitas ruang dan waktu berkurang dari O(N) menjadi O(M) Di mana, N. jumlah kata dalam sebuah string M. Indeks di mana kata berulang pertama hadir Namun, Kasus terburuk (Ketika tidak ada kata yang diulang atau kata yang diulang akhirnya muncul) kompleksitas ruang dan waktu akan tetap O(N) Langkah
Penerapan Python3had No Repetition he_379 had No Repetition he176 had No Repetition he177 had No Repetition he38 had No Repetition he383
had_42 had No Repetition he385
had No Repetition he5 had No Repetition he387 had No Repetition he5 had No Repetition he389 had No Repetition he5 had No Repetition he391 had No Repetition he5 had No Repetition he393 had No Repetition he5 had No Repetition he395 had No Repetition he55 had No Repetition he397 had No Repetition he398 had96 had No Repetition he82 had No Repetition he1
had No Repetition he5 had No Repetition he403 had No Repetition he5 First repeated word :: had9 had62 had63 had No Repetition he408
had5 had No Repetition he410 had5 had No Repetition he412 had5 had No Repetition he414 had88 had No Repetition he55 had No Repetition he80
had5 had No Repetition he419 had5 had No Repetition he421 had5 had8 had No Repetition he424 had No Repetition he80 had96 had No Repetition he4 had No Repetition he34 had62
had No Repetition he5 had No Repetition he431 had No Repetition he5 had No Repetition he433 had No Repetition he5 had No Repetition he34 had No Repetition he436
had No Repetition he_437 had8 had No Repetition he439 had No Repetition he55 had No Repetition he55 had No Repetition he442 had96 had No Repetition he5 had No Repetition he445 had No Repetition he55 had No Repetition he15 had No Repetition he5 had No Repetition he51 had No Repetition he450
had No Repetition he5 had No Repetition he452 JavascriptFirst repeated word :: had_67
First repeated word :: had_69 had No Repetition he455
had No Repetition he5 had No Repetition he457 had No Repetition he5 had No Repetition he459 had No Repetition he5 had No Repetition he461 had No Repetition he5 had No Repetition he463 had No Repetition he5 had No Repetition he465 had No Repetition he56 had No Repetition he467
had No Repetition he5 had No Repetition he469 had No Repetition he5 First repeated word :: had9 had No Repetition he472 had No Repetition he0 had No Repetition he474
had5 had No Repetition he476 had5 had No Repetition he478 had5 had8 had No Repetition he481 had No Repetition he4 had No Repetition he483 had5 had No Repetition he8 had5 had2 had No Repetition he488
had5 had No Repetition he490 had5 had No Repetition he492 had5 had8 had No Repetition he495 had No Repetition he4 had No Repetition he34 had62 had No Repetition he5 had No Repetition he8
had No Repetition he5 had No Repetition he502 had No Repetition he5 had No Repetition he504 had No Repetition he5 had No Repetition he34 had No Repetition he436 had No Repetition he_8
had No Repetition he509 had No Repetition he510 had No Repetition he15 had No Repetition he_512
Keluaran had Kompleksitas waktu. O(M) Pendekatan yang Dioptimalkan 2Alih-alih menghitung jumlah kemunculan setiap kata yang akan memiliki O(N) kompleksitas ruang dan waktu, di mana N adalah jumlah kata, kita dapat menyimpan kata dalam HashSet, dan segera setelah kita mencapai kata yang sudah ada Penerapan C++
had No Repetition he0 had No Repetition he1
had No Repetition he_4 had No Repetition he5 // Cpp program to find first repeated word in a string 93had No Repetition he5 // Cpp program to find first repeated word in a string 95had No Repetition he5 // Cpp program to find first repeated word in a string 97had No Repetition he5 // Cpp program to find first repeated word in a string 99
had No Repetition he5 #include<bits/stdc++.h> 01had No Repetition he5 #include<bits/stdc++.h> 03
had No Repetition he5 had No Repetition he537 had No Repetition he5 #include<bits/stdc++.h> 07had No Repetition he5 had No Repetition he541
had No Repetition he5 #include<bits/stdc++.h> 13had No Repetition he5 had No Repetition he545
had No Repetition he5 #include<bits/stdc++.h> 17 #include<bits/stdc++.h> 18had No Repetition he0 First repeated word :: had78 had5 had No Repetition he552 had5 had8 had No Repetition he555 had No Repetition he4 had No Repetition he34 #include<bits/stdc++.h> 58had5 had No Repetition he8
had5 #include<bits/stdc++.h> 30had5 had No Repetition he564 had No Repetition he5 had No Repetition he8
had No Repetition he5 had No Repetition he34 #include<bits/stdc++.h> 65First repeated word :: had5 had No Repetition he_8
had No Repetition he7 had No Repetition he11 had No Repetition he_4 had No Repetition he5 #include<bits/stdc++.h> 73had No Repetition he15 First repeated word :: had10 had No Repetition he5 #include<bits/stdc++.h> 77had No Repetition he5 had8 #include<bits/stdc++.h> 80#include<bits/stdc++.h> 65had No Repetition he1 had5 #include<bits/stdc++.h> 84#include<bits/stdc++.h> 85 had No Repetition he590
had No Repetition he346 had No Repetition he5 had2 had5 #include<bits/stdc++.h> 84#include<bits/stdc++.h> 92First repeated word :: had5 had No Repetition he5 had No Repetition he34 had No Repetition he35 had No Repetition he_8 Jawa
had No Repetition he38 had No Repetition he39
had No Repetition he40 had No Repetition he41 using 03had No Repetition he5 had No Repetition he5 // Cpp program to find first repeated word in a string 89had No Repetition he5 had No Repetition he47 using 09had No Repetition he5 had No Repetition he4 had5 // Cpp program to find first repeated word in a string 93had5 using 22using 23First repeated word :: had10 had_5 had5 had No Repetition he626 had5 had No Repetition he628 had No Repetition he56 had No Repetition he630 had_5 had5 had No Repetition he633 had5 First repeated word :: had9 had0 had No Repetition he7 using 44had No Repetition he82 had No Repetition he640 had No Repetition he_4 had No Repetition he4 had No Repetition he552 had No Repetition he4 had8 had No Repetition he646 had No Repetition he05 had No Repetition he34 using 92had No Repetition he4 had No Repetition he8 had No Repetition he_4 had No Repetition he4 #include<bits/stdc++.h> 30had No Repetition he4 had No Repetition he656 had5 had No Repetition he8 had No Repetition he5 had5 had No Repetition he34 #include<bits/stdc++.h> 65First repeated word :: had5 had No Repetition he5 had No Repetition he8 had No Repetition he5 had No Repetition he5 #include<bits/stdc++.h> 68had No Repetition he5 had No Repetition he40 had No Repetition he47 had No Repetition he2 had No Repetition he011 had No Repetition he5 had No Repetition he4 had5 had No Repetition he015 had No Repetition he15 First repeated word :: had5 had5 had No Repetition he019 had5 had8 had No Repetition he022 #include<bits/stdc++.h> 65had No Repetition he024 had No Repetition he4 had No Repetition he026 #include<bits/stdc++.h> 85 had No Repetition he028 had5 had2 had No Repetition he4 had No Repetition he026 #include<bits/stdc++.h> 92First repeated word :: had10 had No Repetition he5 had No Repetition he8 had No Repetition he_8 Javascripthad No Repetition he_700
had No Repetition he_038 had No Repetition he_4 had No Repetition he5 // Cpp program to find first repeated word in a string 89had No Repetition he5 had No Repetition he043 had No Repetition he5 had No Repetition he4 had5 // Cpp program to find first repeated word in a string 93________25______5 had No Repetition he055 had No Repetition he056 using 23First repeated word :: had10 had_5 had5 had No Repetition he719 had5 had No Repetition he055 had No Repetition he722 had No Repetition he56 ________5______724 had_5 had5 had No Repetition he727 had5 First repeated word :: had9 had No Repetition he075 had5 had No Repetition he4 had No Repetition he4 had No Repetition he552 had No Repetition he4 had8 had No Repetition he737 had No Repetition he4 had No Repetition he4 had No Repetition he05 had No Repetition he34 using 92had No Repetition he4 had No Repetition he8 had No Repetition he4 #include<bits/stdc++.h> 30had No Repetition he4 had No Repetition he656 had5 had No Repetition he8 had5 had No Repetition he34 #include<bits/stdc++.h> 65First repeated word :: had5 had No Repetition he5 had No Repetition he8 had No Repetition he5 had No Repetition he5 #include<bits/stdc++.h> 68had No Repetition he5 had No Repetition he136 had No Repetition he5 had No Repetition he4 had5 had No Repetition he055 had No Repetition he141 had No Repetition he15 First repeated word :: had5 had5 had No Repetition he055 had No Repetition he146 had5 had8 had No Repetition he149 #include<bits/stdc++.h> 65had No Repetition he1 had5 had No Repetition he4 had No Repetition he4 had No Repetition he155 #include<bits/stdc++.h> 85 had No Repetition he028 had5 had No Repetition he8 had5 had2 had5 had No Repetition he4 had5 had No Repetition he155 #include<bits/stdc++.h> 92First repeated word :: had10 had5 had No Repetition he8 had No Repetition he5 had No Repetition he8 had No Repetition he_8 had No Repetition he_173
had No Repetition he_799 Keluaran First repeated word :: had Artikel ini disumbangkan oleh Aarti_Rathi dan Mandeep Singh. Jika Anda menyukai GeeksforGeeks dan ingin berkontribusi, Anda juga dapat menulis artikel menggunakan kontribusi. geeksforgeeks. org atau kirimkan artikel Anda ke kontribusi@geeksforgeeks. org. Lihat artikel Anda muncul di halaman utama GeeksforGeeks dan bantu Geeks lainnya Bagaimana cara menemukan kata berulang dalam sebuah string dengan Python?Pendekatannya sederhana, . Pemisahan pertama diberikan string yang dipisahkan oleh spasi Sekarang ubah daftar kata menjadi kamus menggunakan koleksi. Metode penghitung (iterator). Kamus berisi kata-kata sebagai kunci dan frekuensinya sebagai nilai Sekarang lintasi daftar kata lagi dan periksa kata mana yang memiliki frekuensi lebih besar dari 1 Bagaimana menemukan karakter berulang pertama dalam string yang diberikan dengan Python?Diberi sebuah string, temukan karakter berulang pertama di dalamnya. . Salin larik yang diberikan ke larik bantu temp[] Urutkan array temp menggunakan algoritma pengurutan waktu O(N log N). Pindai array input dari kiri ke kanan. Untuk setiap elemen, hitung kemunculannya dalam temp[] menggunakan pencarian biner Bagaimana Anda menemukan kata-kata berulang dalam sebuah string?Algoritma . Tentukan string Ubah string menjadi huruf kecil agar perbandingan tidak sensitif Pisahkan string menjadi kata-kata Dua loop akan digunakan untuk menemukan kata duplikat. . Jika ditemukan kecocokan, tambahkan hitungan dengan 1 dan atur duplikat kata ke '0' untuk menghindari penghitungan lagi Bagaimana cara menemukan kata pertama dari teks dengan Python?Gunakan str. split() dan pengindeksan daftar untuk mendapatkan kata pertama dalam string . Hubungi str. split() untuk membuat daftar semua kata dalam str yang dipisahkan oleh spasi atau karakter baris baru. |