Tulis program python untuk menemukan kata berulang pertama dalam string yang diberikan.

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

Jika tidak ada karakter yang berulang, cetak -1

Saya harap, Anda mengerti apa yang sebenarnya akan kami lakukan. Jadi mari kita lanjutkan…

Metode kekerasan

Lintasi 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 string

Gunakan kamus untuk menghitung berapa kali setiap karakter muncul dalam string – kuncinya adalah karakter dan nilainya adalah frekuensi

  • Lintasi seluruh string
  • Periksa apakah karakter saat ini sudah ada di kamus
  • Jika ada, perbarui frekuensi karakter saat ini sebesar 1   i. e dict[str[i]]++
  • Jika tidak, masukkan karakter dengan frekuensi 1 i. e. dict[str[i]]=1

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 string

Mulailah 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

  • Membagi string yang diberikan menjadi kata-kata yang dipisahkan oleh spasi
  • Kemudian kami mengubah kata-kata ini menjadi Kamus menggunakan koleksi
  • Telusuri daftar kata ini dan periksa kata pertama mana yang frekuensinya > 1

Program - Temukan Kata yang Diulangi

Dalam 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()

Algoritma

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

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)

Keluaran

Enter the String ::We are all peaceful soul and blissful soul and loveful soul happy soul
REPEATED WORD IS ::> soul
_

Tulis program python untuk menemukan kata berulang pertama dalam string yang diberikan.


Tulis program python untuk menemukan kata berulang pertama dalam string yang diberikan.

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++




// Cpp program to find first repeated word in a string

#include<bits/stdc++.h>

using

had
No Repetition
he
0
had
No Repetition
he
1

had
No Repetition
he
2
had
No Repetition
he
3

had
No Repetition
he
_4

had
No Repetition
he
5
had
No Repetition
he
6
had
No Repetition
he
7
had
No Repetition
he
8
had
No Repetition
he
9

had
No Repetition
he
5
First repeated word :: had
1
First repeated word :: had
2
First repeated word :: had
3
First repeated word :: had
2
First repeated word :: had
5

had
No Repetition
he
5
First repeated word :: had
7

had
No Repetition
he
5
First repeated word :: had
9
had
0
had
No Repetition
he
7
had
2

had
No Repetition
he
5
had
No Repetition
he
4

had
5
had
6

had
5
had
8
had
9
had
No Repetition
he
0
had
No Repetition
he
1

had
5
had
No Repetition
he
4

had
No Repetition
he
4
had
No Repetition
he
5

had
No Repetition
he
_4

had
5
had
No Repetition
he
8

had
5
had
0

had
5
had
2

had
5
had
No Repetition
he
4

had
No Repetition
he
4
had
6

had
No Repetition
he
4
had
8

had
No Repetition
he
4
had
8
First repeated word :: had
1

________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
he
4
First repeated word :: had
5

had
No Repetition
he
4
First repeated word :: had
7
First repeated word :: had
2
First repeated word :: had
5

had
No Repetition
he
_4

had
5
had
No Repetition
he
8

had
No Repetition
he
5
had
No Repetition
he
8

had
No Repetition
he
5

had
No Repetition
he
5// Cpp program to find first repeated word in a string7

had
No Repetition
he
4
had
6

had
No Repetition
he
4
had
8
First repeated word :: had
1

________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

#include<bits/stdc++.h>_5

had
No Repetition
he
5
had
8#include<bits/stdc++.h>8
First repeated word :: had
2
had
No Repetition
he
1

had
No Repetition
he
5
had
No Repetition
he
4

had
5using4

had
5using6

had
5using8using9
First repeated word :: had
5

had
No Repetition
he
5
had
No Repetition
he
8

had
No Repetition
he
5
had
2

had
No Repetition
he
5
had
No Repetition
he
06
had
No Repetition
he
07
First repeated word :: had
5

had
No Repetition
he
_8

had
No Repetition
he
7
had
No Repetition
he
11

had
No Repetition
he
_4

had
No Repetition
he
5
had
No Repetition
he
14
had
No Repetition
he
15
First repeated word :: had
5

had
No Repetition
he
5
had
No Repetition
he
18
had
No Repetition
he
19
First repeated word :: had
5

had
No Repetition
he
5
had
No Repetition
he
22
had
No Repetition
he
23
First repeated word :: had
5

had
No Repetition
he
5
had
No Repetition
he
26

had
No Repetition
he
5
had
No Repetition
he
28

had
No Repetition
he
5
had
No Repetition
he
30

had
No Repetition
he
5

had
No Repetition
he
5

had
No Repetition
he
5
had
No Repetition
he
34
had
No Repetition
he
35

had
No Repetition
he
5

had
No Repetition
he
_8

Jawa




had
No Repetition
he
38
had
No Repetition
he
39

 

had
No Repetition
he
40
had
No Repetition
he
41
had
No Repetition
he
42

had
No Repetition
he
43
had
No Repetition
he
44

had
No Repetition
he
43
had
No Repetition
he
40
had
No Repetition
he
47
had
No Repetition
he
2
had
No Repetition
he
49

had
No Repetition
he
43
had
No Repetition
he
4

had
No Repetition
he
5
had
No Repetition
he
53

had
No Repetition
he
54
had
No Repetition
he
55
had
No Repetition
he
56
had
No Repetition
he
57

had
No Repetition
he
5
had
No Repetition
he
59
had
No Repetition
he
60

had
No Repetition
he
5
had
No Repetition
he
62

had
No Repetition
he
5
had
No Repetition
he
64
First repeated word :: had
2
First repeated word :: had
5

had
No Repetition
he
5
had
No Repetition
he
68
First repeated word :: had
2
First repeated word :: had
5

had
No Repetition
he
5
had
No Repetition
he
72

had
No Repetition
he
5
had
No Repetition
he
74

had
No Repetition
he
5
First repeated word :: had
9
had
0
had
No Repetition
he
7
had
No Repetition
he
79
had
No Repetition
he
80
had
No Repetition
he
81
had
No Repetition
he
82
had
No Repetition
he
83

had
No Repetition
he
54
had
No Repetition
he
85

had
No Repetition
he
54
had
No Repetition
he
87

had
No Repetition
he
54
had
8
had
No Repetition
he
90
had
No Repetition
he
0________5______92

had
5
had
No Repetition
he
_94

had
No Repetition
he
54
had
No Repetition
he
8

had
No Repetition
he
54
had
No Repetition
he
98

had
No Repetition
he
54
First repeated word :: had
00

had
No Repetition
he
54
had
2
had
No Repetition
he
4

had
5
had
8
First repeated word :: had
06

First repeated word :: had
_07
First repeated word :: had
08
had
No Repetition
he
80
First repeated word :: had
10

had
5
had
No Repetition
he
8

had
5
had
2
had
No Repetition
he
4

First repeated word :: had
07
First repeated word :: had
17
had
No Repetition
he
80
First repeated word :: had
10

had
5
had
No Repetition
he
8

 

had
5
First repeated word :: had
23

________25

had
No Repetition
he
_16_______25

had
5
had
8
First repeated word :: had
28
had
No Repetition
he
80________5______92

First repeated word :: had
_07
First repeated word :: had
32

had
5
had
No Repetition
he
8

________25

had
No Repetition
he
_16_______36

had
5
First repeated word :: had
38
First repeated word :: had
2
First repeated word :: had
5

had
No Repetition
he
54
had
No Repetition
he
8

had
No Repetition
he
5
had
No Repetition
he
8

 

had
No Repetition
he
5// Cpp program to find first repeated word in a string7

had
No Repetition
he
5
had
8
First repeated word :: had
06

had
No Repetition
he
54
First repeated word :: had
08
had
No Repetition
he
80
First repeated word :: had
10

had
No Repetition
he
5
had
No Repetition
he
8

had
No Repetition
he
5
had
2
had
No Repetition
he
4

had
No Repetition
he
54
First repeated word :: had
17
had
No Repetition
he
80
First repeated word :: had
10

had
No Repetition
he
5
had
No Repetition
he
8

had
No Repetition
he
5
had
8
First repeated word :: had
28
had
No Repetition
he
80
had
No Repetition
he
92

had
No Repetition
he
54
First repeated word :: had
32

had
No Repetition
he
5
had
No Repetition
he
8

 

had
No Repetition
he
5
had
8
First repeated word :: had
76
First repeated word :: had
2
First repeated word :: had
78

had
No Repetition
he
54
First repeated word :: had
80

had
No Repetition
he
54
First repeated word :: had
82

had
No Repetition
he
54
First repeated word :: had
84
had
No Repetition
he
56
First repeated word :: had
86

 

had
No Repetition
he
54
First repeated word :: had
88

had
No Repetition
he
54
First repeated word :: had
90

 

had
No Repetition
he
54
First repeated word :: had
92

had
No Repetition
he
54
First repeated word :: had
94

 

had
No Repetition
he
54
First repeated word :: had
96

had
No Repetition
he
5
had
No Repetition
he
8

had
No Repetition
he
5
had
2
had
No Repetition
he
4

had
No Repetition
he
54
had
03
had
No Repetition
he
07
First repeated word :: had
10

had
No Repetition
he
5
had
No Repetition
he
8

had
No Repetition
he
43
had
No Repetition
he
8

had
No Repetition
he
43
had
No Repetition
he
40
had
No Repetition
he
47
had
No Repetition
he
2
had
14

had
No Repetition
he
43
had
No Repetition
he
4

had
No Repetition
he
5
had
18

had
No Repetition
he
54
had
No Repetition
he
55
had
No Repetition
he
15
First repeated word :: had
5

had
No Repetition
he
5
had
24
had
No Repetition
he
19
First repeated word :: had
5

had
No Repetition
he
5
had
28
had
No Repetition
he
23
First repeated word :: had
5

had
No Repetition
he
5
had
No Repetition
he
26

had
No Repetition
he
5
had
No Repetition
he
28

had
No Repetition
he
5
had
No Repetition
he
30

had
No Repetition
he
43
had
No Repetition
he
8

had
No Repetition
he
_8

 

had
_40

Python3




had
_41

had
_42
had
43

 

had
No Repetition
he
5
had
45
had
No Repetition
he
55
had
47________25______48

had
No Repetition
he
5
had
50
had
No Repetition
he
55
First repeated word :: had
2

had
No Repetition
he
5
had
54
had
No Repetition
he
55
First repeated word :: had
2

had
No Repetition
he
5

had
No Repetition
he
5
had
59

had
No Repetition
he
5
First repeated word :: had
9
had
62
had
63
had
64
had
0
had
66
had
67
had
68
had
No Repetition
he
80
had
70
had
68
had
No Repetition
he
80
had
70
had
68
had
No Repetition
he
80
had
76

had
No Repetition
he
_54

________25

had
No Repetition
he
_25_______79

had
5
had
8
had
82
had
No Repetition
he
55
had
No Repetition
he
0
had
76

had
No Repetition
he
4
had
50
had
88
had
No Repetition
he
55
had
90

had
_5

had
5
had
93

had
5
had
2
had
96

had
_5

had
No Repetition
he
4
had
99

had
No Repetition
he
4
had
8
had
No Repetition
he
02
had
63
had
No Repetition
he
04

had
No Repetition
he
05
had
54
had
No Repetition
he
55
had
50

had
No Repetition
he
4
had
2
had
96

had
No Repetition
he
05
had
No Repetition
he
13
had
No Repetition
he
55
had
No Repetition
he
80

had
No Repetition
he
_4

had
No Repetition
he
4
had
No Repetition
he
18

had
No Repetition
he
4
had
50
had
No Repetition
he
55
First repeated word :: had
2

had
No Repetition
he
_4

had
No Repetition
he
5
had
No Repetition
he
25

had
No Repetition
he
5
had
8
had
No Repetition
he
02
had
63
had
No Repetition
he
04

had
5
had
54
had
No Repetition
he
55
had
50

had
No Repetition
he
_35

had
No Repetition
he
5
had
8
had
No Repetition
he
38
had
No Repetition
he
55
had
No Repetition
he
40

 

had
5
had
No Repetition
he
42

had
5
had
54
had
No Repetition
he
55
had
No Repetition
he
46
had
68
had
No Repetition
he
80
had
No Repetition
he
49

had
5
had
No Repetition
he
51
had
No Repetition
he
52

had
No Repetition
he
5
had
2
had
96

had
5
had
No Repetition
he
51
had
0
had
No Repetition
he
59
had
No Repetition
he
1

 

had
No Repetition
he
_61

had
No Repetition
he
62
had
No Repetition
he
55
had
No Repetition
he
15

had
No Repetition
he
65
had
No Repetition
he
55
had
No Repetition
he
19

had
No Repetition
he
68
had
No Repetition
he
55
had
No Repetition
he
23

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

using

had
No Repetition
he
77

using

had
No Repetition
he
79

 

had
No Repetition
he
41
had
No Repetition
he
42

had
No Repetition
he
43
had
No Repetition
he
47
had
No Repetition
he
2
had
No Repetition
he
85
had
No Repetition
he
86
had
No Repetition
he
87

had
No Repetition
he
43
had
No Repetition
he
4

had
No Repetition
he
5
had
No Repetition
he
91
had
No Repetition
he
86
had
70
had
No Repetition
he
7
had
No Repetition
he
95
had
No Repetition
he
56
had
No Repetition
he
91

had
No Repetition
he
54
had
No Repetition
he
86
had
70
had
No Repetition
he
7
had
02
had
No Repetition
he
9

had
No Repetition
he
5
had
No Repetition
he
86
First repeated word :: had
38
First repeated word :: had
2________16______5

had
No Repetition
he
5
had
No Repetition
he
86
had
11
First repeated word :: had
2
First repeated word :: had
5

had
No Repetition
he
5
had
No Repetition
he
72

had
No Repetition
he
5
had
No Repetition
he
74

had
No Repetition
he
5
First repeated word :: had
9
had
0
had
No Repetition
he
7
had
22

had
No Repetition
he
54
had
No Repetition
he
85

had
No Repetition
he
54
had
No Repetition
he
87

had
No Repetition
he
54
had
8
had
29
had
No Repetition
he
0
had
No Repetition
he
92

________25

had
No Repetition
he
_25_______33

had
No Repetition
he
54
had
No Repetition
he
8

had
No Repetition
he
54
had
No Repetition
he
98

had
No Repetition
he
54
First repeated word :: had
00

had
No Repetition
he
54
had
2
had
No Repetition
he
4

had
5
had
8
had
45

First repeated word :: had
07
had
47

had
5
had
No Repetition
he
8

had
5
had
2
had
No Repetition
he
4

First repeated word :: had
07
had
54

had
5
had
No Repetition
he
8

had
5
First repeated word :: had
23

________25

had
No Repetition
he
_16_______25

had
5
had
8
had
63

First repeated word :: had
_07
First repeated word :: had
32

had
5
had
No Repetition
he
8

________25

had
No Repetition
he
_16_______36

had
5
First repeated word :: had
38
First repeated word :: had
2
First repeated word :: had
5

had
No Repetition
he
54
had
No Repetition
he
8

had
No Repetition
he
5
had
No Repetition
he
8

 

had
No Repetition
he
5// Cpp program to find first repeated word in a string7

had
No Repetition
he
5
had
8
had
45

had
No Repetition
he
54
had
47

had
No Repetition
he
5
had
No Repetition
he
8

had
No Repetition
he
5
had
2
had
No Repetition
he
4

had
No Repetition
he
54
had
54

had
No Repetition
he
5
had
No Repetition
he
8

 

had
No Repetition
he
5
had
8
had
63

had
No Repetition
he
54
First repeated word :: had
32

had
No Repetition
he
5
had
No Repetition
he
8

 

had
No Repetition
he
5
had
8
First repeated word :: had
03
First repeated word :: had
2
had
No Repetition
he
92

had
No Repetition
he
54
First repeated word :: had
80

had
No Repetition
he
54
First repeated word :: had
82

had
No Repetition
he
54
First repeated word :: had
11
First repeated word :: had
12

had
No Repetition
he
54
First repeated word :: had
14

had
No Repetition
he
54
First repeated word :: had
16
had
No Repetition
he
56
had
No Repetition
he
86________16______19

had
No Repetition
he
5
had
No Repetition
he
8

had
No Repetition
he
5
had
2
had
No Repetition
he
4

had
No Repetition
he
54
First repeated word :: had
26
had
No Repetition
he
07
First repeated word :: had
10

had
No Repetition
he
5
had
No Repetition
he
8

had
No Repetition
he
43
had
No Repetition
he
8

had
No Repetition
he
43
had
No Repetition
he
40
had
No Repetition
he
47
had
No Repetition
he
2
First repeated word :: had
37

had
No Repetition
he
43
had
No Repetition
he
4

had
No Repetition
he
5
had
No Repetition
he
86
had
No Repetition
he
62

had
No Repetition
he
54
had
No Repetition
he
55
had
No Repetition
he
15
First repeated word :: had
5

had
No Repetition
he
5
had
No Repetition
he
86
First repeated word :: had
49
had
No Repetition
he
19
First repeated word :: had
5

had
No Repetition
he
5
had
No Repetition
he
86
First repeated word :: had
54
had
No Repetition
he
23
First repeated word :: had
5

had
No Repetition
he
5
had
No Repetition
he
26

had
No Repetition
he
5
had
No Repetition
he
28

had
No Repetition
he
5
had
No Repetition
he
30

had
No Repetition
he
43
had
No Repetition
he
8

had
No Repetition
he
_8

 

had
_40

Javascript




First repeated word :: had
_67

First repeated word :: had
_68

 

First repeated word :: had
_69
First repeated word :: had
70

had
No Repetition
he
_4

had
No Repetition
he
5
First repeated word :: had
73
had
No Repetition
he
56
First repeated word :: had
75
had
No Repetition
he
9

had
No Repetition
he
5
First repeated word :: had
78
First repeated word :: had
2
First repeated word :: had
5

had
No Repetition
he
5
First repeated word :: had
82
First repeated word :: had
2
First repeated word :: had
5

had
No Repetition
he
5

had
No Repetition
he
5
First repeated word :: had
7

had
No Repetition
he
5
First repeated word :: had
9
First repeated word :: had
90

had
No Repetition
he
5
had
No Repetition
he
4

had
5
had
6

had
5
had
8
had
29
had
No Repetition
he
0
had
No Repetition
he
1

had
5
had
No Repetition
he
4

had
No Repetition
he
4
had
33

had
No Repetition
he
_4

had
5
had
No Repetition
he
8

had
_5

had
5
had
0

had
5
had
2

had
5
had
No Repetition
he
4

had
_5

had
No Repetition
he
4
had
8

had
No Repetition
he
4
had
8// Cpp program to find first repeated word in a string19

________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
he
4
had
2 // Cpp program to find first repeated word in a string24

had
No Repetition
he
_4

had
No Repetition
he
4
First repeated word :: had
5

had
No Repetition
he
4
First repeated word :: had
38
First repeated word :: had
2
First repeated word :: had
5

had
No Repetition
he
_4

had
5
had
No Repetition
he
8

had
No Repetition
he
5
had
No Repetition
he
8

had
No Repetition
he
5

had
No Repetition
he
5// Cpp program to find first repeated word in a string7

had
No Repetition
he
5
had
8// Cpp program to find first repeated word in a string42

#include<bits/stdc++.h>_5

had
No Repetition
he
5
had
8#include<bits/stdc++.h>8
First repeated word :: had
2
had
No Repetition
he
1

had
No Repetition
he
5
had
No Repetition
he
4

had
5using4

had
5// Cpp program to find first repeated word in a string54
First repeated word :: had
2
First repeated word :: had
10

had
5// Cpp program to find first repeated word in a string58

had
No Repetition
he
5
had
No Repetition
he
8

had
No Repetition
he
5
had
2

had
No Repetition
he
5// Cpp program to find first repeated word in a string64
had
No Repetition
he
59
First repeated word :: had
10

had
No Repetition
he
_8

 

// Cpp program to find first repeated word in a string_68

// Cpp program to find first repeated word in a string69

had
No Repetition
he
15
First repeated word :: had
5

// Cpp program to find first repeated word in a string72

had
No Repetition
he
19
First repeated word :: had
5

// Cpp program to find first repeated word in a string75

had
No Repetition
he
23
First repeated word :: had
5

had
No Repetition
he
_26

had
No Repetition
he
_28

had
No Repetition
he
_30

 

// Cpp program to find first repeated word in a string_81

// Cpp program to find first repeated word in a string_82

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




// Cpp program to find first repeated word in a string_83

// Cpp program to find first repeated word in a string_84

// Cpp program to find first repeated word in a string_85

using

had
No Repetition
he
0
had
No Repetition
he
1

 

// Cpp program to find first repeated word in a string_89

// Cpp program to find first repeated word in a string_90

had
No Repetition
he
_4

had
No Repetition
he
5// Cpp program to find first repeated word in a string93

had
No Repetition
he
5// Cpp program to find first repeated word in a string95

had
No Repetition
he
5// Cpp program to find first repeated word in a string97

had
No Repetition
he
5// Cpp program to find first repeated word in a string99

 

had
No Repetition
he
5#include<bits/stdc++.h>01

had
No Repetition
he
5#include<bits/stdc++.h>03

 

had
No Repetition
he
5#include<bits/stdc++.h>05

had
No Repetition
he
5#include<bits/stdc++.h>07

had
No Repetition
he
5
had
No Repetition
he
6
had
No Repetition
he
7#include<bits/stdc++.h>11

 

had
No Repetition
he
5#include<bits/stdc++.h>13

had
No Repetition
he
5#include<bits/stdc++.h>15

 

had
No Repetition
he
5#include<bits/stdc++.h>17 #include<bits/stdc++.h>18
had
No Repetition
he
0
First repeated word :: had
78

had
5
had
8 #include<bits/stdc++.h>23

had
No Repetition
he
4#include<bits/stdc++.h>25#include<bits/stdc++.h>26

had
5
had
2

had
No Repetition
he
4#include<bits/stdc++.h>30

had
No Repetition
he
4#include<bits/stdc++.h>32

had
No Repetition
he
5
had
No Repetition
he
8

 

had
No Repetition
he
5#include<bits/stdc++.h>36

had
No Repetition
he
5#include<bits/stdc++.h>38

 

had
No Repetition
he
5#include<bits/stdc++.h>40

had
No Repetition
he
5#include<bits/stdc++.h>42

had
No Repetition
he
5#include<bits/stdc++.h>44

had
No Repetition
he
5#include<bits/stdc++.h>17 #include<bits/stdc++.h>47
had
No Repetition
he
0
First repeated word :: had
78

had
5
had
No Repetition
he
7 #include<bits/stdc++.h>52

had
5
had
8 #include<bits/stdc++.h>55

had
No Repetition
he
4
had
No Repetition
he
34 #include<bits/stdc++.h>58

had
5
had
No Repetition
he
8

had
No Repetition
he
5
had
No Repetition
he
8

 

had
No Repetition
he
5
had
No Repetition
he
34 #include<bits/stdc++.h>65
First repeated word :: had
5

had
No Repetition
he
_8

 

#include<bits/stdc++.h>_68

had
No Repetition
he
7
had
No Repetition
he
11

had
No Repetition
he
_4

had
No Repetition
he
5#include<bits/stdc++.h>73
had
No Repetition
he
15
First repeated word :: had
10

had
No Repetition
he
5#include<bits/stdc++.h>77

had
No Repetition
he
5
had
8 #include<bits/stdc++.h>80#include<bits/stdc++.h>65
had
No Repetition
he
1

________25

had
No Repetition
he
_1609_______84#include<bits/stdc++.h>85

#include<bits/stdc++.h>86#include<bits/stdc++.h>87

had
No Repetition
he
5
had
2

had
5#include<bits/stdc++.h>84#include<bits/stdc++.h>92
First repeated word :: had
5

had
No Repetition
he
5
had
No Repetition
he
34
had
No Repetition
he
35

had
No Repetition
he
_8

Jawa




#include<bits/stdc++.h>_98

// Cpp program to find first repeated word in a string_84

had
No Repetition
he
38
had
No Repetition
he
39

 

had
No Repetition
he
41 using03

had
No Repetition
he
5

had
No Repetition
he
5// Cpp program to find first repeated word in a string89

had
No Repetition
he
5
had
No Repetition
he
47 using09

had
No Repetition
he
5
had
No Repetition
he
4

had
5// Cpp program to find first repeated word in a string93

had
5// Cpp program to find first repeated word in a string95

had
5// Cpp program to find first repeated word in a string97

had
5// Cpp program to find first repeated word in a string99

had
No Repetition
he
5

had
5using22using23
First repeated word :: had
10

had
No Repetition
he
5

had
5#include<bits/stdc++.h>05

had
5#include<bits/stdc++.h>07

had
5using31
had
No Repetition
he
56 using33

had
No Repetition
he
5

had
5#include<bits/stdc++.h>13

had
5#include<bits/stdc++.h>15

had
No Repetition
he
5

had
5
First repeated word :: had
9
had
0
had
No Repetition
he
7 using44
had
No Repetition
he
82using46

had
No Repetition
he
4
had
8 using49

had
No Repetition
he
05using51
had
No Repetition
he
80
First repeated word :: had
10#include<bits/stdc++.h>26

had
No Repetition
he
4
had
2

had
No Repetition
he
05#include<bits/stdc++.h>30

had
No Repetition
he
05using60
had
No Repetition
he
80using62

had
5
had
No Repetition
he
8

had
No Repetition
he
5

had
5#include<bits/stdc++.h>36

had
5#include<bits/stdc++.h>38

had
No Repetition
he
5

had
5#include<bits/stdc++.h>40

had
5#include<bits/stdc++.h>42

had
5
First repeated word :: had
9
had
0
had
No Repetition
he
7 using44
had
No Repetition
he
82using46

had
No Repetition
he
4
had
No Repetition
he
7 using84

had
No Repetition
he
4
had
8 using87
had
No Repetition
he
80
had
No Repetition
he
92

had
No Repetition
he
05
had
No Repetition
he
34 using92

had
No Repetition
he
4
had
No Repetition
he
8

had
5
had
No Repetition
he
8

had
No Repetition
he
5

had
5
had
No Repetition
he
34 #include<bits/stdc++.h>65
First repeated word :: had
5

had
No Repetition
he
5
had
No Repetition
he
8

had
No Repetition
he
5

had
No Repetition
he
5#include<bits/stdc++.h>68

had
No Repetition
he
5
had
No Repetition
he
40
had
No Repetition
he
47
had
No Repetition
he
2
had
No Repetition
he
011

had
No Repetition
he
5
had
No Repetition
he
4

had
5
had
No Repetition
he
015
had
No Repetition
he
15
First repeated word :: had
5

had
5
had
No Repetition
he
019

had
5
had
8
had
No Repetition
he
022#include<bits/stdc++.h>65
had
No Repetition
he
024

had
No Repetition
he
4
had
No Repetition
he
026#include<bits/stdc++.h>85
had
No Repetition
he
028

had
5
had
2

had
No Repetition
he
4
had
No Repetition
he
026#include<bits/stdc++.h>92
First repeated word :: had
10

had
No Repetition
he
5
had
No Repetition
he
8

had
No Repetition
he
_8

Javascript




had
No Repetition
he
_038

had
No Repetition
he
_4

had
No Repetition
he
5// Cpp program to find first repeated word in a string89

had
No Repetition
he
5
had
No Repetition
he
043

had
No Repetition
he
5
had
No Repetition
he
4

had
5// Cpp program to find first repeated word in a string93

had
5// Cpp program to find first repeated word in a string95

had
5// Cpp program to find first repeated word in a string97

had
5// Cpp program to find first repeated word in a string99

________25______5

had
No Repetition
he
055
had
No Repetition
he
056using23
First repeated word :: had
10

had
_5

had
5
had
No Repetition
he
061

had
5#include<bits/stdc++.h>07

________25______5

had
No Repetition
he
055
had
No Repetition
he
066
had
No Repetition
he
56
had
No Repetition
he
068

had
5#include<bits/stdc++.h>13

had
5
had
No Repetition
he
072

had
5
First repeated word :: had
9
had
No Repetition
he
075

had
5
had
No Repetition
he
4

had
No Repetition
he
4
had
8
had
No Repetition
he
080

had
No Repetition
he
4
had
No Repetition
he
4

had
No Repetition
he
05
had
No Repetition
he
084

had
No Repetition
he
4
had
No Repetition
he
8

had
No Repetition
he
4
had
2

had
No Repetition
he
4
had
No Repetition
he
4

had
No Repetition
he
05
had
No Repetition
he
092

had
No Repetition
he
05
had
No Repetition
he
094

had
No Repetition
he
4
had
No Repetition
he
8

had
5
had
No Repetition
he
8

had
5#include<bits/stdc++.h>36

had
5#include<bits/stdc++.h>38

had
5#include<bits/stdc++.h>40

had
5
had
No Repetition
he
106

had
5
First repeated word :: had
9
had
No Repetition
he
075

had
5
had
No Repetition
he
4

had
No Repetition
he
4
had
No Repetition
he
055 using84

had
No Repetition
he
4
had
8
had
No Repetition
he
117

had
No Repetition
he
4
had
No Repetition
he
4

had
No Repetition
he
05
had
No Repetition
he
34 using92

had
No Repetition
he
4
had
No Repetition
he
8

had
5
had
No Repetition
he
8

had
5
had
No Repetition
he
34 #include<bits/stdc++.h>65
First repeated word :: had
5

had
No Repetition
he
5
had
No Repetition
he
8

had
No Repetition
he
5#include<bits/stdc++.h>68

had
No Repetition
he
5
had
No Repetition
he
136

had
No Repetition
he
5
had
No Repetition
he
4

had
5
had
No Repetition
he
055
had
No Repetition
he
141
had
No Repetition
he
15
First repeated word :: had
5

had
5
had
No Repetition
he
055
had
No Repetition
he
146

had
5
had
8
had
No Repetition
he
149#include<bits/stdc++.h>65
had
No Repetition
he
1

had
5
had
No Repetition
he
4

had
No Repetition
he
4
had
No Repetition
he
155#include<bits/stdc++.h>85
had
No Repetition
he
028

had
5
had
No Repetition
he
8

had
5
had
2

had
5
had
No Repetition
he
4

had
No Repetition
he
4
had
No Repetition
he
155#include<bits/stdc++.h>92
First repeated word :: had
10

had
5
had
No Repetition
he
8

had
No Repetition
he
5
had
No Repetition
he
8

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

  • Karena semua kata dalam kalimat dipisahkan oleh spasi
  • Kita harus membagi kalimat dengan spasi menggunakan split()
  • Kami membagi semua kata dengan spasi dan menyimpannya dalam daftar
  • Gunakan fungsi Penghitung untuk menghitung frekuensi kata
  • Lintasi daftar dan periksa apakah ada kata yang memiliki frekuensi lebih besar dari 1
  • Jika ada maka cetak kata dan hancurkan loop

Penerapan

Python3




had
No Repetition
he
_175

had
No Repetition
he
176
had
No Repetition
he
177
had
No Repetition
he
38
had
No Repetition
he
179

 

had
No Repetition
he
_180

had
No Repetition
he
_181

had
_42
had
No Repetition
he
183

 

had
No Repetition
he
5
had
No Repetition
he
185

had
No Repetition
he
5
had
No Repetition
he
187
had
No Repetition
he
55
had
No Repetition
he
189
had
No Repetition
he
190using23
had
No Repetition
he
024

had
No Repetition
he
5

had
No Repetition
he
5
had
No Repetition
he
195

had
No Repetition
he
5
had
No Repetition
he
197
had
No Repetition
he
55
had
No Repetition
he
199

had
No Repetition
he
5

had
No Repetition
he
5________5______202

had
No Repetition
he
5
First repeated word :: had
9
had
62
had
63
had
No Repetition
he
207

had
No Repetition
he
_54

had
5
had
No Repetition
he
210

had
_5

had
5
had
8
had
No Repetition
he
214
had
No Repetition
he
80________25______76

had
No Repetition
he
4
had
No Repetition
he
218

had
No Repetition
he
4
had
No Repetition
he
34
had
62

 

 

had
No Repetition
he
_222

had
No Repetition
he
223
had
No Repetition
he
55
had
No Repetition
he
225

had
No Repetition
he
51
had
No Repetition
he
227

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

// Cpp program to find first repeated word in a string_85

using

had
No Repetition
he
0
had
No Repetition
he
1

 

had
No Repetition
he
2
had
No Repetition
he
3

had
No Repetition
he
_4

had
No Repetition
he
5
had
No Repetition
he
7
had
No Repetition
he
239
had
No Repetition
he
240

had
No Repetition
he
5
had
No Repetition
he
6
had
No Repetition
he
7
had
No Repetition
he
244

had
5
had
No Repetition
he
246
had
No Repetition
he
247

had
No Repetition
he
5
had
No Repetition
he
249
First repeated word :: had
2
had
No Repetition
he
251
First repeated word :: had
2
First repeated word :: had
5

had
No Repetition
he
5
had
No Repetition
he
7
had
No Repetition
he
256
had
No Repetition
he
257

had
No Repetition
he
258
had
No Repetition
he
259

 

had
No Repetition
he
5
had
No Repetition
he
7
had
No Repetition
he
262

had
5
had
No Repetition
he
264
had
No Repetition
he
265

First repeated word :: had
2
had
No Repetition
he
267

 

had
No Repetition
he
5
had
No Repetition
he
269

had
No Repetition
he
5
had
No Repetition
he
271

had
No Repetition
he
5#include<bits/stdc++.h>17
had
No Repetition
he
274

 

had
5
had
No Repetition
he
276

had
5
had
No Repetition
he
278

had
5
had
8
had
No Repetition
he
281
had
No Repetition
he
0
had
No Repetition
he
283

had
No Repetition
he
4
had
8
had
No Repetition
he
286
had
No Repetition
he
287

had
No Repetition
he
288
had
No Repetition
he
289

had
No Repetition
he
05
had
No Repetition
he
291

had
No Repetition
he
4
had
No Repetition
he
8

had
No Repetition
he
4
had
2
had
No Repetition
he
4
had
No Repetition
he
297

had
No Repetition
he
298
had
No Repetition
he
299

had
No Repetition
he
05
had
8
had
No Repetition
he
302

had
No Repetition
he
303
had
No Repetition
he
304

had
No Repetition
he
303
First repeated word :: had
32

had
No Repetition
he
05
had
No Repetition
he
8

had
No Repetition
he
4
had
No Repetition
he
8

 

had
No Repetition
he
4
had
No Repetition
he
312

had
No Repetition
he
4
First repeated word :: had
38
First repeated word :: had
2
First repeated word :: had
5

had
No Repetition
he
4
had
No Repetition
he
318

had
No Repetition
he
4
had
No Repetition
he
320

had
5
had
No Repetition
he
8

had
5
had
2
had
No Repetition
he
4

had
No Repetition
he
4
had
No Repetition
he
327

had
No Repetition
he
4
had
No Repetition
he
329

had
5
had
No Repetition
he
8

had
No Repetition
he
5
had
No Repetition
he
8

 

had
No Repetition
he
5
had
No Repetition
he
335

had
No Repetition
he
5
had
No Repetition
he
337

had
No Repetition
he
5
had
8
had
No Repetition
he
340
First repeated word :: had
2
had
No Repetition
he
1

had
5#include<bits/stdc++.h>84
had
No Repetition
he
59
had
No Repetition
he
346

had
No Repetition
he
5
had
2

had
5
had
No Repetition
he
350

had
No Repetition
he
_8

 

had
No Repetition
he
7
had
No Repetition
he
11

had
No Repetition
he
_4

had
No Repetition
he
5
had
No Repetition
he
356

had
5
had
No Repetition
he
55
had
No Repetition
he
15
First repeated word :: had
5

had
No Repetition
he
5
had
No Repetition
he
362
had
No Repetition
he
19
First repeated word :: had
5

had
No Repetition
he
5
had
No Repetition
he
366
had
No Repetition
he
23
First repeated word :: had
5

 

had
No Repetition
he
5
had
No Repetition
he
370

had
No Repetition
he
5________5______372

had
No Repetition
he
5
had
No Repetition
he
374

 

had
No Repetition
he
5
had
No Repetition
he
34
had
No Repetition
he
35

had
No Repetition
he
_8

Keluaran

had
No Repetition
he
_

Pendekatan yang Dioptimalkan

Alih-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

  • Buat kamus default dengan nilai awal 0, untuk melacak jumlah kata
  • Ulangi setiap kata dalam kalimat dan tingkatkan jumlah kata itu dengan 1
  • Jika (jumlah kata) > 1, kembalikan kata tersebut
  • Jika hitungan tidak ada kata yang lebih besar dari 1 maka kita berada di luar lingkaran kita lalu kembalikan "Tidak ada kata yang diulang"

Penerapan

Python3




had
No Repetition
he
_379

had
No Repetition
he
176
had
No Repetition
he
177
had
No Repetition
he
38
had
No Repetition
he
383

 

had
_42
had
No Repetition
he
385

 

had
No Repetition
he
5
had
No Repetition
he
387

had
No Repetition
he
5
had
No Repetition
he
389

had
No Repetition
he
5
had
No Repetition
he
391

had
No Repetition
he
5
had
No Repetition
he
393

had
No Repetition
he
5
had
No Repetition
he
395
had
No Repetition
he
55
had
No Repetition
he
397
had
No Repetition
he
398
had
96
had
No Repetition
he
82
had
No Repetition
he
1

 

had
No Repetition
he
5
had
No Repetition
he
403

had
No Repetition
he
5
First repeated word :: had
9
had
62
had
63
had
No Repetition
he
408

 

had
5
had
No Repetition
he
410

had
5
had
No Repetition
he
412

had
5
had
No Repetition
he
414
had
88
had
No Repetition
he
55
had
No Repetition
he
80

 

had
5
had
No Repetition
he
419

had
5
had
No Repetition
he
421

had
5
had
8
had
No Repetition
he
424
had
No Repetition
he
80
had
96

had
No Repetition
he
4
had
No Repetition
he
34
had
62

 

had
No Repetition
he
5
had
No Repetition
he
431

had
No Repetition
he
5
had
No Repetition
he
433

had
No Repetition
he
5
had
No Repetition
he
34
had
No Repetition
he
436

 

 

had
No Repetition
he
_437

had
8
had
No Repetition
he
439
had
No Repetition
he
55
had
No Repetition
he
55
had
No Repetition
he
442
had
96

had
No Repetition
he
5
had
No Repetition
he
445
had
No Repetition
he
55
had
No Repetition
he
15

had
No Repetition
he
5
had
No Repetition
he
51
had
No Repetition
he
450

 

had
No Repetition
he
5
had
No Repetition
he
452

Javascript




First repeated word :: had
_67

 

First repeated word :: had
_69
had
No Repetition
he
455

 

had
No Repetition
he
5
had
No Repetition
he
457

had
No Repetition
he
5
had
No Repetition
he
459

had
No Repetition
he
5
had
No Repetition
he
461

had
No Repetition
he
5
had
No Repetition
he
463

had
No Repetition
he
5
had
No Repetition
he
465
had
No Repetition
he
56
had
No Repetition
he
467

 

had
No Repetition
he
5
had
No Repetition
he
469

had
No Repetition
he
5
First repeated word :: had
9
had
No Repetition
he
472
had
No Repetition
he
0
had
No Repetition
he
474

 

had
5
had
No Repetition
he
476

had
5
had
No Repetition
he
478

had
5
had
8
had
No Repetition
he
481

had
No Repetition
he
4
had
No Repetition
he
483

had
5
had
No Repetition
he
8

had
5
had
2
had
No Repetition
he
488

 

had
5
had
No Repetition
he
490

had
5
had
No Repetition
he
492

had
5
had
8
had
No Repetition
he
495

had
No Repetition
he
4
had
No Repetition
he
34
had
62

had
No Repetition
he
5
had
No Repetition
he
8

 

had
No Repetition
he
5
had
No Repetition
he
502

had
No Repetition
he
5
had
No Repetition
he
504

had
No Repetition
he
5
had
No Repetition
he
34
had
No Repetition
he
436

had
No Repetition
he
_8

 

had
No Repetition
he
509

had
No Repetition
he
510
had
No Repetition
he
15

had
No Repetition
he
_512

 

// Cpp program to find first repeated word in a string_81

 

// Cpp program to find first repeated word in a string_82

Keluaran

had

Kompleksitas waktu. O(M)
Kompleksitas Ruang. O(M)

Pendekatan yang Dioptimalkan 2

Alih-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++




// Cpp program to find first repeated word in a string_83

// Cpp program to find first repeated word in a string_84

// Cpp program to find first repeated word in a string_85

using

had
No Repetition
he
0
had
No Repetition
he
1

 

// Cpp program to find first repeated word in a string_89

// Cpp program to find first repeated word in a string_90

had
No Repetition
he
_4

had
No Repetition
he
5// Cpp program to find first repeated word in a string93

had
No Repetition
he
5// Cpp program to find first repeated word in a string95

had
No Repetition
he
5// Cpp program to find first repeated word in a string97

had
No Repetition
he
5// Cpp program to find first repeated word in a string99

 

had
No Repetition
he
5#include<bits/stdc++.h>01

had
No Repetition
he
5#include<bits/stdc++.h>03

 

had
No Repetition
he
5
had
No Repetition
he
537

had
No Repetition
he
5#include<bits/stdc++.h>07

had
No Repetition
he
5
had
No Repetition
he
541

 

had
No Repetition
he
5#include<bits/stdc++.h>13

had
No Repetition
he
5
had
No Repetition
he
545

 

had
No Repetition
he
5#include<bits/stdc++.h>17 #include<bits/stdc++.h>18
had
No Repetition
he
0
First repeated word :: had
78

had
5
had
No Repetition
he
552

had
5
had
8
had
No Repetition
he
555

had
No Repetition
he
4
had
No Repetition
he
34 #include<bits/stdc++.h>58

had
5
had
No Repetition
he
8

 

had
5#include<bits/stdc++.h>30

had
5
had
No Repetition
he
564

had
No Repetition
he
5
had
No Repetition
he
8

 

had
No Repetition
he
5
had
No Repetition
he
34 #include<bits/stdc++.h>65
First repeated word :: had
5

had
No Repetition
he
_8

 

#include<bits/stdc++.h>_68

had
No Repetition
he
7
had
No Repetition
he
11

had
No Repetition
he
_4

had
No Repetition
he
5#include<bits/stdc++.h>73
had
No Repetition
he
15
First repeated word :: had
10

had
No Repetition
he
5#include<bits/stdc++.h>77

had
No Repetition
he
5
had
8 #include<bits/stdc++.h>80#include<bits/stdc++.h>65
had
No Repetition
he
1

had
5#include<bits/stdc++.h>84#include<bits/stdc++.h>85
had
No Repetition
he
590

#include<bits/stdc++.h>86

had
No Repetition
he
346

had
No Repetition
he
5
had
2

had
5#include<bits/stdc++.h>84#include<bits/stdc++.h>92
First repeated word :: had
5

had
No Repetition
he
5
had
No Repetition
he
34
had
No Repetition
he
35

had
No Repetition
he
_8

Jawa




#include<bits/stdc++.h>_98

// Cpp program to find first repeated word in a string_84

had
No Repetition
he
38
had
No Repetition
he
39

 

had
No Repetition
he
40
had
No Repetition
he
41 using03

had
No Repetition
he
5

had
No Repetition
he
5// Cpp program to find first repeated word in a string89

had
No Repetition
he
5
had
No Repetition
he
47 using09

had
No Repetition
he
5
had
No Repetition
he
4

had
5// Cpp program to find first repeated word in a string93

had
5using22using23
First repeated word :: had
10

had
_5

had
5
had
No Repetition
he
626

had
5
had
No Repetition
he
628
had
No Repetition
he
56
had
No Repetition
he
630

had
_5

had
5
had
No Repetition
he
633

had
5
First repeated word :: had
9
had
0
had
No Repetition
he
7 using44
had
No Repetition
he
82
had
No Repetition
he
640

had
No Repetition
he
_4

had
No Repetition
he
4
had
No Repetition
he
552

had
No Repetition
he
4
had
8
had
No Repetition
he
646

had
No Repetition
he
05
had
No Repetition
he
34 using92

had
No Repetition
he
4
had
No Repetition
he
8

had
No Repetition
he
_4

had
No Repetition
he
4#include<bits/stdc++.h>30

had
No Repetition
he
4
had
No Repetition
he
656

had
5
had
No Repetition
he
8

had
No Repetition
he
5

had
5
had
No Repetition
he
34 #include<bits/stdc++.h>65
First repeated word :: had
5

had
No Repetition
he
5
had
No Repetition
he
8

had
No Repetition
he
5

had
No Repetition
he
5#include<bits/stdc++.h>68

had
No Repetition
he
5
had
No Repetition
he
40
had
No Repetition
he
47
had
No Repetition
he
2
had
No Repetition
he
011

had
No Repetition
he
5
had
No Repetition
he
4

had
5
had
No Repetition
he
015
had
No Repetition
he
15
First repeated word :: had
5

had
5
had
No Repetition
he
019

had
5
had
8
had
No Repetition
he
022#include<bits/stdc++.h>65
had
No Repetition
he
024

had
No Repetition
he
4
had
No Repetition
he
026#include<bits/stdc++.h>85
had
No Repetition
he
028

had
5
had
2

had
No Repetition
he
4
had
No Repetition
he
026#include<bits/stdc++.h>92
First repeated word :: had
10

had
No Repetition
he
5
had
No Repetition
he
8

had
No Repetition
he
_8

Javascript




had
No Repetition
he
_700

// Cpp program to find first repeated word in a string_84

 

had
No Repetition
he
_038

had
No Repetition
he
_4

had
No Repetition
he
5// Cpp program to find first repeated word in a string89

had
No Repetition
he
5
had
No Repetition
he
043

had
No Repetition
he
5
had
No Repetition
he
4

had
5// Cpp program to find first repeated word in a string93

________25______5

had
No Repetition
he
055
had
No Repetition
he
056using23
First repeated word :: had
10

had
_5

had
5
had
No Repetition
he
719

had
5
had
No Repetition
he
055
had
No Repetition
he
722
had
No Repetition
he
56 ________5______724

had
_5

had
5
had
No Repetition
he
727

had
5
First repeated word :: had
9
had
No Repetition
he
075

had
5
had
No Repetition
he
4

had
No Repetition
he
4
had
No Repetition
he
552

had
No Repetition
he
4
had
8
had
No Repetition
he
737

had
No Repetition
he
4
had
No Repetition
he
4

had
No Repetition
he
05
had
No Repetition
he
34 using92

had
No Repetition
he
4
had
No Repetition
he
8

had
No Repetition
he
4#include<bits/stdc++.h>30

had
No Repetition
he
4
had
No Repetition
he
656

had
5
had
No Repetition
he
8

had
5
had
No Repetition
he
34 #include<bits/stdc++.h>65
First repeated word :: had
5

had
No Repetition
he
5
had
No Repetition
he
8

had
No Repetition
he
5

had
No Repetition
he
5#include<bits/stdc++.h>68

had
No Repetition
he
5
had
No Repetition
he
136

had
No Repetition
he
5
had
No Repetition
he
4

had
5
had
No Repetition
he
055
had
No Repetition
he
141
had
No Repetition
he
15
First repeated word :: had
5

had
5
had
No Repetition
he
055
had
No Repetition
he
146

had
5
had
8
had
No Repetition
he
149#include<bits/stdc++.h>65
had
No Repetition
he
1

had
5
had
No Repetition
he
4

had
No Repetition
he
4
had
No Repetition
he
155#include<bits/stdc++.h>85
had
No Repetition
he
028

had
5
had
No Repetition
he
8

had
5
had
2

had
5
had
No Repetition
he
4

had
5
had
No Repetition
he
155#include<bits/stdc++.h>92
First repeated word :: had
10

had
5
had
No Repetition
he
8

had
No Repetition
he
5
had
No Repetition
he
8

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.