Hướng dẫn how do you count non repeated characters in a string python? - làm thế nào để bạn đếm các ký tự không lặp lại trong một chuỗi python?

Cho một chuỗi S bao gồm các chữ cái Latin thường, nhiệm vụ là tìm ký tự không lặp lại đầu tiên trong S.S consisting of lowercase Latin Letters, the task is to find the first non-repeating character in S.

Examples:  

Đầu vào: Đầu ra của Geekforgeeks đầu ra: FexPlanation: Vì ‘F, là ký tự đầu tiên trong chuỗi không lặp lại. “geeksforgeeks”
Output: f
Explanation: As ‘f’ is first character in the string which does not repeat.

Đầu vào: Đầu ra thuật toán của người Viking: AExplanation: Như một ký tự đầu tiên là ký tự đầu tiên trong chuỗi không lặp lại. “algorithm”
Output: a
Explanation: As ‘a’ is first character in the string which does not repeat.

Cách tiếp cận ngây thơ:

Ý tưởng là lặp qua chuỗi và đối với mỗi ký tự, hãy kiểm tra sự xuất hiện của cùng một ký tự trong chuỗi. Nếu số lần xuất hiện của nó là 1 thì hãy trả về ký tự đó. Nếu không, tìm kiếm các ký tự còn lại.1 then return that character. Otherwise, search for the remaining characters.

Lưu ý: Trong Python để tìm sự xuất hiện của một ký tự trong chuỗi có một chuỗi hàm được xây dựng.Count []. In python to find the occurrence of a character in the string there is an In-Built Function string.count[].

Dưới đây là việc thực hiện phương pháp trên:

Python3

string = "geeksforgeeks"

index =

First non-repeating character is f
1
First non-repeating character is f
2

First non-repeating character is f
3=
First non-repeating character is f
5

First non-repeating character is f
6
First non-repeating character is f
7
First non-repeating character is f
8
First non-repeating character is f
9

First non-repeating character is f
0____21
First non-repeating character is f
2==
First non-repeating character is f
2
First non-repeating character is f
6

First non-repeating character is f
7
First non-repeating character is f
3
First non-repeating character is f
9__
First non-repeating character is f
1

First non-repeating character is f
7
First non-repeating character is f
3

First non-repeating character is f
0
First non-repeating character is f
5
First non-repeating character is f
6

First non-repeating character is f
7index
First non-repeating character is f
9=
First non-repeating character is f
2

First non-repeating character is f
1 index =
First non-repeating character is f
2
First non-repeating character is f
6

First non-repeating character is f
0
First non-repeating character is f
9
First non-repeating character is f
0
First non-repeating character is f
1
First non-repeating character is f
2

First non-repeating character is f
5
First non-repeating character is f
6

First non-repeating character is f
0
First non-repeating character is f
9
First non-repeating character is f
0
First non-repeating character is f
8
First non-repeating character is f
9

Đầu ra

First non-repeating character is f

Độ phức tạp về thời gian: O [N2], Traverse qua chuỗi cho mỗi ký tự trong chuỗi kích thước N.AUXILIARY SPACE: O [1] O[N2], Traverse over the string for every character in the string of size N.
Auxiliary Space: O[1]

Ký tự không lặp lại đầu tiên bằng cách sử dụng hàm chuỗi tìm []:

Ý tưởng là tìm kiếm ký tự hiện tại trong chuỗi ngay sau lần xuất hiện đầu tiên trong chuỗi. Nếu ký tự được tìm thấy trong chuỗi còn lại thì hãy trả về ký tự đó. & NBSP;

Việc tìm kiếm được thực hiện bằng hàm find [] được xây dựng.in-built find[] function.

Dưới đây là việc thực hiện phương pháp này.

C++

string 0

string 1 string 2 string 3

string 4 string 5

string 6

First non-repeating character is f
0
First non-repeating character is f
6
First non-repeating character is f
0=0 =1

First non-repeating character is f
7
First non-repeating character is f
1 =4

=5=6

=5=8=9

"geeksforgeeks"0"geeksforgeeks"1

=5"geeksforgeeks"3"geeksforgeeks"4

First non-repeating character is f
7"geeksforgeeks"6

First non-repeating character is f
0"geeksforgeeks"6

First non-repeating character is f
0=8index 1

=5index 3"geeksforgeeks"4

First non-repeating character is f
0"geeksforgeeks"3"geeksforgeeks"4

"geeksforgeeks"6

=0

First non-repeating character is f
00

string 6

First non-repeating character is f
0
First non-repeating character is f
03"geeksforgeeks""geeksforgeeks"4

First non-repeating character is f
0
First non-repeating character is f
07

"geeksforgeeks"6

Python3

First non-repeating character is f
09
First non-repeating character is f
10

First non-repeating character is f
0
First non-repeating character is f
6
First non-repeating character is f
7
First non-repeating character is f
8
First non-repeating character is f
15

Các

=5

First non-repeating character is f
9
First non-repeating character is f
0
First non-repeating character is f
8
First non-repeating character is f
31

=5

First non-repeating character is f
3

First non-repeating character is f
0"geeksforgeeks"3

First non-repeating character is f
36=
First non-repeating character is f
38

First non-repeating character is f
39

C#

string 1

First non-repeating character is f
41

First non-repeating character is f
42
First non-repeating character is f
43
First non-repeating character is f
44
First non-repeating character is f
45

First non-repeating character is f
0
First non-repeating character is f
42
First non-repeating character is f
43 string 4
First non-repeating character is f
50
First non-repeating character is f
51
First non-repeating character is f
52

First non-repeating character is f
0string 6

First non-repeating character is f
7
First non-repeating character is f
6
First non-repeating character is f
0=0
First non-repeating character is f
59

=5

First non-repeating character is f
1
First non-repeating character is f
62

First non-repeating character is f
63
First non-repeating character is f
64

First non-repeating character is f
63
First non-repeating character is f
66

First non-repeating character is f
67=9

First non-repeating character is f
67
First non-repeating character is f
70

First non-repeating character is f
63
First non-repeating character is f
3"geeksforgeeks"4

=5"geeksforgeeks"6

First non-repeating character is f
7"geeksforgeeks"6

First non-repeating character is f
7"geeksforgeeks"3"geeksforgeeks"4

First non-repeating character is f
0"geeksforgeeks"6

First non-repeating character is f
0
First non-repeating character is f
84
First non-repeating character is f
43 string 4
First non-repeating character is f
87

First non-repeating character is f
0string 6

First non-repeating character is f
7
First non-repeating character is f
51
First non-repeating character is f
92"geeksforgeeks""geeksforgeeks"4

First non-repeating character is f
7
First non-repeating character is f
07

First non-repeating character is f
0"geeksforgeeks"6

"geeksforgeeks"6

JavaScript

First non-repeating character is f
00

First non-repeating character is f
01
First non-repeating character is f
02

First non-repeating character is f
03
First non-repeating character is f
6
First non-repeating character is f
05

First non-repeating character is f
03string 6

First non-repeating character is f
08
First non-repeating character is f
1
First non-repeating character is f
10

First non-repeating character is f
08string 6

First non-repeating character is f
13
First non-repeating character is f
14

First non-repeating character is f
13
First non-repeating character is f
3

First non-repeating character is f
08"geeksforgeeks"6

First non-repeating character is f
03"geeksforgeeks"6

First non-repeating character is f
03"geeksforgeeks"3

"geeksforgeeks"6

First non-repeating character is f
24
First non-repeating character is f
38

First non-repeating character is f
39

First non-repeating character is f
27

Đầu ra

First non-repeating character is f

Độ phức tạp về thời gian: O [N2], Traverse qua chuỗi cho mỗi ký tự trong chuỗi kích thước N.AUXILIARY SPACE: O [1]O[N2]
Auxiliary Space: O[1]

Ký tự không lặp lại đầu tiên bằng cách sử dụng hàm chuỗi tìm []:HashMap and two string traversals.

Ý tưởng là tìm kiếm ký tự hiện tại trong chuỗi ngay sau lần xuất hiện đầu tiên trong chuỗi. Nếu ký tự được tìm thấy trong chuỗi còn lại thì hãy trả về ký tự đó. & NBSP;unit frequency. This task could be done efficiently using a hash_map which will map the character to their respective frequencies and in which we can simultaneously update the frequency of any character we come across in constant time.

Việc tìm kiếm được thực hiện bằng hàm find [] được xây dựng.

  • Dưới đây là việc thực hiện phương pháp này.hash_map that will map the character to their respective frequencies.
  • string 1 string 2 string 3
  • string 4 string 5hash_map.
  • First non-repeating character is f
    0
    First non-repeating character is f
    6
    First non-repeating character is f
    0=0 =1frequency=1.
  • First non-repeating character is f
    7
    First non-repeating character is f
    1 =4frequency>1 continue the traversal.
  • First non-repeating character is f
    0"geeksforgeeks"6break the loop and print the current character as the answer.

=0

First non-repeating character is f
00

C++

First non-repeating character is f
28

string 1 string 2 string 3

First non-repeating character is f
32

=0

First non-repeating character is f
34
First non-repeating character is f
35
First non-repeating character is f
36

string 6

First non-repeating character is f
0=0
First non-repeating character is f
40=0
First non-repeating character is f
42
First non-repeating character is f
43
First non-repeating character is f
0
First non-repeating character is f
45
First non-repeating character is f
0=0
First non-repeating character is f
48

First non-repeating character is f
0=0
First non-repeating character is f
51

First non-repeating character is f
0
First non-repeating character is f
6
First non-repeating character is f
54

First non-repeating character is f
7
First non-repeating character is f
56

First non-repeating character is f
0"geeksforgeeks"3
First non-repeating character is f
59

"geeksforgeeks"6

=0

First non-repeating character is f
62
First non-repeating character is f
35
First non-repeating character is f
36

string 6

First non-repeating character is f
0=0
First non-repeating character is f
68

First non-repeating character is f
0=0
First non-repeating character is f
71

First non-repeating character is f
0
First non-repeating character is f
6
First non-repeating character is f
74

First non-repeating character is f
7
First non-repeating character is f
1
First non-repeating character is f
77

=5

First non-repeating character is f
79

=5

First non-repeating character is f
3"geeksforgeeks"4

First non-repeating character is f
7"geeksforgeeks"6

First non-repeating character is f
0"geeksforgeeks"6

First non-repeating character is f
0
First non-repeating character is f
88
First non-repeating character is f
89

First non-repeating character is f
0"geeksforgeeks"3
First non-repeating character is f
92

"geeksforgeeks"6

=0

First non-repeating character is f
00

string 6

First non-repeating character is f
0
First non-repeating character is f
35
First non-repeating character is f
99"geeksforgeeks""geeksforgeeks"4

First non-repeating character is f
0=0
First non-repeating character is f
04

First non-repeating character is f
0
First non-repeating character is f
1
First non-repeating character is f
07

First non-repeating character is f
7=8index 1

First non-repeating character is f
63index 3"geeksforgeeks"4

First non-repeating character is f
0
First non-repeating character is f
5

First non-repeating character is f
7=8=9

First non-repeating character is f
19
First non-repeating character is f
20

First non-repeating character is f
0
First non-repeating character is f
22
First non-repeating character is f
23

First non-repeating character is f
0"geeksforgeeks"3
First non-repeating character is f
26

"geeksforgeeks"6

C

First non-repeating character is f
28

First non-repeating character is f
29

First non-repeating character is f
32

=0

First non-repeating character is f
34
First non-repeating character is f
35
First non-repeating character is f
36

string 6

First non-repeating character is f
0=0
First non-repeating character is f
40=0
First non-repeating character is f
42
First non-repeating character is f
43
First non-repeating character is f
0
First non-repeating character is f
45
First non-repeating character is f
0=0
First non-repeating character is f
48

First non-repeating character is f
0=0
First non-repeating character is f
51

First non-repeating character is f
0
First non-repeating character is f
6
First non-repeating character is f
54

First non-repeating character is f
7
First non-repeating character is f
56

First non-repeating character is f
0"geeksforgeeks"3
First non-repeating character is f
59

"geeksforgeeks"6

=0

First non-repeating character is f
62
First non-repeating character is f
35
First non-repeating character is f
36

string 6

First non-repeating character is f
0=0
First non-repeating character is f
68

First non-repeating character is f
0=0
First non-repeating character is f
71

First non-repeating character is f
0
First non-repeating character is f
6
First non-repeating character is f
74

First non-repeating character is f
7
First non-repeating character is f
1
First non-repeating character is f
77

=5

First non-repeating character is f
79

=5

First non-repeating character is f
3"geeksforgeeks"4

First non-repeating character is f
7"geeksforgeeks"6

First non-repeating character is f
0"geeksforgeeks"6

First non-repeating character is f
0
First non-repeating character is f
88
First non-repeating character is f
89

First non-repeating character is f
0"geeksforgeeks"3
First non-repeating character is f
92

"geeksforgeeks"6

=0

First non-repeating character is f
00

string 6

First non-repeating character is f
0
First non-repeating character is f
35
First non-repeating character is f
99"geeksforgeeks""geeksforgeeks"4

First non-repeating character is f
0=0
First non-repeating character is f
04

First non-repeating character is f
0
First non-repeating character is f
1
First non-repeating character is f
07

First non-repeating character is f
7
First non-repeating character is f
07
First non-repeating character is f
0index 1

First non-repeating character is f
10index 3
First non-repeating character is f
12

First non-repeating character is f
0
First non-repeating character is f
5

First non-repeating character is f
7
First non-repeating character is f
07
First non-repeating character is f
0
First non-repeating character is f
18
First non-repeating character is f
19

First non-repeating character is f
10
First non-repeating character is f
21

First non-repeating character is f
0
First non-repeating character is f
22
First non-repeating character is f
23

First non-repeating character is f
0"geeksforgeeks"3
First non-repeating character is f
26

"geeksforgeeks"6

C

Java

First non-repeating character is f
29
First non-repeating character is f
30

First non-repeating character is f
44
First non-repeating character is f
45

First non-repeating character is f
0
First non-repeating character is f
43
First non-repeating character is f
35 =0
First non-repeating character is f
37
First non-repeating character is f
38"geeksforgeeks"4

First non-repeating character is f
0
First non-repeating character is f
43
First non-repeating character is f
35
First non-repeating character is f
43
First non-repeating character is f
44
First non-repeating character is f
35
First non-repeating character is f
46

First non-repeating character is f
0string 6

First non-repeating character is f
0
First non-repeating character is f
43 string 4
First non-repeating character is f
50

=5

First non-repeating character is f
61

First non-repeating character is f
0"geeksforgeeks"6

First non-repeating character is f
7
First non-repeating character is f
6
First non-repeating character is f
0=0
First non-repeating character is f
57
First non-repeating character is f
58
First non-repeating character is f
59

First non-repeating character is f
0string 6

First non-repeating character is f
7
First non-repeating character is f
71

First non-repeating character is f
0
First non-repeating character is f
43 =0
First non-repeating character is f
67

First non-repeating character is f
7=0
First non-repeating character is f
74
First non-repeating character is f
2
First non-repeating character is f
76

First non-repeating character is f
7
First non-repeating character is f
6
First non-repeating character is f
79
First non-repeating character is f
58
First non-repeating character is f
81

First non-repeating character is f
63
First non-repeating character is f
79

=5

First non-repeating character is f
1
First non-repeating character is f
84
First non-repeating character is f
2
First non-repeating character is f
86

=5"geeksforgeeks"6

First non-repeating character is f
7"geeksforgeeks"6

First non-repeating character is f
63
First non-repeating character is f
3"geeksforgeeks"4

First non-repeating character is f
0"geeksforgeeks"6

First non-repeating character is f
7"geeksforgeeks"3
First non-repeating character is f
92

First non-repeating character is f
0string 6

First non-repeating character is f
7
First non-repeating character is f
09"geeksforgeeks""geeksforgeeks"4

First non-repeating character is f
0
First non-repeating character is f
42
First non-repeating character is f
43 string 4
First non-repeating character is f
05

First non-repeating character is f
7
First non-repeating character is f
16

=5

First non-repeating character is f
18
First non-repeating character is f
2

First non-repeating character is f
63
First non-repeating character is f
21
First non-repeating character is f
22

First non-repeating character is f
23
First non-repeating character is f
24
First non-repeating character is f
25

First non-repeating character is f
63
First non-repeating character is f
27=9

First non-repeating character is f
23
First non-repeating character is f
30

First non-repeating character is f
0"geeksforgeeks"6

"geeksforgeeks"6

Python3

First non-repeating character is f
7=0
First non-repeating character is f
04

First non-repeating character is f
34=
First non-repeating character is f
38

First non-repeating character is f
09
First non-repeating character is f
38

First non-repeating character is f
0
First non-repeating character is f
40=
First non-repeating character is f
42
First non-repeating character is f
58
First non-repeating character is f
44444545
First non-repeating character is f
46

First non-repeating character is f
0
First non-repeating character is f
6
First non-repeating character is f
7
First non-repeating character is f
8
First non-repeating character is f
9

First non-repeating character is f
7
First non-repeating character is f
53
First non-repeating character is f
54
First non-repeating character is f
5555
First non-repeating character is f
9=

First non-repeating character is f
0"geeksforgeeks"3
First non-repeating character is f
61

First non-repeating character is f
09
First non-repeating character is f
63

First non-repeating character is f
0
First non-repeating character is f
40=
First non-repeating character is f
67

First non-repeating character is f
0index __
First non-repeating character is f
1
First non-repeating character is f
2

First non-repeating character is f
0
First non-repeating character is f
40=
First non-repeating character is f
42
First non-repeating character is f
58
First non-repeating character is f
44444545
First non-repeating character is f
46

First non-repeating character is f
0
First non-repeating character is f
6
First non-repeating character is f
7
First non-repeating character is f
8
First non-repeating character is f
9

First non-repeating character is f
7
First non-repeating character is f
53
First non-repeating character is f
54
First non-repeating character is f
5555
First non-repeating character is f
9=

=5

First non-repeating character is f
3

First non-repeating character is f
0"geeksforgeeks"3
First non-repeating character is f
61

First non-repeating character is f
09
First non-repeating character is f
63

First non-repeating character is f
0
First non-repeating character is f
40=
First non-repeating character is f
67

First non-repeating character is f
0index __
First non-repeating character is f
1
First non-repeating character is f
2

First non-repeating character is f
0
First non-repeating character is f
74=
First non-repeating character is f
58

First non-repeating character is f
0
First non-repeating character is f
9
First non-repeating character is f
0
First non-repeating character is f
1
First non-repeating character is f
2

First non-repeating character is f
5
First non-repeating character is f
6

First non-repeating character is f
0
First non-repeating character is f
9
First non-repeating character is f
0
First non-repeating character is f
8string 28

C#

First non-repeating character is f
7
First non-repeating character is f
1
First non-repeating character is f
53
First non-repeating character is f
54
First non-repeating character is f
555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555====
First non-repeating character is f
2
First non-repeating character is f
6

=5index =

First non-repeating character is f
94

First non-repeating character is f
29
First non-repeating character is f
30

First non-repeating character is f
44
First non-repeating character is f
45

First non-repeating character is f
0
First non-repeating character is f
43
First non-repeating character is f
35 =0
First non-repeating character is f
37
First non-repeating character is f
38"geeksforgeeks"4

First non-repeating character is f
0
First non-repeating character is f
43
First non-repeating character is f
35
First non-repeating character is f
43
First non-repeating character is f
44
First non-repeating character is f
35
First non-repeating character is f
46

First non-repeating character is f
0string 6

First non-repeating character is f
0
First non-repeating character is f
43 string 4
First non-repeating character is f
50

=5string 60

First non-repeating character is f
0"geeksforgeeks"6

First non-repeating character is f
0
First non-repeating character is f
43 =0
First non-repeating character is f
62
First non-repeating character is f
51 string 51

First non-repeating character is f
0string 6

First non-repeating character is f
7
First non-repeating character is f
71

First non-repeating character is f
7=0
First non-repeating character is f
71

First non-repeating character is f
7
First non-repeating character is f
6 string 78

=5

First non-repeating character is f
1 string 81

First non-repeating character is f
63
First non-repeating character is f
79

First non-repeating character is f
63
First non-repeating character is f
3"geeksforgeeks"4

=5"geeksforgeeks"6

First non-repeating character is f
7"geeksforgeeks"6

First non-repeating character is f
7"geeksforgeeks"3
First non-repeating character is f
92

First non-repeating character is f
0"geeksforgeeks"6

First non-repeating character is f
0
First non-repeating character is f
42
First non-repeating character is f
43 string 4
First non-repeating character is f
87

First non-repeating character is f
0string 6

First non-repeating character is f
7
First non-repeating character is f
51 =05"geeksforgeeks""geeksforgeeks"4

First non-repeating character is f
7=0
First non-repeating character is f
04

First non-repeating character is f
7=12

=5=14

First non-repeating character is f
63
First non-repeating character is f
21=17

First non-repeating character is f
23
First non-repeating character is f
24=20

First non-repeating character is f
23
First non-repeating character is f
24
First non-repeating character is f
25

First non-repeating character is f
63
First non-repeating character is f
27=26

First non-repeating character is f
23
First non-repeating character is f
24=29 =30

First non-repeating character is f
0"geeksforgeeks"6

"geeksforgeeks"6

PHP

=34

=35 =36

=37==39=40

First non-repeating character is f
01 string 49=43
First non-repeating character is f
2

string 6

First non-repeating character is f
0=47 =37"geeksforgeeks"4

First non-repeating character is f
0
First non-repeating character is f
6
First non-repeating character is f
0=53 =54

____ ____7555=53 =57=58

First non-repeating character is f
0=43=61=53=63

Các

"geeksforgeeks"6

First non-repeating character is f
01
First non-repeating character is f
62=43
First non-repeating character is f
2

string 6

First non-repeating character is f
0=47 =37"geeksforgeeks"4

First non-repeating character is f
0string 49=43
First non-repeating character is f
12

First non-repeating character is f
0
First non-repeating character is f
6
First non-repeating character is f
0=53 =54

First non-repeating character is f
0
First non-repeating character is f
6
First non-repeating character is f
0=53 =54

____ ____7555=53 =57=58

First non-repeating character is f
0=43=61=53=63

First non-repeating character is f
0string 6

Các

First non-repeating character is f
7string 6

First non-repeating character is f
01
First non-repeating character is f
62=43
First non-repeating character is f
2

First non-repeating character is f
0=86 =87

First non-repeating character is f
7"geeksforgeeks"6

First non-repeating character is f
0"geeksforgeeks"6

First non-repeating character is f
7
First non-repeating character is f
1
First non-repeating character is f
0=37=66=43
First non-repeating character is f
42=53"geeksforgeeks"12

"geeksforgeeks"6

=5

First non-repeating character is f
3"geeksforgeeks"4

"geeksforgeeks"3 =86"geeksforgeeks"4

=43 "geeksforgeeks"17"geeksforgeeks""geeksforgeeks"4

"geeksforgeeks"46"geeksforgeeks"47"geeksforgeeks"4

First non-repeating character is f
5

=86 "geeksforgeeks"36=43

First non-repeating character is f
12

=5"geeksforgeeks"54"geeksforgeeks"45

First non-repeating character is f
10=43
First non-repeating character is f
42=86"geeksforgeeks"60

"geeksforgeeks"61

First non-repeating character is f
1
First non-repeating character is f
0=86 "geeksforgeeks"42

First non-repeating character is f

"geeksforgeeks"43 "geeksforgeeks"44 "geeksforgeeks"45O[N], Traversing over the string of size N
Auxiliary Space: O[256], To store the frequency of the characters in the string.

"geeksforgeeks"43 "geeksforgeeks"51"geeksforgeeks"45HashMap and single string traversal

Đầu ra

Độ phức tạp về thời gian: O [n], đi qua chuỗi không gian kích thước nauxiliary: O [256], để lưu trữ tần số của các ký tự trong chuỗi.

  • Ký tự không lặp lại đầu tiên bằng cách sử dụng hashmap và chuỗi chuỗi đơncount_array which will have two fields namely frequency, first occurrence of a character.
  • Ý tưởng là tạo ra một mảng đếm thay vì hash_map có số lượng ký tự tối đa [256]. Tăng cường mảng đếm bằng cách lưu trữ không chỉ tính mà còn chỉ là chỉ số của lần đầu tiên một nhân vật gặp phải. Vì vậy, khi nói đến việc tìm kiếm người không phóng thích đầu tiên, chỉ cần quét mảng đếm, thay vì chuỗi.count_array is 256.
  • Thực hiện theo các bước dưới đây để giải quyết vấn đề:
  • Tạo một Count_array sẽ có hai trường là tần số, lần đầu tiên xuất hiện của một ký tự.
  • Kích thước của Count_array là 256.
  • Traverse Chuỗi đã cho bằng một con trỏ.count_array[] and find the character with the least first occurrence value and frequency value as unity.
  • Tăng số lượng các ký tự hiện tại và cập nhật sự xuất hiện.

Bây giờ, ở đây, một sản phẩm bắt, mảng sẽ chứa sự xuất hiện đầu tiên hợp lệ của nhân vật có tần suất thống nhất. Nếu không, sự xuất hiện đầu tiên tiếp tục cập nhật.

C++

string 0

Bây giờ đi qua Count_array [] và tìm ký tự có giá trị xuất hiện và giá trị tần số ít nhất là thống nhất.

First non-repeating character is f
32

Trả lại nhân vật đó.

string 6

First non-repeating character is f
0"geeksforgeeks"73=0"geeksforgeeks"75=0"geeksforgeeks"77

Dưới đây là việc thực hiện phương pháp trên.

First non-repeating character is f
7"geeksforgeeks"84

First non-repeating character is f
7"geeksforgeeks"86

First non-repeating character is f
0"geeksforgeeks"6

string 1 string 2 string 3

=0

First non-repeating character is f
62
First non-repeating character is f
35
First non-repeating character is f
36

First non-repeating character is f
0
First non-repeating character is f
6
First non-repeating character is f
0=0 "geeksforgeeks"82

=5index 01

First non-repeating character is f
0=0 "geeksforgeeks"91

"geeksforgeeks"6

First non-repeating character is f
0
First non-repeating character is f
6
First non-repeating character is f
0=0 "geeksforgeeks"96

string 6

First non-repeating character is f
7
First non-repeating character is f
1 "geeksforgeeks"99

First non-repeating character is f
0"geeksforgeeks"3 index 04

=0

First non-repeating character is f
00

First non-repeating character is f
7
First non-repeating character is f
07
First non-repeating character is f
0index 23

First non-repeating character is f
10index 25
First non-repeating character is f
12

First non-repeating character is f
0
First non-repeating character is f
5

First non-repeating character is f
7
First non-repeating character is f
07
First non-repeating character is f
0=26

First non-repeating character is f
10index 34
First non-repeating character is f
19

First non-repeating character is f
10
First non-repeating character is f
21

First non-repeating character is f
0
First non-repeating character is f
35
First non-repeating character is f
99"geeksforgeeks""geeksforgeeks"4

"geeksforgeeks"6

First non-repeating character is f
0=0
First non-repeating character is f
04

index 42

First non-repeating character is f
28

First non-repeating character is f
29

First non-repeating character is f
32

First non-repeating character is f
0
First non-repeating character is f
1 index 19

First non-repeating character is f
0"geeksforgeeks"3
First non-repeating character is f
26

C

index 54

index 46 index 47

string 6

First non-repeating character is f
0=0
First non-repeating character is f
59

First non-repeating character is f
0=0
First non-repeating character is f
92

First non-repeating character is f
0=0
First non-repeating character is f
51

First non-repeating character is f
0
First non-repeating character is f
6
First non-repeating character is f
74

First non-repeating character is f
7index 79

First non-repeating character is f
7
First non-repeating character is f
1 index 82

=5index 84

First non-repeating character is f
0"geeksforgeeks"6

First non-repeating character is f
0"geeksforgeeks"3
First non-repeating character is f
59

"geeksforgeeks"6

=0

First non-repeating character is f
62
First non-repeating character is f
35
First non-repeating character is f
36

string 6

First non-repeating character is f
0index 46 index 98

First non-repeating character is f
0=0
First non-repeating character is f
001

First non-repeating character is f
0
First non-repeating character is f
6
First non-repeating character is f
004

First non-repeating character is f
7
First non-repeating character is f
1
First non-repeating character is f
007

=5

First non-repeating character is f
009

First non-repeating character is f
0"geeksforgeeks"6

First non-repeating character is f
0
First non-repeating character is f
88
First non-repeating character is f
89

First non-repeating character is f
0"geeksforgeeks"3
First non-repeating character is f
017

"geeksforgeeks"6

=0

First non-repeating character is f
00

string 6

First non-repeating character is f
0
First non-repeating character is f
35
First non-repeating character is f
99"geeksforgeeks""geeksforgeeks"4

First non-repeating character is f
0=0
First non-repeating character is f
04

First non-repeating character is f
0
First non-repeating character is f
1 index 19

First non-repeating character is f
7
First non-repeating character is f
07
First non-repeating character is f
0"geeksforgeeks"44

First non-repeating character is f
10"geeksforgeeks"47
First non-repeating character is f
12

First non-repeating character is f
0
First non-repeating character is f
5

First non-repeating character is f
7
First non-repeating character is f
07
First non-repeating character is f
0=26

First non-repeating character is f
10index 34
First non-repeating character is f
19

First non-repeating character is f
10
First non-repeating character is f
21

First non-repeating character is f
0
First non-repeating character is f
22
First non-repeating character is f
23

First non-repeating character is f
0"geeksforgeeks"3
First non-repeating character is f
26

"geeksforgeeks"6

Java

First non-repeating character is f
29
First non-repeating character is f
059

First non-repeating character is f
44
First non-repeating character is f
061

First non-repeating character is f
0=0
First non-repeating character is f
064

First non-repeating character is f
0
First non-repeating character is f
42
First non-repeating character is f
067=0
First non-repeating character is f
069

First non-repeating character is f
0string 6

First non-repeating character is f
7
First non-repeating character is f
073
First non-repeating character is f
074
First non-repeating character is f
2"geeksforgeeks"4

First non-repeating character is f
7
First non-repeating character is f
073
First non-repeating character is f
079

First non-repeating character is f
0"geeksforgeeks"6

First non-repeating character is f
0
First non-repeating character is f
42 string 4
First non-repeating character is f
085
First non-repeating character is f
073
First non-repeating character is f
087

"geeksforgeeks"6

First non-repeating character is f
44
First non-repeating character is f
45

First non-repeating character is f
0
First non-repeating character is f
43
First non-repeating character is f
35 =0
First non-repeating character is f
37
First non-repeating character is f
38"geeksforgeeks"4

First non-repeating character is f
0
First non-repeating character is f
43
First non-repeating character is f
100

First non-repeating character is f
7"geeksforgeeks"17
First non-repeating character is f
44
First non-repeating character is f
104

First non-repeating character is f
0
First non-repeating character is f
43 string 4
First non-repeating character is f
50

First non-repeating character is f
0string 6

First non-repeating character is f
7
First non-repeating character is f
6
First non-repeating character is f
0=0
First non-repeating character is f
57
First non-repeating character is f
58
First non-repeating character is f
81

=5

First non-repeating character is f
1
First non-repeating character is f
120

First non-repeating character is f
63
First non-repeating character is f
122

=5"geeksforgeeks"6

=5

First non-repeating character is f
5 string 6

First non-repeating character is f
63
First non-repeating character is f
129
First non-repeating character is f
44
First non-repeating character is f
131

=5"geeksforgeeks"6

First non-repeating character is f
7"geeksforgeeks"6

First non-repeating character is f
0"geeksforgeeks"6

First non-repeating character is f
0
First non-repeating character is f
43 =0
First non-repeating character is f
67

First non-repeating character is f
0string 6

First non-repeating character is f
7
First non-repeating character is f
71

First non-repeating character is f
7=0
First non-repeating character is f
148

First non-repeating character is f
7
First non-repeating character is f
6
First non-repeating character is f
151

First non-repeating character is f
19
First non-repeating character is f
153

=5=0

First non-repeating character is f
156

=5=0

First non-repeating character is f
159

=5

First non-repeating character is f
1
First non-repeating character is f
162
First non-repeating character is f
2
First non-repeating character is f
164

First non-repeating character is f
63
First non-repeating character is f
166

=5"geeksforgeeks"6

First non-repeating character is f
7"geeksforgeeks"6

First non-repeating character is f
7"geeksforgeeks"3
First non-repeating character is f
017

First non-repeating character is f
0"geeksforgeeks"6

First non-repeating character is f
0
First non-repeating character is f
42
First non-repeating character is f
43 string 4
First non-repeating character is f
05

First non-repeating character is f
0string 6

First non-repeating character is f
7
First non-repeating character is f
09"geeksforgeeks""geeksforgeeks"4

First non-repeating character is f
7=0
First non-repeating character is f
04

First non-repeating character is f
7
First non-repeating character is f
16

=5

First non-repeating character is f
193

First non-repeating character is f
63
First non-repeating character is f
21
First non-repeating character is f
196

First non-repeating character is f
23
First non-repeating character is f
24
First non-repeating character is f
199

First non-repeating character is f
63
First non-repeating character is f
27=9

First non-repeating character is f
23
First non-repeating character is f
30

First non-repeating character is f
0"geeksforgeeks"6

"geeksforgeeks"6

Python3

First non-repeating character is f
29
First non-repeating character is f
209

First non-repeating character is f
34=
First non-repeating character is f
38

First non-repeating character is f
09
First non-repeating character is f
62
First non-repeating character is f
215
First non-repeating character is f
216

First non-repeating character is f
0
First non-repeating character is f
218=
First non-repeating character is f
220
First non-repeating character is f
6
First non-repeating character is f
7
First non-repeating character is f
8

First non-repeating character is f
0
First non-repeating character is f
6
First non-repeating character is f
7
First non-repeating character is f
8
First non-repeating character is f
224
First non-repeating character is f
231

First non-repeating character is f
7
First non-repeating character is f
233=
First non-repeating character is f
42
First non-repeating character is f
58"geeksforgeeks"75
First non-repeating character is f
58
First non-repeating character is f
239

First non-repeating character is f
0
First non-repeating character is f
6
First non-repeating character is f
7
First non-repeating character is f
8
First non-repeating character is f
224
First non-repeating character is f
0
First non-repeating character is f
246
First non-repeating character is f
0
First non-repeating character is f
215
First non-repeating character is f
249

Các

Các

First non-repeating character is f
0
First non-repeating character is f
272=
First non-repeating character is f
274

First non-repeating character is f
0
First non-repeating character is f
6
First non-repeating character is f
7
First non-repeating character is f
8
First non-repeating character is f
224
First non-repeating character is f
231

First non-repeating character is f
7
First non-repeating character is f
233=
First non-repeating character is f
42
First non-repeating character is f
58"geeksforgeeks"75
First non-repeating character is f
58
First non-repeating character is f
239

First non-repeating character is f
0
First non-repeating character is f
6
First non-repeating character is f
7
First non-repeating character is f
8
First non-repeating character is f
224
First non-repeating character is f
0
First non-repeating character is f
246
First non-repeating character is f
0
First non-repeating character is f
215
First non-repeating character is f
249

Các

Các

First non-repeating character is f
0
First non-repeating character is f
272=
First non-repeating character is f
274

Các

First non-repeating character is f
0
First non-repeating character is f
9
First non-repeating character is f
0
First non-repeating character is f
1
First non-repeating character is f
2

First non-repeating character is f
5
First non-repeating character is f
6

=5

First non-repeating character is f
272=
First non-repeating character is f
293
First non-repeating character is f
294
First non-repeating character is f
2
First non-repeating character is f
296

C#

First non-repeating character is f
0"geeksforgeeks"3
First non-repeating character is f
299

First non-repeating character is f
215 = "geeksforgeeks"

First non-repeating character is f
44
First non-repeating character is f
061

First non-repeating character is f
0=0
First non-repeating character is f
064

First non-repeating character is f
0
First non-repeating character is f
42
First non-repeating character is f
067=0
First non-repeating character is f
069

First non-repeating character is f
0string 6

First non-repeating character is f
7
First non-repeating character is f
073
First non-repeating character is f
346

First non-repeating character is f
7
First non-repeating character is f
073
First non-repeating character is f
079

First non-repeating character is f
0"geeksforgeeks"6

First non-repeating character is f
0
First non-repeating character is f
42 string 4
First non-repeating character is f
085
First non-repeating character is f
073
First non-repeating character is f
087

"geeksforgeeks"6

First non-repeating character is f
44
First non-repeating character is f
45

First non-repeating character is f
0
First non-repeating character is f
43
First non-repeating character is f
35 =0
First non-repeating character is f
37
First non-repeating character is f
38"geeksforgeeks"4

First non-repeating character is f
0
First non-repeating character is f
43
First non-repeating character is f
100

First non-repeating character is f
7"geeksforgeeks"17
First non-repeating character is f
44
First non-repeating character is f
104

First non-repeating character is f
0
First non-repeating character is f
43 string 4
First non-repeating character is f
50

First non-repeating character is f
0string 6

First non-repeating character is f
7
First non-repeating character is f
6
First non-repeating character is f
0=0
First non-repeating character is f
392

=5

First non-repeating character is f
1
First non-repeating character is f
395

First non-repeating character is f
63
First non-repeating character is f
397

=5"geeksforgeeks"6

=5

First non-repeating character is f
5 string 6

First non-repeating character is f
63
First non-repeating character is f
404
First non-repeating character is f
44
First non-repeating character is f
406

=5"geeksforgeeks"6

First non-repeating character is f
7"geeksforgeeks"6

First non-repeating character is f
0"geeksforgeeks"6

First non-repeating character is f
0
First non-repeating character is f
84
First non-repeating character is f
43 =0
First non-repeating character is f
62
First non-repeating character is f
51 string 51

First non-repeating character is f
0string 6

First non-repeating character is f
7
First non-repeating character is f
71

First non-repeating character is f
7=0
First non-repeating character is f
426=0
First non-repeating character is f
428

First non-repeating character is f
7
First non-repeating character is f
6 string 78

=5

First non-repeating character is f
1
First non-repeating character is f
434

First non-repeating character is f
63
First non-repeating character is f
436

First non-repeating character is f
63
First non-repeating character is f
438

=5"geeksforgeeks"6

First non-repeating character is f
7"geeksforgeeks"6

First non-repeating character is f
7"geeksforgeeks"3
First non-repeating character is f
017

First non-repeating character is f
0"geeksforgeeks"6

First non-repeating character is f
0
First non-repeating character is f
42
First non-repeating character is f
43 string 4
First non-repeating character is f
452
First non-repeating character is f
51
First non-repeating character is f
454

First non-repeating character is f
0string 6

First non-repeating character is f
7
First non-repeating character is f
51 =05"geeksforgeeks""geeksforgeeks"4

First non-repeating character is f
7=0
First non-repeating character is f
04

First non-repeating character is f
7=12

=5

First non-repeating character is f
468=0
First non-repeating character is f
470

First non-repeating character is f
63
First non-repeating character is f
21
First non-repeating character is f
196

First non-repeating character is f
23
First non-repeating character is f
24
First non-repeating character is f
199

First non-repeating character is f
63
First non-repeating character is f
27=9

First non-repeating character is f
23=30

First non-repeating character is f
0"geeksforgeeks"6

"geeksforgeeks"6

JavaScript

First non-repeating character is f
00

First non-repeating character is f
486

First non-repeating character is f
01
First non-repeating character is f
488

string 6

First non-repeating character is f
0
First non-repeating character is f
491
First non-repeating character is f
44
First non-repeating character is f
493

First non-repeating character is f
0
First non-repeating character is f
6
First non-repeating character is f
496

First non-repeating character is f
7
First non-repeating character is f
498

First non-repeating character is f
0"geeksforgeeks"6

First non-repeating character is f
0
First non-repeating character is f
6
First non-repeating character is f
503

First non-repeating character is f
7
First non-repeating character is f
505

First non-repeating character is f
7
First non-repeating character is f
507

First non-repeating character is f
0"geeksforgeeks"6

First non-repeating character is f
0
First non-repeating character is f
511

First non-repeating character is f
0
First non-repeating character is f
6
First non-repeating character is f
514

First non-repeating character is f
7
First non-repeating character is f
1
First non-repeating character is f
517

=5

First non-repeating character is f
519

First non-repeating character is f
0"geeksforgeeks"3 index 04

"geeksforgeeks"6

First non-repeating character is f
524"geeksforgeeks""geeksforgeeks"4

First non-repeating character is f
527

First non-repeating character is f
1
First non-repeating character is f
529

First non-repeating character is f
0
First non-repeating character is f
531
First non-repeating character is f
1
First non-repeating character is f
12

First non-repeating character is f
5

First non-repeating character is f
0
First non-repeating character is f
531=9
First non-repeating character is f
538

First non-repeating character is f
27

Đầu ra

First non-repeating character is f

Độ phức tạp về thời gian: O [n], vì chuỗi cần được đi qua ít nhất một lần. Không gian của bạn: O [256], không gian bị chiếm bởi việc sử dụng Count_array/Hash_map để theo dõi tần số. O[N], As the string needs to be traversed at least once.
Auxiliary Space: O[256], Space is occupied by the use of count_array/hash_map to keep track of frequency.

Ký tự không lặp lại đầu tiên bằng cách sử dụng mảng đếm và chuỗi chuỗi đơn:

Ý tưởng là đánh dấu các yếu tố lặp đi lặp lại với một số giá trị, hãy nói với -2 và người lặp lại một lần sẽ được đánh dấu bằng chỉ số hiện tại.

Thực hiện theo các bước dưới đây để giải quyết vấn đề:

  • Tạo một mảng đếm có số lượng ký tự tối đa [256] và khởi tạo tất cả các phần tử trong mảng này thành -1. & Nbsp;-1
  • Sau đó lặp qua ký tự chuỗi theo ký tự và kiểm tra xem phần tử mảng có ký tự này là chỉ mục là -1 hay không. & Nbsp;-1 or not. 
  • Nếu đó là -1 thì hãy thay đổi nó thành i và. Nếu nó không phải là -1, thì điều này có nghĩa là ký tự này đã xuất hiện trước đó, vì vậy hãy thay đổi nó thành -2. & Nbsp;-1 then change it to i and. If it is not -1, then this means that this character already appeared before, so change it to -2
  • Cuối cùng, tất cả các ký tự lặp lại sẽ được thay đổi thành -2 và tất cả các ký tự không lặp lại sẽ chứa chỉ mục nơi chúng xảy ra. & NBSP;-2 and all non-repeating characters will contain the index where they occur. 
  • Bây giờ, chỉ cần lặp qua tất cả các ký tự không lặp lại và tìm chỉ mục tối thiểu hoặc chỉ mục đầu tiên.

Dưới đây là việc thực hiện phương pháp trên.

C++

First non-repeating character is f
540

First non-repeating character is f
28

string 1 string 2 string 3

=0

First non-repeating character is f
546

string 6

First non-repeating character is f
0=0
First non-repeating character is f
550

First non-repeating character is f
0
First non-repeating character is f
6
First non-repeating character is f
0=0
First non-repeating character is f
555

First non-repeating character is f
7
First non-repeating character is f
557

First non-repeating character is f
0
First non-repeating character is f
6
First non-repeating character is f
0=0
First non-repeating character is f
562

First non-repeating character is f
7
First non-repeating character is f
1
First non-repeating character is f
565

=5

First non-repeating character is f
567

First non-repeating character is f
7"geeksforgeeks"6

First non-repeating character is f
7
First non-repeating character is f
5 string 6

=5

First non-repeating character is f
574

First non-repeating character is f
7"geeksforgeeks"6

First non-repeating character is f
0"geeksforgeeks"6

First non-repeating character is f
0=0 "geeksforgeeks"91

First non-repeating character is f
0
First non-repeating character is f
6
First non-repeating character is f
0=0
First non-repeating character is f
586

First non-repeating character is f
7
First non-repeating character is f
1
First non-repeating character is f
589

=5

First non-repeating character is f
591

First non-repeating character is f
0"geeksforgeeks"6

First non-repeating character is f
0____21
First non-repeating character is f
596

First non-repeating character is f
7"geeksforgeeks"3
First non-repeating character is f
599

First non-repeating character is f
0
First non-repeating character is f
5

First non-repeating character is f
7"geeksforgeeks"3 index 04

"geeksforgeeks"6

=0

First non-repeating character is f
00

string 6

First non-repeating character is f
0
First non-repeating character is f
610

First non-repeating character is f
0=05"geeksforgeeks""geeksforgeeks"4

First non-repeating character is f
0=0
First non-repeating character is f
617

First non-repeating character is f
0
First non-repeating character is f
1
First non-repeating character is f
620

First non-repeating character is f
7=8index 1

First non-repeating character is f
63index 3"geeksforgeeks"4

First non-repeating character is f
0
First non-repeating character is f
5

First non-repeating character is f
7=8=9

First non-repeating character is f
19
First non-repeating character is f
633

First non-repeating character is f
0"geeksforgeeks"3
First non-repeating character is f
26

"geeksforgeeks"6

Java

First non-repeating character is f
42
First non-repeating character is f
44
First non-repeating character is f
45

First non-repeating character is f
0
First non-repeating character is f
42
First non-repeating character is f
43 =0
First non-repeating character is f
67

First non-repeating character is f
0string 6

First non-repeating character is f
7=0
First non-repeating character is f
650

=5"geeksforgeeks"17

First non-repeating character is f
44 =0
First non-repeating character is f
42
First non-repeating character is f
38
First non-repeating character is f
657

First non-repeating character is f
7
First non-repeating character is f
6
First non-repeating character is f
0=0
First non-repeating character is f
57
First non-repeating character is f
58
First non-repeating character is f
664
First non-repeating character is f
38
First non-repeating character is f
666

=5

First non-repeating character is f
668
First non-repeating character is f
2"geeksforgeeks"4

First non-repeating character is f
7
First non-repeating character is f
6
First non-repeating character is f
0=0
First non-repeating character is f
57
First non-repeating character is f
58
First non-repeating character is f
81

=5

First non-repeating character is f
1
First non-repeating character is f
680
First non-repeating character is f
2
First non-repeating character is f
86

First non-repeating character is f
63
First non-repeating character is f
684

=5"geeksforgeeks"6

=5

First non-repeating character is f
5 string 6

First non-repeating character is f
63
First non-repeating character is f
691
First non-repeating character is f
692"geeksforgeeks"4

=5"geeksforgeeks"6

First non-repeating character is f
7"geeksforgeeks"6

First non-repeating character is f
7=0
First non-repeating character is f
700

First non-repeating character is f
7
First non-repeating character is f
6
First non-repeating character is f
0=0
First non-repeating character is f
57
First non-repeating character is f
58
First non-repeating character is f
664
First non-repeating character is f
38
First non-repeating character is f
709

=5

First non-repeating character is f
1
First non-repeating character is f
712
First non-repeating character is f
58
First non-repeating character is f
2

First non-repeating character is f
63
First non-repeating character is f
716

First non-repeating character is f
7"geeksforgeeks"6

First non-repeating character is f
7
First non-repeating character is f
1
First non-repeating character is f
721

=5"geeksforgeeks"3

First non-repeating character is f
1
First non-repeating character is f
2"geeksforgeeks"4

First non-repeating character is f
7
First non-repeating character is f
5

=5"geeksforgeeks"3 index 04

First non-repeating character is f
0"geeksforgeeks"6

First non-repeating character is f
0
First non-repeating character is f
42
First non-repeating character is f
43 string 4
First non-repeating character is f
738

First non-repeating character is f
0string 6

First non-repeating character is f
7
First non-repeating character is f
742

First non-repeating character is f
7=05"geeksforgeeks""geeksforgeeks"4

First non-repeating character is f
7=0
First non-repeating character is f
617

First non-repeating character is f
7
First non-repeating character is f
1
First non-repeating character is f
752
First non-repeating character is f
2
First non-repeating character is f
2

=5

First non-repeating character is f
16

First non-repeating character is f
63
First non-repeating character is f
1
First non-repeating character is f
12

First non-repeating character is f
7
First non-repeating character is f
5

=5

First non-repeating character is f
16

First non-repeating character is f
63=9

First non-repeating character is f
63
First non-repeating character is f
767

First non-repeating character is f
0"geeksforgeeks"6

"geeksforgeeks"6

Python3

First non-repeating character is f
29
First non-repeating character is f
209

First non-repeating character is f
09
First non-repeating character is f
62
First non-repeating character is f
215
First non-repeating character is f
216

First non-repeating character is f
0
First non-repeating character is f
778__

First non-repeating character is f
0
First non-repeating character is f
6
First non-repeating character is f
7
First non-repeating character is f
8
First non-repeating character is f
224
First non-repeating character is f
0
First non-repeating character is f
246
First non-repeating character is f
0
First non-repeating character is f
215
First non-repeating character is f
249

Các

Các

First non-repeating character is f
7
First non-repeating character is f
5
First non-repeating character is f
6

Các

First non-repeating character is f
0
First non-repeating character is f
272=
First non-repeating character is f
274

First non-repeating character is f
0
First non-repeating character is f
6
First non-repeating character is f
7
First non-repeating character is f
8
First non-repeating character is f
224
First non-repeating character is f
0
First non-repeating character is f
38
First non-repeating character is f
216

First non-repeating character is f
7
First non-repeating character is f
1
First non-repeating character is f
846=
First non-repeating character is f
58
First non-repeating character is f
216

=5

First non-repeating character is f
272=
First non-repeating character is f
293
First non-repeating character is f
854

First non-repeating character is f
0
First non-repeating character is f
1
First non-repeating character is f
857=__
First non-repeating character is f
312

First non-repeating character is f
7"geeksforgeeks"3
First non-repeating character is f
1
First non-repeating character is f
2

First non-repeating character is f
0
First non-repeating character is f
5
First non-repeating character is f
6

First non-repeating character is f
7"geeksforgeeks"3
First non-repeating character is f
299

First non-repeating character is f
215 = "geeksforgeeks"

First non-repeating character is f
874=
First non-repeating character is f
62
First non-repeating character is f
215
First non-repeating character is f
2

First non-repeating character is f
1
First non-repeating character is f
880=__
First non-repeating character is f
1
First non-repeating character is f
2
First non-repeating character is f
216

First non-repeating character is f
0
First non-repeating character is f
9
First non-repeating character is f
0
First non-repeating character is f
1
First non-repeating character is f
2

First non-repeating character is f
5
First non-repeating character is f
6

First non-repeating character is f
0
First non-repeating character is f
9
First non-repeating character is f
0=9
First non-repeating character is f
9
First non-repeating character is f
898
First non-repeating character is f
0
First non-repeating character is f
215
First non-repeating character is f
901

C#

string 1

First non-repeating character is f
41

First non-repeating character is f
42
First non-repeating character is f
44
First non-repeating character is f
45

First non-repeating character is f
0
First non-repeating character is f
42
First non-repeating character is f
43 =0
First non-repeating character is f
62
First non-repeating character is f
51 string 51

First non-repeating character is f
0string 6

First non-repeating character is f
7=0
First non-repeating character is f
650

=5"geeksforgeeks"17

First non-repeating character is f
44 =0
First non-repeating character is f
923

First non-repeating character is f
7
First non-repeating character is f
6
First non-repeating character is f
0=0
First non-repeating character is f
555

=5

First non-repeating character is f
557

First non-repeating character is f
7
First non-repeating character is f
6
First non-repeating character is f
0=0
First non-repeating character is f
392

=5

First non-repeating character is f
1
First non-repeating character is f
565

First non-repeating character is f
63
First non-repeating character is f
567

=5"geeksforgeeks"6

=5

First non-repeating character is f
5 string 6

First non-repeating character is f
63
First non-repeating character is f
574

=5"geeksforgeeks"6

First non-repeating character is f
7"geeksforgeeks"6

First non-repeating character is f
7=0
First non-repeating character is f
954

First non-repeating character is f
7
First non-repeating character is f
6
First non-repeating character is f
0=0
First non-repeating character is f
586

=5

First non-repeating character is f
1
First non-repeating character is f
589

First non-repeating character is f
63
First non-repeating character is f
964

First non-repeating character is f
7"geeksforgeeks"6

First non-repeating character is f
7
First non-repeating character is f
1
First non-repeating character is f
969

=5"geeksforgeeks"3

First non-repeating character is f
599

First non-repeating character is f
7
First non-repeating character is f
5

=5"geeksforgeeks"3 index 04

First non-repeating character is f
0"geeksforgeeks"6

First non-repeating character is f
0
First non-repeating character is f
42
First non-repeating character is f
43 string 4
First non-repeating character is f
738

First non-repeating character is f
0string 6

First non-repeating character is f
7=0
First non-repeating character is f
617

First non-repeating character is f
7=05"geeksforgeeks""geeksforgeeks"4

First non-repeating character is f
7=0
First non-repeating character is f
617

First non-repeating character is f
7
First non-repeating character is f
1
First non-repeating character is f
752
First non-repeating character is f
2
First non-repeating character is f
2

=5=12

First non-repeating character is f
63
First non-repeating character is f
1
First non-repeating character is f
12

First non-repeating character is f
7
First non-repeating character is f
5

=5=12

First non-repeating character is f
63=9

First non-repeating character is f
63
First non-repeating character is f
012

First non-repeating character is f
0"geeksforgeeks"6

"geeksforgeeks"6

First non-repeating character is f
29
First non-repeating character is f
209

First non-repeating character is f
00

First non-repeating character is f
09
First non-repeating character is f
62
First non-repeating character is f
215
First non-repeating character is f
216

string 6

First non-repeating character is f
0
First non-repeating character is f
778__

First non-repeating character is f
0
First non-repeating character is f
026

First non-repeating character is f
0
First non-repeating character is f
6
First non-repeating character is f
7
First non-repeating character is f
8
First non-repeating character is f
224
First non-repeating character is f
0
First non-repeating character is f
246
First non-repeating character is f
0
First non-repeating character is f
215
First non-repeating character is f
249

First non-repeating character is f
7
First non-repeating character is f
557

Các

First non-repeating character is f
0string 6

First non-repeating character is f
7
First non-repeating character is f
1
First non-repeating character is f
043

First non-repeating character is f
7string 6

=5

First non-repeating character is f
047

First non-repeating character is f
7"geeksforgeeks"6

First non-repeating character is f
7
First non-repeating character is f
5

First non-repeating character is f
7string 6

=5

First non-repeating character is f
055

First non-repeating character is f
7"geeksforgeeks"6

First non-repeating character is f
0"geeksforgeeks"6

Các

Các

First non-repeating character is f
0
First non-repeating character is f
272=
First non-repeating character is f
274

=5

First non-repeating character is f
716

First non-repeating character is f
0"geeksforgeeks"6

First non-repeating character is f
0
First non-repeating character is f
6
First non-repeating character is f
7
First non-repeating character is f
8
First non-repeating character is f
224
First non-repeating character is f
0
First non-repeating character is f
38
First non-repeating character is f
216

First non-repeating character is f
7
First non-repeating character is f
1
First non-repeating character is f
846=
First non-repeating character is f
58
First non-repeating character is f
216

"geeksforgeeks"6

=5

First non-repeating character is f
272=
First non-repeating character is f
293
First non-repeating character is f
854

First non-repeating character is f
0=05"geeksforgeeks""geeksforgeeks"4

First non-repeating character is f
0
First non-repeating character is f
1
First non-repeating character is f
857=__
First non-repeating character is f
312

First non-repeating character is f
7"geeksforgeeks"3
First non-repeating character is f
1
First non-repeating character is f
2

First non-repeating character is f
7
First non-repeating character is f
531
First non-repeating character is f
1
First non-repeating character is f
12

First non-repeating character is f
0
First non-repeating character is f
5

First non-repeating character is f
7
First non-repeating character is f
531=9
First non-repeating character is f
767

First non-repeating character is f
27

First non-repeating character is f
7"geeksforgeeks"3
First non-repeating character is f
299

First non-repeating character is f

First non-repeating character is f
215 = "geeksforgeeks": O[N], As the string needs to be traversed once
Auxiliary Space: O[1], Space is occupied by the use of count-array to keep track of frequency.

Nhân vật không lặp lại đầu tiên bằng cách sử dụng các hàm Python tích hợp:

Ý tưởng là tìm tần số của tất cả các ký tự trong chuỗi và kiểm tra ký tự nào có tần số đơn vị.unit frequency.

Thực hiện theo các bước dưới đây để giải quyết vấn đề:

  • Tính tất cả các tần số của tất cả các ký tự bằng hàm bộ đếm [].
  • Đi qua chuỗi và kiểm tra nếu bất kỳ phần tử nào có tần số 1.
  • In ký tự và phá vỡ vòng lặp.

Dưới đây là việc thực hiện phương pháp trên:

Python3

First non-repeating character is f
108
First non-repeating character is f
109
First non-repeating character is f
29
First non-repeating character is f
111

First non-repeating character is f
09
First non-repeating character is f
113

First non-repeating character is f
0
First non-repeating character is f
115=
First non-repeating character is f
117

First non-repeating character is f
0
First non-repeating character is f
6
First non-repeating character is f
7
First non-repeating character is f
8
First non-repeating character is f
9

Các

=5

First non-repeating character is f
3

string = "geeksforgeeks"

First non-repeating character is f
141

Đầu ra

First non-repeating character is f

Độ phức tạp về thời gian: O [N]. Vì chuỗi cần được đi qua ít nhất một lần. Không gian của bạn: O [256], không gian bị chiếm bởi việc sử dụng mảng tần số.O[N]. As the string needs to be traversed at least once.
Auxiliary Space: O[256], Space is occupied by the use of the frequency array.

Vấn đề liên quan: Nhân vật không lặp lại của K

Bài viết này được đóng góp bởi aarti_rathi. & Nbsp; vui lòng viết nhận xét nếu bạn tìm thấy bất cứ điều gì không chính xác hoặc bạn muốn chia sẻ thêm thông tin về chủ đề được thảo luận ở trên.Aarti_Rathi
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.


Làm thế nào để bạn đếm các ký tự không lặp lại trong một chuỗi?

Các ký tự không lặp lại là các ký tự chỉ có trong chuỗi một lần. Để tìm các ký tự không lặp lại trong một chuỗi, chúng tôi sẽ sử dụng một vòng để tính tần số của từng ký tự và in những ký tự có tần số đếm một lần sử dụng một vòng khác cho vòng lặp.use one for loop to calculate the frequency of each character and print those characters that have frequency count one using another for loop.

Làm thế nào để bạn có được một giá trị không lặp lại trong Python?

Một trong những cách sau đây có thể được sử dụng để có được các giá trị duy nhất từ danh sách trong Python:..
Phương thức python set [] ..
Sử dụng danh sách Python.Phương thức nối [] cùng với một vòng lặp ..
Sử dụng Python Numpy.Phương thức duy nhất [] ..

Làm thế nào để bạn tìm thấy các ký tự độc đáo trong một chuỗi trong Python?

Khoa học dữ liệu thực tế sử dụng Python..
Tạo một bản đồ tần số ..
Đối với mỗi ký tự C trong chuỗi, làm.Nếu C không ở tần số, thì hãy chèn nó vào tần số và đặt giá trị 1. Nếu không, hãy tăng số lượng theo tần số ..
Quét bản đồ tần số, nếu giá trị của một khóa cụ thể là 1, thì hãy trả lại khóa đó, nếu không thì trả về -1 ..

Làm cách nào để đếm các ký tự lặp lại trong một chuỗi trong Python?

Python..
chuỗi = "Trách nhiệm lớn" ;.
in ["ký tự trùng lặp trong một chuỗi đã cho:"] ;.
#Count mỗi ký tự có trong chuỗi ..
cho i trong phạm vi [0, len [chuỗi]]:.
Đếm = 1 ;.
cho J trong phạm vi [i+1, len [chuỗi]]:.
if [chuỗi [i] == chuỗi [j] và chuỗi [i]! = '']:.
Đếm = đếm + 1 ;.

Bài Viết Liên Quan

Chủ Đề