Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

Longest Common Prefix Method 2

class Solution:
    def longestCommonPrefix(self, strs):
        result = []
        for i in zip(*strs):
            if len(set(i)) != 1:
                break
            result.append(i[0])
        return "".join(result)     

"""The zip() method returns a zip object, which is an iterator of tuples where 
the first item in each passed iterator is paired together, and then the second item in 
each passed iterator are paired together etc.
If the passed iterators have different lengths, the iterator with the least items 
decides the length of the new iterator.

set() method is used to convert any of the iterable to sequence of iterable elements 
with distinct elements, commonly called Set. 
"""                 
 
        

Task = Solution()
print("3a. ",Task.longestCommonPrefix(["flower","flow","flight"]))
print("3b. ",Task.longestCommonPrefix(["dog","racecar","car"]))
Comment

Longest Common Prefix

class Solution:
    def longestCommonPrefix(self, strs):
        result = ""
        
        for i in range(len(strs[0])):                       # Pick any word in the list and loop through its length. Thats the number of times youre to loop thu the list
                                                            # because your answer cant be longer in length than any word in the list
            for word in strs:                                  # loop through the words in the the list so you can use the word[i] to acces the letters of each word                
                if i == len(word) or word[i] != strs[0][i]:     # stop code by returning result if loop count(i) is same as length of your chosen word 
                    return result                               # or if theres no more similar between other words and your chosen word
            result = result + strs[0][i]                        # otherwise keep adding the similar letters that occur in same position in all the words to the result 
 
        

Task = Solution()
print(Task.longestCommonPrefix(["flower","flow","flight"]))
print(Task.longestCommonPrefix(["dog","racecar","car"]))
Comment

Longest Common Prefix

class Solution {
public:
    string longestCommonPrefix(vector<string>& strs) {
        
    }
};
Comment

Longest Common Prefix

class Solution {
    public String longestCommonPrefix(String[] strs) {
        
    }
}
Comment

Longest Common Prefix



char * longestCommonPrefix(char ** strs, int strsSize){

}
Comment

Longest Common Prefix

public class Solution {
    public string LongestCommonPrefix(string[] strs) {
        
    }
}
Comment

Longest Common Prefix

/**
 * @param {string[]} strs
 * @return {string}
 */
var longestCommonPrefix = function(strs) {
    
};
Comment

Longest Common Prefix

# @param {String[]} strs
# @return {String}
def longest_common_prefix(strs)
    
end
Comment

Longest Common Prefix

class Solution {
    func longestCommonPrefix(_ strs: [String]) -> String {
        
    }
}
Comment

Longest Common Prefix

class Solution {

    /**
     * @param String[] $strs
     * @return String
     */
    function longestCommonPrefix($strs) {
        
    }
}
Comment

Longest Common Prefix

function longestCommonPrefix(strs: string[]): string {

};
Comment

Longest prefix which is also suffix

// CPP program to find length of the longest 
// prefix which is also suffix 
#include <bits/stdc++.h> 
using namespace std; 
  
// Function to find largest prefix which is also a suffix 
int largest_prefix_suffix(const std::string &str) { 
    
  int n = str.length(); 
    
  if(n < 2) { 
    return 0; 
  } 
  
  int len = 0; 
  int i = n/2; 
    
  while(i < n) { 
    if(str[i] == str[len]) { 
      ++len; 
      ++i; 
    } else { 
      if(len == 0) { // no prefix 
        ++i; 
      } else { // search for shorter prefixes 
        --len; 
      } 
    } 
  } 
    
  return len; 
  
} 
  
// Driver code 
int main() { 
      
string s = "blablabla"; 
  
cout << largest_prefix_suffix(s); 
  
return 0; 
} 
Comment

PREVIOUS NEXT
Code Example
Python :: textrank python implementation 
Python :: change part of a text file python 
Python :: how to round whole numbers in python 
Python :: how to split a string by colon in python 
Python :: How to clone or copy a list in python 
Python :: Create a hexadecimal colour based on a string with python 
Python :: start index from 1 in python 
Python :: join multiple excel files with python 
Python :: loading bar 
Python :: python tuple example 
Python :: python any() function 
Python :: python list equality 
Python :: django middleware 
Python :: sqlalchemy function for default value for column 
Python :: how to limit a command to a role in discord.py 
Python :: python remove character from string 
Python :: convert ipynb to py 
Python :: Reverse an string Using Reversed function 
Python :: tf dataset 
Python :: datetime to string 
Python :: what is xarray 
Python :: celery periodic tasks 
Python :: map in python 3 
Python :: standard error of mean 
Python :: how to remove a string in python 
Python :: gaussian 
Python :: add new column of dataframe 
Python :: sort a dataframe 
Python :: python sort based on multiple keys 
Python :: numpy.dot 
ADD CONTENT
Topic
Content
Source link
Name
7+7 =