查找不重複字符的最長子字串(面試常見題-用8種編程語言回答)


查找不重複字符的最長子字串(面試常見題-用8種編程語言回答)


查找不重複字符的最長子字符串(編程面試中常見題-用8種編程語言來回答)

給定一個字符串str,找到不重複字符的最長子字符串。

比如我們有 “ABDEFGABEF”, 最長的字符串是 “BDEFGA” 和 “DEFGAB”, 長度為6.

再如 “BBBB” 最長字符串是 “B”, 長度為1.

再比如 “neatcoding” 最長字符串是“neatcodi”, 長度為8.

所需的時間複雜度為O(n),其中n是字符串的長度。

我們將逐個字符地遍歷該字符串

檢查此字符是否在當前子字符串中,如果是,則將當前子字符串保存到子字符串集合中,使用此字符作為起始值創建一個新的子字符串;

如果否,則將此字符添加到當前子字符串中;

至此,循環結束,檢查當前子字符串是否為空,如果是,則不執行任何操作

如果否,請將其添加到子字符串集合

我們設置一個對象來存儲最長的子字符串,

現在,讓我們遍歷子字符串集合,找到最長的一個

檢查當前子字符串是否更長,如果是,則替換最長的字符串

如果沒有,什麼也不做

最後,我們有最長的子字符串。

讓我們編碼。

In Javascript:

returnLongestNonRepeatSubString(s){

if(!s){

return "";

}

let subCollection = [];

let currentSub = "";

for(let i = 0; i < s.length; i ++) {

let c = s[i];

if(currentSub.indexOf(c) === -1) {

currentSub += c;

}

else {

subCollection.push(currentSub);

currentSub = "" + c;

}

}

if(currentSub.length > 0){

subCollection.push(currentSub);

}

let longest = "";

for(let i = 0 ; i < subCollection.length ; i ++) {

let sub = subCollection[i];

if(sub.length > longest.length) {

longest = sub;

}

}

return longest;

}

In C#:

string returnLongestNonRepeatSubString(string s)

{

if (string.IsNullOrEmpty(s))

{

return "";

}

List<string> subCollection = new List<string>();/<string>/<string>

string currentSub = "";

for (int i = 0; i < s.Length; i++)

{

char c = s[i];

if (currentSub.IndexOf(c) == -1)

{

currentSub += c;

}

else

{

subCollection.Add(currentSub);

currentSub = "" + c;

}

}

if (currentSub.Length > 0)

{

subCollection.Add(currentSub);

}

string longest = "";

for (int i = 0; i < subCollection.Count; i++)

{

string sub = subCollection[i];

if (sub.Length > longest.Length)

{

longest = sub;

}

}

return longest;

}

In Java:

String returnLongestNonRepeatSubString(String s)

{

if (s == null || s.length() == 0)

{

return "";

}

List<string> subCollection = new ArrayList<string>();/<string>/<string>

String currentSub = "";

for (int i = 0; i < s.length(); i++)

{

char c = s.charAt(i);

if (currentSub.indexOf(c) == -1)

{

currentSub += c;

}

else

{

subCollection.add(currentSub);

currentSub = "" + c;

}

}

if (currentSub.length() > 0)

{

subCollection.add(currentSub);

}

String longest = "";

for (int i = 0; i < subCollection.size(); i++)

{

String sub = subCollection.get(i);

if (sub.length() > longest.length())

{

longest = sub;

}

}

return longest;

}

In Kotlin:

fun returnLongestNonRepeatSubString(s: String?): String {

if (s == null || s.length == 0) {

return ""

}

val subCollection = ArrayList<string>()/<string>

var currentSub = ""

for (i in 0 until s.length) {

val c = s[i]

if (currentSub.indexOf(c) == -1) {

currentSub += c

} else {

subCollection.add(currentSub)

currentSub = "" + c

}

}

if (currentSub.length > 0) {

subCollection.add(currentSub)

}

var longest = ""

for (i in subCollection.indices) {

val sub = subCollection[i]

if (sub.length > longest.length) {

longest = sub

}

}

return longest

}

In Golang:

func returnLongestNonRepeatSubString(s string) string {

if len(s) == 0 {

return ""

}

subCollection := make([]string, 1)

currentSub := ""

for i := 0; i < len(s); i++ {

var c = s[i]

if !strings.Contains(currentSub, string(c)) {

currentSub += string(c)

} else {

subCollection = append(subCollection, currentSub)

currentSub = string(c)

}

}

if len(currentSub) > 0 {

subCollection = append(subCollection, currentSub)

}

var longest = ""

for _, sub := range subCollection {

if len(sub) > len(longest) {

longest = sub

}

}

return longest

}

In c++:

string

returnLongestNonRepeatSubString (const string & s)

{

if (s.empty ())

{

return "";

}

std::vector <string> subCollection;/<string>

string currentSub;

for (int i = 0; i < s.length (); i++)

{

char c = s[i];

if (currentSub.find (c) == -1)

{

currentSub += c;

}

else

{

subCollection.push_back (currentSub);

currentSub = string (1, c);

}

}

if(!currentSub.empty())

{

subCollection.push_back (currentSub);

}

string longest = "";

for (int i = 0; i < subCollection.size(); i++)

{

string sub = subCollection.at(i);

if (sub.length() > longest.length())

{

longest = sub;

}

}

return longest;

}

In Python:

def returnLongestNonRepeatSubString(s):

if not s:

return ""

subCollection = []

currentSub = ""

for c in s:

if (currentSub.find(c) == -1):

currentSub += c

else:

subCollection.append(currentSub)

currentSub = c

if currentSub:

subCollection.append(currentSub)

longest = ""

for sub in subCollection:

if (len(sub) > len(longest)):

longest = sub

return longest

In Swift:

func returnLongestNonRepeatSubString (s: String) -> String {

if (s == nil || s.isEmpty) {

return ""

}

var subCollection = [String]()

var currentSub = ""

for c in s {

let i = currentSub.firstIndex(of: c)

if(i == nil) {

currentSub = currentSub + String(c)

}

else {

subCollection.append(currentSub);

currentSub = String(c)

}

}

var longest = ""

for sub in subCollection {

if sub.length > longest.length {

longest = sub

}

}

return longest

}


分享到:


相關文章: