0% found this document useful (0 votes)
54 views3 pages

Integer To Roman 2. Longest Common Prefix: Assignment 2

This document contains 10 coding problems or assignments: 1) Converting integers to Roman numerals 2) Finding the longest common prefix in a string array 3) Grouping anagrams 4) Validating palindromes 5) Finding the first unique character in a string 6) Validating IP addresses 7) Finding unique email addresses from a list ignoring certain formatting differences 8) Finding string matches within an array of strings 9) Breaking a given palindrome string by changing one character 10) Compressing a list of strings by replacing repeating substrings with their counts
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
54 views3 pages

Integer To Roman 2. Longest Common Prefix: Assignment 2

This document contains 10 coding problems or assignments: 1) Converting integers to Roman numerals 2) Finding the longest common prefix in a string array 3) Grouping anagrams 4) Validating palindromes 5) Finding the first unique character in a string 6) Validating IP addresses 7) Finding unique email addresses from a list ignoring certain formatting differences 8) Finding string matches within an array of strings 9) Breaking a given palindrome string by changing one character 10) Compressing a list of strings by replacing repeating substrings with their counts
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

ASSIGNMENT 2

1. INTEGER TO ROMAN
2. LONGEST COMMON PREFIX
#include<stdio.h>
#include<string.h>
char* longestCommonPrefix(char** strs, int strsSize)
{
int i=0; int j=0;int index;int tempindex=0;
if(strsSize<1)
return "";
index=strlen(strs[0]);
char *a;
a= malloc(sizeof(char)*(index+1));
strcpy(a,strs[0]);
for(i=1;i<strsSize;i++)
{ tempindex=0;
for(j=0;j<index;j++)
{
if(a[j]==strs[i][j])
tempindex++;
else
{a[j]='\0';
break;
}
}
if (tempindex==0)return ("");
if(tempindex<index)index=tempindex;

}
return a;

}
3. GROUP ANAGRAMS
4. VALID PALINDROME

bool isPalindrome(char * s){

if(s==NULL || !strcmp(s,"")) return true;

for(char* p = s + strlen(s) -1; p>s;)


if(isalnum(*s) && isalnum(*p) && tolower(*(s++)) != tolower(*(p--))) return
false;
else if(!isalnum(*s)) s++;
else if(!isalnum(*p)) p--;

return true;
}
5. FIRST UNIQUE CHARACTER IN A STRING
from collections import Counter

class Solution(object):
def firstUniqChar(self, string):
# counter = Counter()
# for char in string:
# counter[char]+=1
counter = Counter(string)

for i in xrange(len(string)):
char = string[i]
if counter[char]==1: return i

return -1
6. VALIDATE IP ADDRESS
7. UNIQUE EMAIL ADDRESS
class Solution:
def numUniqueEmails(self, emails: List[str]) -> int:
result=set()
for e in emails:
name,last=e.split('@')
if '+' in name:
name=name[:name.index('+')]
name=name.replace('.','')
name=name+'@'+last
result.add(name)
return len(result)
8. STRING MATCHING IN AN ARRAY

char ** stringMatching(char ** words, int wordsSize, int* returnSize){


char **a = malloc(sizeof(char *[wordsSize])), **w = words;
int k = 0;
for (int i = 0 ; i < wordsSize ; i++)
for (int j = i + 1, x, y ; j < wordsSize ; j++)
if (w[j] && w[i] && ((x = !!strstr(w[i], w[j])) || (y = !!strstr(w[j], w[i])))) {
a[k++] = x ? w[j] : w[i];
x ? w[j] = NULL : (w[i] = NULL);
}
return *returnSize = k, a;
}
9. BREAK AN PALINDROME
class Solution:
def breakPalindrome(self, palindrome: str) -> str:
if len(palindrome) < 2:
return ""
palindrome = list(palindrome)
for i in range(len(palindrome)):
if not (len(palindrome) % 2 == 1 and i == len(palindrome)//2):
if palindrome[i] != "a":
palindrome[i] = "a"
return "".join(palindrome)
if i == len(palindrome)-1 and palindrome[i] == "a":
palindrome[i] = "b"
return "".join(palindrome)
return ""
10.STRING COMPRESSION
class Solution:
def compress(self, C: List[str]) -> int:
if len(C) <= 1: return
c, i, _ = 1, 0, C.append(" ")
while C[i] != " ":
if C[i] == C[i+1]: c += 1
elif c > 1: C[i+2-c:i+1], i, c = list(str(c)), i + 1 - c + len(list(str(c))), 1
i += 1
del C[-1]

You might also like