Chuỗi là sự kết hợp của các ký tự khi 2 hoặc nhiều ký tự kết hợp với nhau, nó tạo thành chuỗi cho dù sự hình thành đó có nghĩa hay vô nghĩa. Trong lập trình python, chúng tôi coi một ký tự đơn lẻ cũng là một chuỗi vì không có kiểu dữ liệu nào là một ký tự trong python. Trong chương trình python này, chúng ta sẽ tìm các phần tử duy nhất hoặc các phần tử không lặp lại của chuỗi
Cho một xâu S bao gồm các chữ cái Latinh viết thường, nhiệm vụ là tìm ký tự không lặp lại đầu tiên trong S
ví dụ.
Đầu vào. “geeksforgeeks”
Đầu ra. f
Giải thích. Vì 'f' là ký tự đầu tiên trong chuỗi không lặp lại.Đầu vào. “thuật toán”
Đầu ra. a
Giải thích. Vì 'a' là ký tự đầu tiên trong chuỗi không lặp lại.
Thực hành được đề xuất Ký tự không lặp lại Hãy thử
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ì trả về ký tự đó. Nếu không, tìm kiếm các ký tự còn lại
Ghi chú. Trong python để tìm sự xuất hiện của một ký tự trong chuỗi có In-Built Function string. đếm[]
Dưới đây là việc thực hiện các phương pháp trên
Python3
First non-repeating character is f06
________ 207 ________ 208 ________ 209
First non-repeating character is f0
First non-repeating character is f08
First non-repeating character is f2
First non-repeating character is f3
First non-repeating character is f4
First non-repeating character is f08
First non-repeating character is f6
First non-repeating character is f7
First non-repeating character is f8
First non-repeating character is f9
First non-repeating character is f0
First non-repeating character is f1
First non-repeating character is f2
First non-repeating character is f3
First non-repeating character is f08
First non-repeating character is f08
First non-repeating character is f3
First non-repeating character is f7
First non-repeating character is f8
First non-repeating character is f4____00
First non-repeating character is f08
First non-repeating character is f8
First non-repeating character is f8
First non-repeating character is f4
_______01____06____07
First non-repeating character is f8
First non-repeating character is f0
First non-repeating character is f0
First non-repeating character is f08
First non-repeating character is f3
First non-repeating character is f2
First non-repeating character is f0
First non-repeating character is f08
First non-repeating character is f08
First non-repeating character is f3
First non-repeating character is f7
First non-repeating character is f1
First non-repeating character is f0
First non-repeating character is f1
First non-repeating character is f2
First non-repeating character is f3
First non-repeating character is f6
First non-repeating character is f7
First non-repeating character is f1
First non-repeating character is f0____01
First non-repeating character is f9
First non-repeating character is f060
Đầu ra
First non-repeating character is f
Độ phức tạp về thời gian. O[N2], Duyệt qua chuỗi cho mọi ký tự trong chuỗi có kích thước N.
Không gian phụ. Ô[1]
Ký tự không lặp lại đầu tiên sử dụng hàm chuỗi find[]
Ý 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 của nó trong chuỗi. Nếu ký tự được tìm thấy trong chuỗi còn lại thì trả về ký tự đó.
Việc tìm kiếm được thực hiện bằng hàm find[] tích hợp
Dưới đây là việc thực hiện phương pháp
C++
First non-repeating character is f061
First non-repeating character is f062
________ 2063 ________ 2064 ________ 2065
________ 2066 ________ 2067
First non-repeating character is f068
First non-repeating character is f1
First non-repeating character is f7
First non-repeating character is f1____2072
First non-repeating character is f073
First non-repeating character is f8
First non-repeating character is f2
First non-repeating character is f076
First non-repeating character is f077
First non-repeating character is f078
First non-repeating character is f077
First non-repeating character is f080
First non-repeating character is f081
________ 2082 ________ 2083
First non-repeating character is f077
First non-repeating character is f085
First non-repeating character is f086
First non-repeating character is f8
First non-repeating character is f088
First non-repeating character is f1____2088
First non-repeating character is f1____2080
First non-repeating character is f093
________ 2077 ________ 2095 ________ 2086
________ 01 ________ 2085 ________ 2086
First non-repeating character is f088
First non-repeating character is f01
First non-repeating character is f072
First non-repeating character is f03
First non-repeating character is f068
_______01____006____209
First non-repeating character is f086
First non-repeating character is f1
First non-repeating character is f10
First non-repeating character is f088
First non-repeating character is f12
Java
First non-repeating character is f13
First non-repeating character is f14
First non-repeating character is f15
First non-repeating character is f16
First non-repeating character is f17
First non-repeating character is f18
First non-repeating character is f19
First non-repeating character is f18
First non-repeating character is f21
First non-repeating character is f18
First non-repeating character is f23
First non-repeating character is f24
First non-repeating character is f066
First non-repeating character is f26
First non-repeating character is f18
First non-repeating character is f068
First non-repeating character is f1
First non-repeating character is f7
First non-repeating character is f1____2072
First non-repeating character is f33
First non-repeating character is f34
First non-repeating character is f35
First non-repeating character is f36
First non-repeating character is f2
First non-repeating character is f38
First non-repeating character is f3
First non-repeating character is f40
First non-repeating character is f3
First non-repeating character is f42
First non-repeating character is f8
First non-repeating character is f44____2081
First non-repeating character is f46
_______08____04____2086
_______036____2088
First non-repeating character is f1____2088
________ 01 ________ 2085 ________ 2086
First non-repeating character is f18
First non-repeating character is f088
First non-repeating character is f18
First non-repeating character is f23
First non-repeating character is f24
First non-repeating character is f066
First non-repeating character is f63
_______01____065____209
First non-repeating character is f086
First non-repeating character is f1
First non-repeating character is f10
First non-repeating character is f18
First non-repeating character is f088
First non-repeating character is f088
First non-repeating character is f73
Python3
First non-repeating character is f74
First non-repeating character is f75
First non-repeating character is f76
First non-repeating character is f1
First non-repeating character is f7
First non-repeating character is f8
First non-repeating character is f9
First non-repeating character is f81
First non-repeating character is f8
First non-repeating character is f2
First non-repeating character is f84
First non-repeating character is f0
First non-repeating character is f3
First non-repeating character is f87
First non-repeating character is f08
First non-repeating character is f08
First non-repeating character is f2
First non-repeating character is f3
First non-repeating character is f7
First non-repeating character is f077
First non-repeating character is f0
First non-repeating character is f1
First non-repeating character is f9
First non-repeating character is f97
First non-repeating character is f077
First non-repeating character is f4
First non-repeating character is f1____2085
First non-repeating character is f02
________ 003 ________ 208 ________ 005
First non-repeating character is f06
C#
First non-repeating character is f07
First non-repeating character is f08
First non-repeating character is f063
First non-repeating character is f10
________ 023 ________ 024 ________ 016 ________ 017
First non-repeating character is f1
First non-repeating character is f19
First non-repeating character is f1
First non-repeating character is f21
First non-repeating character is f1
First non-repeating character is f23
First non-repeating character is f24
First non-repeating character is f066
First non-repeating character is f23
First non-repeating character is f07
First non-repeating character is f25
First non-repeating character is f1____2068
First non-repeating character is f8
First non-repeating character is f7
First non-repeating character is f1____2072
First non-repeating character is f32
First non-repeating character is f077
First non-repeating character is f2
First non-repeating character is f35
First non-repeating character is f36
First non-repeating character is f37
First non-repeating character is f36
First non-repeating character is f39
First non-repeating character is f40
First non-repeating character is f081
First non-repeating character is f40
First non-repeating character is f43
First non-repeating character is f36
First non-repeating character is f4____2086
First non-repeating character is f077
First non-repeating character is f088
First non-repeating character is f8
First non-repeating character is f088
________ 08 ________ 2085 ________ 2086
First non-repeating character is f1____2088
First non-repeating character is f1
First non-repeating character is f01
First non-repeating character is f1
First non-repeating character is f59
First non-repeating character is f24
First non-repeating character is f066
First non-repeating character is f62
First non-repeating character is f1____2068
First non-repeating character is f8
First non-repeating character is f07
First non-repeating character is f67____209
First non-repeating character is f086
First non-repeating character is f8
First non-repeating character is f10
First non-repeating character is f1____2088
First non-repeating character is f088
First non-repeating character is f75
Javascript
First non-repeating character is f76
First non-repeating character is f77
First non-repeating character is f78
First non-repeating character is f79
_______080____07____082
First non-repeating character is f80
First non-repeating character is f068
First non-repeating character is f85
First non-repeating character is f2
First non-repeating character is f87
First non-repeating character is f85
First non-repeating character is f068
First non-repeating character is f90____091
First non-repeating character is f90
First non-repeating character is f4
First non-repeating character is f85
First non-repeating character is f088
First non-repeating character is f80
First non-repeating character is f088
First non-repeating character is f80
First non-repeating character is f085
First non-repeating character is f088
First non-repeating character is f01
First non-repeating character is f02
First non-repeating character is f05
First non-repeating character is f06
First non-repeating character is f12
First non-repeating character is f06
Đầu ra
First non-repeating character is f
Độ phức tạp về thời gian. O[N2]
Không gian phụ. Ô[1]
Ký tự không lặp lại đầu tiên sử dụng HashMap và hai lần duyệt chuỗi
Ý tưởng là tìm tần số của tất cả các ký tự trong chuỗi và kiểm tra xem ký tự nào có tần số đơn vị
Cách tốt nhất để tìm ký tự không lặp lại đầu tiên trong một chuỗi là gì?
Thuật toán tìm ký tự không lặp lại đầu tiên trong một chuỗi .Nhập chuỗi từ người dùngBắt đầu đi ngang qua chuỗi bằng hai vòngSử dụng vòng lặp đầu tiên để quét từng ký tự của chuỗiSử dụng vòng lặp thứ hai để tìm xem ký tự hiện tại có xuất hiện ở phần sau hay không nếu chuỗi đó hay khôngLàm cách nào để tìm ký tự không lặp lại đầu tiên trong chuỗi bằng luồng?
Sử dụng phương thức indexOf[] và lastIndexOf[] , chúng ta có thể tìm thấy ký tự không lặp lại đầu tiên trong một chuỗi trong Java. Phương thức indexOf[] trả về vị trí xuất hiện đầu tiên của một ký tự đã cho trong một chuỗi trong khi phương thức lastIndexOf[] trả về vị trí xuất hiện cuối cùng của một ký tự đã cho trong một chuỗi.Làm cách nào để lấy các ký tự không khớp trong một chuỗi bằng Python?
Thông tin liên quan .strcat[] — Nối chuỗistrchr[] — Tìm kiếm ký tựstrcmp[] — So sánh chuỗistrcpy[] — Sao chép chuỗistrcspn[] — Tìm độ lệch của ký tự khớp đầu tiênstrpbrk [] - Tìm ký tự trong chuỗistrrchr [] - Xác định vị trí xuất hiện cuối cùng của ký tự trong chuỗiwcschr [] - Tìm kiếm ký tự rộng