Skip to content

Commit 97cbbe3

Browse files
author
aswanidutt93
committed
Remove K digits
1 parent deb5f73 commit 97cbbe3

File tree

5 files changed

+173
-0
lines changed

5 files changed

+173
-0
lines changed

LeetCodeMayChallenge.sln

+6
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LeetCodeMayChallengeDay11",
2323
EndProject
2424
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "LeetCodeMayChallengeDay12", "LeetCodeMayChallengeDay12\LeetCodeMayChallengeDay12.csproj", "{08CFE6A7-BE29-4100-A477-D87CE4682C5F}"
2525
EndProject
26+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "RemoveKDigits", "RemoveKDigits\RemoveKDigits.csproj", "{3E200FF4-0D61-4CAB-AAD5-ED2FCE9DF8AF}"
27+
EndProject
2628
Global
2729
GlobalSection(SolutionConfigurationPlatforms) = preSolution
2830
Debug|Any CPU = Debug|Any CPU
@@ -69,6 +71,10 @@ Global
6971
{08CFE6A7-BE29-4100-A477-D87CE4682C5F}.Debug|Any CPU.Build.0 = Debug|Any CPU
7072
{08CFE6A7-BE29-4100-A477-D87CE4682C5F}.Release|Any CPU.ActiveCfg = Release|Any CPU
7173
{08CFE6A7-BE29-4100-A477-D87CE4682C5F}.Release|Any CPU.Build.0 = Release|Any CPU
74+
{3E200FF4-0D61-4CAB-AAD5-ED2FCE9DF8AF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
75+
{3E200FF4-0D61-4CAB-AAD5-ED2FCE9DF8AF}.Debug|Any CPU.Build.0 = Debug|Any CPU
76+
{3E200FF4-0D61-4CAB-AAD5-ED2FCE9DF8AF}.Release|Any CPU.ActiveCfg = Release|Any CPU
77+
{3E200FF4-0D61-4CAB-AAD5-ED2FCE9DF8AF}.Release|Any CPU.Build.0 = Release|Any CPU
7278
EndGlobalSection
7379
GlobalSection(SolutionProperties) = preSolution
7480
HideSolutionNode = FALSE

RemoveKDigits/App.config

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<configuration>
3+
<startup>
4+
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2" />
5+
</startup>
6+
</configuration>

RemoveKDigits/Program.cs

+72
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace RemoveKDigits
8+
{
9+
class Program
10+
{
11+
static void Main(string[] args)
12+
{
13+
var res=RemoveKdigits("100", 1);
14+
}
15+
16+
public static string RemoveKdigits(string num, int k)
17+
{
18+
if (num.Length <= k) { return "0"; }
19+
var res = string.Empty;
20+
Stack<char> fStack = new Stack<char>();
21+
foreach (var item in num)
22+
{
23+
while (fStack.Any() && fStack.Peek() > item && k > 0)
24+
{
25+
fStack.Pop();
26+
k--;
27+
}
28+
fStack.Push(item);
29+
}
30+
31+
while (fStack.Any() && k > 0)
32+
{
33+
fStack.Pop();
34+
k--;
35+
}
36+
res = new string(fStack.Reverse().ToArray());
37+
int zeroCount = 0;
38+
int nonZeroCount = 0;
39+
40+
41+
for (int i = 0; i < res.Length; i++)
42+
{
43+
if (res[i] == '0')
44+
{
45+
zeroCount++;
46+
}
47+
else
48+
{
49+
nonZeroCount++;
50+
break;
51+
}
52+
}
53+
54+
if (zeroCount > 0) {
55+
if(nonZeroCount > 0)
56+
{
57+
res = res.Substring(zeroCount);
58+
}
59+
else
60+
{
61+
res = "0";
62+
}
63+
64+
}
65+
66+
67+
return res;
68+
}
69+
70+
71+
}
72+
}
+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using System.Reflection;
2+
using System.Runtime.CompilerServices;
3+
using System.Runtime.InteropServices;
4+
5+
// General Information about an assembly is controlled through the following
6+
// set of attributes. Change these attribute values to modify the information
7+
// associated with an assembly.
8+
[assembly: AssemblyTitle("RemoveKDigits")]
9+
[assembly: AssemblyDescription("")]
10+
[assembly: AssemblyConfiguration("")]
11+
[assembly: AssemblyCompany("")]
12+
[assembly: AssemblyProduct("RemoveKDigits")]
13+
[assembly: AssemblyCopyright("Copyright © 2020")]
14+
[assembly: AssemblyTrademark("")]
15+
[assembly: AssemblyCulture("")]
16+
17+
// Setting ComVisible to false makes the types in this assembly not visible
18+
// to COM components. If you need to access a type in this assembly from
19+
// COM, set the ComVisible attribute to true on that type.
20+
[assembly: ComVisible(false)]
21+
22+
// The following GUID is for the ID of the typelib if this project is exposed to COM
23+
[assembly: Guid("3e200ff4-0d61-4cab-aad5-ed2fce9df8af")]
24+
25+
// Version information for an assembly consists of the following four values:
26+
//
27+
// Major Version
28+
// Minor Version
29+
// Build Number
30+
// Revision
31+
//
32+
// You can specify all the values or you can default the Build and Revision Numbers
33+
// by using the '*' as shown below:
34+
// [assembly: AssemblyVersion("1.0.*")]
35+
[assembly: AssemblyVersion("1.0.0.0")]
36+
[assembly: AssemblyFileVersion("1.0.0.0")]

RemoveKDigits/RemoveKDigits.csproj

+53
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
3+
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
4+
<PropertyGroup>
5+
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
6+
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
7+
<ProjectGuid>{3E200FF4-0D61-4CAB-AAD5-ED2FCE9DF8AF}</ProjectGuid>
8+
<OutputType>Exe</OutputType>
9+
<RootNamespace>RemoveKDigits</RootNamespace>
10+
<AssemblyName>RemoveKDigits</AssemblyName>
11+
<TargetFrameworkVersion>v4.7.2</TargetFrameworkVersion>
12+
<FileAlignment>512</FileAlignment>
13+
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
14+
<Deterministic>true</Deterministic>
15+
</PropertyGroup>
16+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
17+
<PlatformTarget>AnyCPU</PlatformTarget>
18+
<DebugSymbols>true</DebugSymbols>
19+
<DebugType>full</DebugType>
20+
<Optimize>false</Optimize>
21+
<OutputPath>bin\Debug\</OutputPath>
22+
<DefineConstants>DEBUG;TRACE</DefineConstants>
23+
<ErrorReport>prompt</ErrorReport>
24+
<WarningLevel>4</WarningLevel>
25+
</PropertyGroup>
26+
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
27+
<PlatformTarget>AnyCPU</PlatformTarget>
28+
<DebugType>pdbonly</DebugType>
29+
<Optimize>true</Optimize>
30+
<OutputPath>bin\Release\</OutputPath>
31+
<DefineConstants>TRACE</DefineConstants>
32+
<ErrorReport>prompt</ErrorReport>
33+
<WarningLevel>4</WarningLevel>
34+
</PropertyGroup>
35+
<ItemGroup>
36+
<Reference Include="System" />
37+
<Reference Include="System.Core" />
38+
<Reference Include="System.Xml.Linq" />
39+
<Reference Include="System.Data.DataSetExtensions" />
40+
<Reference Include="Microsoft.CSharp" />
41+
<Reference Include="System.Data" />
42+
<Reference Include="System.Net.Http" />
43+
<Reference Include="System.Xml" />
44+
</ItemGroup>
45+
<ItemGroup>
46+
<Compile Include="Program.cs" />
47+
<Compile Include="Properties\AssemblyInfo.cs" />
48+
</ItemGroup>
49+
<ItemGroup>
50+
<None Include="App.config" />
51+
</ItemGroup>
52+
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
53+
</Project>

0 commit comments

Comments
 (0)