Code wars Question:(Isogram)
An Isogram is a word that has no repeating letters,consecutive or non-consecutive.Implement a function that determines whether a string that contains only letters is an Isogram. Assume the empty string is an Isogram.Ignore letter case.
Test Cases:
"Dermatoglyphics"-->true
"aba" --> false
"balLoon"--> false
My Code:
#include <bits/stdc++.h>
using namespace std;
bool isIsogram(string s)
{
for (int i = 0; i < s.length(); i++)
{
for (int j = i + 1; j < s.length(); j++)
{
if (s[i] == s[j] || s[i] == s[j] + 32 || s[i] == s[j] - 32)
{
return false;
}
return true;
}
}
}
int main()
{
cout << isIsogram("abd") << endl;
return 0;
}
If there is an optimized solution for this problem?
Can we reduce time complexity of this problem?
Your code is of O(N^2) Time Complexity.
Yes, there is an Optimized Way of doing it.
Here's the optimized solution: O(N)
#include <bits/stdc++.h>
using namespace std;
bool Isogram(string s)
{
int N = 125;
bool check[N];
for (int i = 0; i < 125; i++)
{
check[i] = false;
}
for (int i = 0; i < s.size(); i++)
{
if (check[s[i]] != false)
{
return false;
}
check[s[i]] = true;
if (s[i] + 32 < N)
check[s[i] + 32] = true;
if (s[i] - 32 >= 0)
check[s[i] - 32] = true;
}
return true;
}
int main()
{
string s;
cin >> s;
if (Isogram(s) == true)
cout << "The word " << s << " is an Isogram" << endl;
else
cout << "The word " << s << " is not an Isogram" << endl;
return 0;
}
We made an array of 125 elements because the ASCII value of lowercase particles lies in the range from 65 to 90, and for uppercase, it is 97 to 122.
And we are adding 32 and subtracting 32 to cover the uppercase alphabet (if the element is in lowercase) and vice versa.
Hope you get it...
Related
I need help solving this issue, I am expecting a number to come out but get this error instead
Line 65: Char 5: error: conflicting types for 'main' int main(int argc, char *argv[]) { ^ Line 47: Char 5: note: previous definition is here int main() ^ 1 error generated.
here is some of my code
class Solution {
public:
int value(char r){
if (r == 'I')
return 1;
if (r == 'V')
return 5;
if (r == 'X')
return 10;
if (r == 'L')
return 50;
if (r == 'C')
return 100;
if (r == 'D')
return 500;
if (r == 'M')
return 1000;
return -1;
}
int romanToInt(string& s) {
int ret = 0;
for (int i = 0; i < s.length(); i++) {
int s1 = value(s[i]);
if (i + 1 < s.length()) {
int s2 = value(s[i + 1]);
if (s1 >= s2) {
ret = ret + s1;
}
else {
ret = ret + s2 - s1;
i++;
}
}
else {
ret = ret + s1;
}
}
return ret;
}
};
int main()
{
Solution m;
string str = "III";
cout << "Integer form of Roman Numeral is " << m.romanToInt(str) << endl;
return 0;
}
I am trying to use a pointer array where it reads the line letter by letter and recognizing the value of the letter in the function value(), I think I understand that my main needs to be formatted differently in order to do this task but I am a little stuck on how to do so.
You have probably defined int main twice. Considering you have an error on line 65 while your code is less than 60 lines long I would assume there is more code than what was copied here.
I am taking hard coded information of two arrays and creating the Union, Intersection and Difference of them.
My issue is that I can't get the Intersection or Difference to print with the right format.
I have tried a different variety of while loops with for and if statements, but can't quite get it. I know I'm close-ish. (Hopefully)
Main information:
int setA[] = {3,4,9,12,13,15};
int setB[] = {1,3,5,7,9};
int lenA = sizeof(setA) / sizeof(setA[0]);
int lenB = sizeof(setB) / sizeof(setB[0]);
Difference(setA, setB, lenA, lenB);
Intersection(setA, setB, lenA, lenB);
void Intersection(int setA[], int setB[], int lenA, int lenB)
{
cout << "AnB = {";
for (int i = 0; i < lenA; i++)
{
for (int j = 0; j < lenB; j++)
{
while(setA[i] == setB[j] && i < lenA - 1)
{
cout << setA[i++] << ", ";
}
if(i < lenA && setA[i] == setB[j])
{
cout << setA[i++];
}
}
}
cout << "}";
cout << endl;
}
void Difference(int setA[], int setB[], int lenA, int lenB)
{
cout << "A - B = {";
for (int i = 0; i < lenA; i++)
{
bool comp = false;
for (int j = 0; j < lenB; j++)
if (setA[i] == setB[j])
{
comp = true;
break;
}
if(!comp)
{
cout << setA[i] << ",";
}
}
cout << "}";
cout << endl;
}
I want the output to look like:
AnB = {3, 9} (Intersection)
A-B = {4, 12, 13, 15} (Difference)
What I am getting:
AnB = {3, 9, } (Intersection)
A-B = {4, 12, 13, 15, } (Difference)
I just need to get the commas at the end fixed, but don't know how.
Sorry if this question is way too long, I wasn't sure of how to get my issue out.
I have looked extensively for the problem in this code, but I can't seem to figure out what tragic error I made and why it is triggering a breakpoint.
(After 3 or 4 inputs, it triggers and I don't know why it doesn't trigger at the start or what is causing it)
#include <conio.h> // For function getch()
#include <cstdlib> // For several general-purpose functions
#include <fstream> // For file handling
#include <iomanip> // For formatted output
#include <iostream> // For cin, cout, and system
#include <string> // For string data type
using namespace std; // So "std::cout" may be abbreviated to "cout", for example.
string convertDecToBin(int dec)
{
int *arrayHex, arraySize = 0;
arrayHex = new int[];
string s = " ";
int r = dec;
for (int i = 0; r != 0; i++)
{
arrayHex[i] = r % 2;
r = r / 2;
arraySize++;
}
for (int j = 0; j < arraySize; j++)
{
s = s + to_string(arrayHex[arraySize - 1 - j]);
}
delete[] arrayHex;
return s;
}
string convertDecToOct(int dec)
{
int *arrayHex, arraySize = 0;
arrayHex = new int[];
string s = " ";
int r = dec;
for (int i = 0; r != 0; i++)
{
arrayHex[i] = r % 8;
r = r / 8;
arraySize++;
}
for (int j = 0; j < arraySize; j++)
{
s = s + to_string(arrayHex[arraySize - 1 - j]);
}
delete[] arrayHex;
return s;
}
int main()
{
int input = 0;
while (input != -1)
{
cout << "\nEnter a decimal number (-1 to exit loop): ";
cin >> input;
if (input != -1)
{
cout << "Your decimal number in binary expansion: " << convertDecToBin(input);
cout << "\nYour decimal number in octal ecpression: " << convertDecToOct(input);
}
}
cout << "\n\nPress any key to exit. . .";
_getch();
return 0;
}
arrayHex = new int[] is your problem - C\C++ does not support dynamic sizing arrays. You need to specify a size for the array to allocation, otherwise you'll get memory block overruns.
Using Bottom to up DP approach, I am able to solve the problem How to solve http://www.spoj.com/problems/MST1/ upto 10^8.
If input is very large n upto 10^9. I will not be able to create lookup table for upto 10^9. So what will be better approach to solve the problem ?
Is there any heuristic solution ?
#include <iostream>
#include <climits>
#include <algorithm>
using namespace std;
int main()
{
const int N_MAX = 20000001;
int *DP = new int[N_MAX];
DP[1] = 0;
for (int i = 2; i < N_MAX; i++) {
int minimum = DP[i - 1];
if (i % 3 == 0) minimum = min(minimum, DP[i/3]);
if (i % 2 == 0) minimum = min(minimum, DP[i/2]);
DP[i] = minimum + 1;
}
int T, N; cin >> T;
int c = 1;
while (T--) {
cin >> N;
cout << "Case " << c++ << ": " << DP[N] << endl;
}
delete[] DP;
}
Given 2 strings like bangalore and blr, return whether one appears as a subsequence of the other. The above case returns true whereas bangalore and brl returns false.
Greedy strategy should work for this problem.
Find the first letter of the suspected substring (blr) in the big string (*b*angalore)
Find the second letter starting at the index of the first letter plus one (anga*l*ore)
Find the third letter starting at the index of the second letter plus one (o*r*e)
Continue until you can no longer find the next letter of blr in the string (no match), or you run out of letters in the subsequence (you have a match).
Here is a sample code in C++:
#include <iostream>
#include <string>
using namespace std;
int main() {
string txt = "quick brown fox jumps over the lazy dog";
string s = "brownfoxzdog";
int pos = -1;
bool ok = true;
for (int i = 0 ; ok && i != s.size() ; i++) {
ok = (pos = txt.find(s[i], pos+1)) != string::npos;
}
cerr << (ok ? "Found" : "Not found") << endl;
return 0;
}
// Solution 1
public static boolean isSubSequence(String str1, String str2) {
int i = 0;
int j = 0;
while (i < str1.length() && j < str2.length()) {
if (str1.charAt(i) == str2.charAt(j)) {
i++;
j++;
} else {
i++;
}
}
return j == str2.length();
}
// Solution 2 using String indexOf method
public static boolean isSubSequenceUsingIndexOf(String mainStr, String subStr) {
int i = 0;
int index = 0;
while(i<subStr.length()) {
char c = subStr.charAt(i);
if((index = mainStr.indexOf(c, index)) == -1) {
return false;
}
i++;
}
return true;
}
O(N) solution, where N is the length of the substring.
bool subsequence( string s1, string s2 ){
int n1 = s1.length();
int n2 = s2.length();
if( n1 > n2 ){
return false;
}
int i = 0;
int j = 0;
while( i < n1 && j < n2 ){
if( s1[i] == s2[j] ){
i++;
}
j++;
}
return i == n1;
}
Find the length of the longest common subsequence. If it is equal to the length of small string, return true